2017-07-27 118 views
0

在Form1構造爲什麼在裁剪圖像時我會得到裁剪圖像的黑色圖像?

public Form1() 
     { 
      InitializeComponent(); 

      BackColor = Color.LightGreen; 
      TransparencyKey = Color.LightGreen; 
      this.TopMost = true; 
      this.Location = new Point(0, 0); 
      timer1.Enabled = true; 
     } 

然後:

private void Cap() 
     { 
      countImages++;    
      ScreenCapture sc = new ScreenCapture();    
      Image img = sc.CaptureScreen(); 
      Bitmap bmp = new Bitmap(img); 
      Bitmap source = new Bitmap(this.Width, this.Height); 
      Rectangle section = new Rectangle(new Point(source.Width, source.Height), source.Size); 
      Bitmap CroppedImage = CropImage(source, section); 
      CroppedImage.Save("c:\\temp\\screens\\" + countImages + ".gif", ImageFormat.Gif); 
      source.Dispose(); 
     } 

而且

public Bitmap CropImage(Bitmap source, Rectangle section) 
     { 
      // An empty bitmap which will hold the cropped image 
      Bitmap bmp = new Bitmap(section.Width, section.Height); 

      Graphics g = Graphics.FromImage(bmp); 

      // Draw the given area (section) of the source image 
      // at location 0,0 on the empty bitmap (bmp) 
      g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel); 

      return bmp; 
     } 

位圖我保存到硬盤:

CroppedImage.Save("c:\\temp\\screens\\" + countImages + ".gif", ImageFormat.Gif); 

他們都是黑色的。 我想從圖像裁剪這部分並將其保存到硬盤上。截圖

例子,我想從中裁剪什麼: 我打上一個紅色的圓圈透明Form1的,這就是我要裁剪的Form1的一部分,並保存爲裁剪圖像Form1的部分:

Form1 to crop

我想這第一:

private void CaptureDesktop() 
     { 
      countImages++; 
      ScreenCapture sc = new ScreenCapture();      
      Image img = sc.CaptureScreen(); 
      Bitmap bmp = new Bitmap(img);    
      Bitmap source = new Bitmap(this.Width, this.Height); 
      Rectangle section = new Rectangle(new Point(source.Width, source.Height), source.Size); 
      Bitmap CroppedImage = CropImage(bmp, section); 
      CroppedImage.Save("c:\\temp\\desktopscreens\\" + countImages + ".gif", ImageFormat.Gif); 
     } 

,但我發現這個裁剪圖像的結果:在畫面詭異的畫面?

Pinp

然後我嘗試這種代碼:

using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
             Screen.PrimaryScreen.Bounds.Height)) 
      { 
       using (Graphics g = Graphics.FromImage(bmpScreenCapture)) 
       { 

        g.CopyFromScreen(this.Location.X, 
            this.Location.Y, 
            0, 0, 
            this.Size, 
            CopyPixelOperation.SourceCopy); 


       } 

       Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height)); 
       Bitmap CroppedImage = CropImage(bmpScreenCapture, section); 
       CroppedImage.Save("c:\\temp\\desktopscreens\\1.bmp", ImageFormat.Bmp); 


      } 

但結果是:

pinp001

不是我想要的。我只想裁剪整個圖像中的form1部分。

回答

1

ScreenShot

嘗試此(從Capture the screen shot using .NET一些代碼)

private void Cap() 
    { 
     using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
             Screen.PrimaryScreen.Bounds.Height)) 
     { 
      using (Graphics g = Graphics.FromImage(bmpScreenCapture)) 
      { 

       g.CopyFromScreen(this.Location.X, 
           this.Location.Y, 
           0, 0, 
           this.Size, 
           CopyPixelOperation.SourceCopy); 


      } 

      Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height)); 
      Bitmap CroppedImage = CropImage(bmpScreenCapture, section); 
      CroppedImage.Save("c:\\temp\\screens\1.bmp", ImageFormat.Bmp); 


     } 




    } 

    public Bitmap CropImage(Bitmap source, Rectangle section) 
    { 
     // An empty bitmap which will hold the cropped image 
     Bitmap bmp = new Bitmap(section.Width, section.Height); 

     Graphics g = Graphics.FromImage(bmp); 

     // Draw the given area (section) of the source image 
     // at location 0,0 on the empty bitmap (bmp) 
     g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel); 

     return bmp; 
    } 
+0

即使該行首先是CroppedImage.Save(「c:\\ temp \\ screens \\ 1.bmp」,ImageFormat.Bmp);同樣的例外。 –

+0

可能是權限。你可以嘗試將它保存到另一個文件夾。 – SQLAndOtherStuffGuy

+0

另外。文件可能由於某種原因而被讀取。 – SQLAndOtherStuffGuy

2

你做Bitmap source = new Bitmap(this.Width, this.Height);這會創建一個黑色的圖像。

然後你繼續裁剪它。這仍然是一個黑色的形象。

然後你繼續保存它。這仍然是一個黑色的形象。

也許你的意思是裁剪Bitmap bmpImage img而不是Bitmap source。我不知道Bitmap source是什麼意思。

+0

當我使在構造form1中是透明的:TransparencyKey = Color.LightGreen;然後當我採取整個桌面的屏幕截圖時:Image img = sc.CaptureScreen();我現在想從桌面的img中只剪切透明form1的一部分。 –

+0

我編輯我的問題與截圖的例子,並用紅色圓圈標記我想裁剪的部分。位圖源應該是form1的一部分。至少這就是我的意思。所以我明白它是黑色的,但我怎麼能從bmp只剪切form1的部分? –

+0

這是一個不同的問題。你的問題是「我爲什麼變黑」,這就是我的回答。 –