c# winforms:获取必须位于控件后面的屏幕截图图像

发布于 2024-08-03 02:22:21 字数 165 浏览 4 评论 0原文

我有 C# Windows 窗体,上面有几个控件,部分控件一个接一个地位于另一个控件上。我想要一个函数,该函数将从表单中获取控件作为输入,并返回必须位于控件后面的图像。例如:如果表单具有背景图像并包含一个按钮 - 如果我运行此函数,我将获得位于按钮后面的背景图像部分。有什么想法和代码吗?

帮助!!!

I have c# windows form which have several controls on it, part of the controls are located one on another. I want a function that will take for input a control from the form and will return the image that has to be behind the control. for ex: if the form has backgroundimage and contains a button on it - if I'll run this function I'll got the part of backgroundimage that located behind the button. any Idea - and code?

H-E-L-P!!!

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

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

发布评论

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

评论(2

仙女 2024-08-10 02:22:21

这是我最初的猜测,但必须进行测试。

  • 将按钮隐形
  • 捕获当前屏幕
  • 将捕获的屏幕裁剪到按钮的clientRectangle
  • 重新建立按钮。

    public static Image GetBackImage(Control c) {   
        c.可见=假;
        var bmp = GetScreen();
        var img = CropImage(bmp, c.ClientRectangle);
        c.可见=真;
    }
    
    公共静态位图 GetScreen() {
        int 宽度 = SystemInformation.PrimaryMonitorSize.Width;
        int height = SystemInformation.PrimaryMonitorSize.Height;
    
        矩形 screenRegion = Screen.AllScreens[0].Bounds;
        var bitmap = new Bitmap(宽度, 高度, PixelFormat.Format32bppArgb);
        图形图形 = Graphics.FromImage(位图);
        Graphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size);
        返回位图;
    }
    公共静态无效CropImage(图像imagenOriginal,矩形areaCortar){
        图形g = null;
        尝试 {
            //创建目标(裁剪后的)位图
            var bmpCropped = new Bitmap(areaCortar.Width,areaCortar.Height);
            //创建用于绘制的图形对象
            g = Graphics.FromImage(bmpCropped);
    
            var rectDestination = new 矩形(0, 0, bmpCropped.Width, bmpCropped.Height);
    
            //将原图的areaCortar绘制到bmpCropped的rectDestination上
            g.DrawImage(imagenOriginal,rectDestination,areaCortar,GraphicsUnit.Pixel);
            //释放系统资源
        } 最后 {
            如果(g!=空){
                g.Dispose();
            }
        }
    }
    

That's my initial guess, but have to test it.

  • Put button invisible
  • capture current screen
  • Crop screen captured to the clientRectangle of the button
  • Restablish button.

    public static Image GetBackImage(Control c) {   
        c.Visible = false;
        var bmp = GetScreen();
        var img = CropImage(bmp, c.ClientRectangle);
        c.Visible = true;
    }
    
    public static Bitmap GetScreen() {
        int width = SystemInformation.PrimaryMonitorSize.Width;
        int height = SystemInformation.PrimaryMonitorSize.Height;
    
        Rectangle screenRegion = Screen.AllScreens[0].Bounds;
        var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
        Graphics graphics = Graphics.FromImage(bitmap);
        graphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size);
        return bitmap;
    }
    public static void CropImage(Image imagenOriginal, Rectangle areaCortar) {
        Graphics g = null;
        try {
            //create the destination (cropped) bitmap
            var bmpCropped = new Bitmap(areaCortar.Width, areaCortar.Height);
            //create the graphics object to draw with
            g = Graphics.FromImage(bmpCropped);
    
            var rectDestination = new Rectangle(0, 0, bmpCropped.Width, bmpCropped.Height);
    
            //draw the areaCortar of the original image to the rectDestination of bmpCropped
            g.DrawImage(imagenOriginal, rectDestination, areaCortar, GraphicsUnit.Pixel);
            //release system resources
        } finally {
            if (g != null) {
                g.Dispose();
            }
        }
    }
    
预谋 2024-08-10 02:22:21

这很容易做到。窗体上的每个控件都有一个 Size 和一个 Location 属性,您可以使用它们实例化一个新的 Rectangle,如下所示:

Rectangle rect = new Rectangle(button1.Location, button1.Size);

要获取包含位于控件后面的背景图像部分的位图,您首先创建正确的尺寸:

Bitmap bmp = new Bitmap(rect.Width, rect.Height);

然后为新的位图创建一个 Graphics 对象,并使用该对象的 DrawImage 方法复制背景图像的一部分:

using (Graphics g = Graphics.FromImage(bmp))
{
    g.DrawImage(...); // sorry, I don't recall which of the 30 overloads
        // you need here, but it will be one that uses form1.Image as
        // the source, and rect for the coordinates of the source
}

这将为您留下包含该控件下方的背景图像部分的新位图 (bmp) 。

抱歉,我无法在代码中提供更具体的信息 - 我在公共终端。但智能感知信息会告诉您需要为 DrawImage 方法传入什么内容。

This is pretty easy to do. Each control on the form has a Size and a Location property, which you can use to instantiate a new Rectangle, like so:

Rectangle rect = new Rectangle(button1.Location, button1.Size);

To get a Bitmap that contains the portion of the background image located behind the control, you first create a Bitmap of the proper dimensions:

Bitmap bmp = new Bitmap(rect.Width, rect.Height);

You then create a Graphics object for the new Bitmap, and use that object's DrawImage method to copy a portion of the background image:

using (Graphics g = Graphics.FromImage(bmp))
{
    g.DrawImage(...); // sorry, I don't recall which of the 30 overloads
        // you need here, but it will be one that uses form1.Image as
        // the source, and rect for the coordinates of the source
}

This will leave you with the new Bitmap (bmp) containing the portion of the background image underneath that control.

Sorry I can't be more specific in the code - I'm at a public terminal. But the intellisense info will tell you what you need to pass in for the DrawImage method.

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