如何捕获屏幕的一部分

发布于 2024-09-10 09:26:00 字数 1429 浏览 1 评论 0原文

我正在使用 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;
}

I am using the win32 PrintWindow function to capture a screen to a BitMap object.

If I only want to capture a region of the window, how can I crop the image in memory?

Here is the code I'm using to capture the entire window:

[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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧 2024-09-17 09:26:00

您可以简单地抓取整个屏幕,然后将图像传递到裁剪函数中,该函数选择整个图像的一个区域。看一下 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;
}

,请注意,我从 此博客中提取了此内容

You could simply grab the whole screen and then pass the image into a cropping function that selects a region of the total image. Take a look at the Bitmap.Clone() method. e.g.

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;
}

Note, I pulled this down from this blog

七颜 2024-09-17 09:26:00

以下是捕获屏幕并创建 100 像素正方形的裁剪图像的完整代码。该代码取自按钮 Click 事件。使用你需要的东西。

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);
               }     
            }                   
         }

Here's the complete code to capture the screen and create a cropped image of 100 pixels square. The code is taken from a button Click event. Use what you need.

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);
               }     
            }                   
         }
初熏 2024-09-17 09:26:00

为自己省去一些麻烦,请获取 Cropper 的源代码。

Save yourself some trouble and pick up the source to Cropper.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文