English 中文(简体)
如何获得控制截图? DrawToBitmap 无效
原标题:How can I get a screenshot of control? DrawToBitmap not working

我有一个图片框 被画到外部 通过dll调用。

private void myPictureBox_Paint(object sender, PaintEventArgs e)
{
     dllClass.RefreshEx(LWHANDLE, 0, 0);
}

这是有效的,所有, 但我现在需要得到一个截图 图片框的截图, 它没有工作。

我曾经尝试过:

        Control ctrlToDraw = myPictureBox;

        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ctrlToDraw.Width, ctrlToDraw.Height);
        ctrlToDraw.DrawToBitmap(bmp, ctrlToDraw.ClientRectangle); 

我还试图通过Windows API来完成,但结果与上述代码完全相同:一张空白(不是空的)图像。

除了截取整个屏幕的截图之外,谁能说出一些建议呢?

最佳回答

我不知道你对外部程序有多少控制, 或者它如何吸引到你的图片盒, 但如果你正在使用创建的Graphics, 它就行不通了。

private void button1_Click(object sender, EventArgs e)
    {
        //here I am calling the graphics object of the Picture Box, this will draw to the picture box
        //But the DrawToBitmap, will not reflect this change, and once the Picturebox needs to be updated, this will disappear.
        Graphics g = pictureBox1.CreateGraphics();
        g.DrawRectangle(Pens.Blue, 10, 10, 20, 20);

        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(pictureBox1.Width, pictureBox1.Height);
        Rectangle bounds = new Rectangle(Left, Top, Width, Height);
        pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
        //Draws whatever is in the PictureBox to the Forms BackgroundImage
        this.BackgroundImage = bmp;
        //It will not draw the Blue rectangle

    }

如果您的外部程序要绘制位图, 那么您可以设置该位图到图片框背景

    Bitmap buffer;
    public Form1()
    {
        InitializeComponent();
        buffer = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    }


    private void button1_Click(object sender, EventArgs e)
    {
        //draw to the bitmap named buffer
        using (Graphics g = Graphics.FromImage(buffer))
        {
            g.DrawRectangle(Pens.Blue, 10, 10, 20, 20);
        }
        //assign the picturebox image to buffer
        pictureBox1.Image = buffer;

        //Now this will show the blue rectangle
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(pictureBox1.Width, pictureBox1.Height);
        Rectangle bounds = new Rectangle(Left, Top, Width, Height);
        pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);

        this.BackgroundImage = bmp;
    }

< 强力 > EDIT 第三次

这将采取屏幕拍摄, 剪切图片框出来, 然后我改变了形式背景, 只是为了证明它有效。

您需要添加

using System.Drawing.Imaging;

用于像素格式。

 private void button1_Click(object sender, EventArgs e)
    {
        using (Graphics G = pictureBox1.CreateGraphics())
        {
            G.DrawRectangle(Pens.Blue, 10, 10, 10, 10);
        }
        Bitmap BMP = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                        Screen.PrimaryScreen.Bounds.Height,
                                        PixelFormat.Format32bppArgb);
        using (Graphics GFX = Graphics.FromImage(BMP))
        {
            GFX.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                Screen.PrimaryScreen.Bounds.Y,
                                0, 0,
                                Screen.PrimaryScreen.Bounds.Size,
                                CopyPixelOperation.SourceCopy);
        }
        Bitmap YourPictureBoxImage = new Bitmap(pictureBox1.Width,pictureBox1.Height);
        using (Graphics g = Graphics.FromImage(YourPictureBoxImage))
        {
            Point np = pictureBox1.PointToScreen(new Point(0, 0));
            g.DrawImage(BMP,new Rectangle(0,0,100,100),new Rectangle(np,pictureBox1.Size),GraphicsUnit.Pixel);
        }

        this.BackgroundImage = YourPictureBoxImage;
    }
问题回答

您可以使用 ALT+PRINTSCREEN 来抓取活动窗口的截图。 这将捕捉当前活动窗口作为剪贴板的位图。 粘贴到位图编辑工具中, 将鼠标刻到您想要的区域 。

或者使用屏幕捕捉功能,以便在“备注”第二版旧屏幕剪刀等屏幕上对目标区域进行裁剪。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签