C# 如何获得控件的屏幕截图?DrawToBitmap不工作
我有一个pictureBox,正在通过dll调用绘制到外部C# 如何获得控件的屏幕截图?DrawToBitmap不工作,c#,.net,winforms,drawtobitmap,C#,.net,Winforms,Drawtobitmap,我有一个pictureBox,正在通过dll调用绘制到外部 private void myPictureBox_Paint(object sender, PaintEventArgs e) { dllClass.RefreshEx(LWHANDLE, 0, 0); } 这一切都是可行的,但我现在需要得到那个图片盒的截图,但它不起作用 以下是我尝试过的: Control ctrlToDraw = myPictureBox; System.Drawing
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);
我也尝试通过WindowsAPI来实现,但结果与上面的代码完全相同:一个空白(非空)图像
除了拍摄整个屏幕的截图外,还有谁能说出一些建议吗?您可以使用ALT+PRINTSCREEN仅拍摄活动窗口的截图。将当前活动窗口作为位图捕获到剪贴板。将其粘贴到位图编辑工具中,裁剪到所需的区域
或者使用屏幕捕获实用程序,允许在屏幕上裁剪目标区域,例如Evernote版本2的旧屏幕裁剪器。我不知道您对外部程序有多大的控制,或者它如何绘制到图片框,但如果您使用createGraphics,它将无法工作
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
}
如果外部程序要绘制位图,则可以将该位图设置为picturebox背景
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;
}
第三次编辑右侧
这将采取一个屏幕截图,削减了图片框,然后我改变了形式的背景,只是为了证明它的工作
您需要添加
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;
}
请问我怎样才能通过代码来完成?我忘了提到背景可以动态变化。你的问题是“不是一个完整的屏幕截图”。您没有要求在需要代码时自动捕获屏幕的一部分。当你问问题时,这些细节很重要。我知道自动截屏实用程序,但我必须考虑这个新的要求。你确定你的外部调用实际上是绘制到图片框,因为我刚刚测试了你的代码,它工作得很好。是绘制到图片框,还是在picturebox图形对象上绘制?因为这里有一个持久性问题perhaps@K‘Leg It’与普通的pictureboxes配合得很好:(这是一个庞大的该死的项目,我不能轻易发布SSCCE。我需要知道的是你的外部程序是如何与这个程序对话的,以及绘制路线的代码外部程序的所有代码都是“句柄”picturebox的。这就是传递给它的所有内容。所以我猜它直接绘制到它,而不是设置图像或其他。不过我会尝试您的代码。“直接绘制到它”,可能是通过使用bitBlt和srcCopy。我现在没有访问源代码的权限。这太糟糕了,因为在我看来,它是在它上面绘制的,而不是在它上面绘制的。你能看到Picturebox吗,当它在它上面绘制时?一旦在图片框中有一个图像,尝试最小化该表单,然后将其恢复,该图像是否仍然存在这里?图像甚至从未出现在那里(因为它是由外部程序的其他图像绘制的)。我只能假设它一次又一次地重新粉刷得非常快。首先,在你的Picturebox中添加一些东西,看看它是否发生了变化,我认为你的问题就在那里。我为你写了一个新的示例,它完全符合你的需要。它需要一个屏幕截图,剪切你的Picturebox供你使用,但如果你看不到一个在你的盒子里,也没有任何东西可以截图