6

win32 PrintWindow 関数を使用して、画面を BitMap オブジェクトにキャプチャしています。

ウィンドウの一部だけをキャプチャしたい場合、メモリ内の画像をトリミングするにはどうすればよいですか?

ウィンドウ全体をキャプチャするために使用しているコードは次のとおりです。

[System.Runtime.InteropServices.DllImport(strUSER32DLL, CharSet = CharSet.Auto, SetLastError = true)]
public static extern int PrintWindow(IntPtr hWnd, IntPtr hBltDC, uint iFlags);

public enum enPrintWindowFlags : uint
{
    /// <summary>
    /// 
    /// </summary>
    PW_ALL = 0x00000000,
    /// <summary>
    /// Only the client area of the window is copied. By default, the entire window is copied.
    /// </summary>
    PW_CLIENTONLY = 0x00000001
}

public System.Drawing.Bitmap CaptureWindow(IntPtr hWnd, enPrintWindowFlags eFlags)
{
    System.Drawing.Rectangle rctForm = System.Drawing.Rectangle.Empty;

    using(System.Drawing.Graphics grfx = System.Drawing.Graphics.FromHdc(GetWindowDC(hWnd)))
    {
        rctForm = System.Drawing.Rectangle.Round(grfx.VisibleClipBounds);
    }

    System.Drawing.Bitmap pImage = new System.Drawing.Bitmap(rctForm.Width, rctForm.Height);
    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(pImage);

    IntPtr hDC = graphics.GetHdc();        
    //paint control onto graphics using provided options        
    try 
    {            
        PrintWindow(hWnd, hDC, (uint)eFlags);     
    } 
    finally 
    {            
        graphics.ReleaseHdc(hDC);        
    }    
    return pImage;
}
4

3 に答える 3

3

画面をキャプチャして 100 ピクセル四方のトリミングされた画像を作成する完全なコードを次に示します。コードは、ボタンのクリック イベントから取得されます。必要なものを使用してください。

Bitmap screenShot = null;
         Bitmap croppedImage;
         Graphics screen;

         if(saveFileDialog.ShowDialog() == DialogResult.OK)
         {
            this.Hide();
            screenShot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
                                    Screen.PrimaryScreen.Bounds.Height,
                                    PixelFormat.Format32bppArgb);
            screen = Graphics.FromImage(screenShot);
            screen.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                    Screen.PrimaryScreen.Bounds.Y,
                                    0,
                                    0,
                                    Screen.PrimaryScreen.Bounds.Size,
                                    CopyPixelOperation.SourceCopy);
            screenShot.Save(saveFileDialog.FileName, ImageFormat.Png);
            this.Show();
         }

         //crop image
         if(screenShot != null)
         {
            if(saveFileDialog.ShowDialog() == DialogResult.OK)
            {
               int x = 100;
               int y = 100;
               int xWidth = 100;
               int yHeight = 100;
               Rectangle rect = new Rectangle(x, y, xWidth, yHeight);
               croppedImage = screenShot.Clone(rect, PixelFormat.Format32bppArgb);
               if (croppedImage != null)
               {
                  croppedImage.Save(saveFileDialog.FileName, ImageFormat.Png);
               }     
            }                   
         }
于 2010-07-13T21:35:43.203 に答える
3

画面全体を簡単に取得してから、画像全体の領域を選択するクロッピング関数に画像を渡すことができます。Bitmap.Clone() メソッドを見てください。例えば

public Bitmap CropBitmap(Bitmap bitmap, int cropX, int cropY, int cropWidth, int cropHeight)
{
Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat);
return cropped;
}

注、私はこのブログからこれを引っ張ってきました

于 2010-07-13T20:34:50.680 に答える
0

手間を省いて、ソースをCropperに取得してください。

于 2010-07-13T20:43:49.333 に答える