gpt4 book ai didi

c# - 如何获取控件的屏幕截图? DrawToBitmap 不工作

转载 作者:行者123 更新时间:2023-11-30 17:12:21 24 4
gpt4 key购买 nike

我有一个 pictureBox 正在通过 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 执行此操作,但结果与上述代码完全相同:一张空白(非空)图像。

除了截取整个屏幕外,谁能提出一些建议?

最佳答案

我不知道您对外部程序有多少控制权,或者它如何绘制到您的图片框,但如果您使用 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

}

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

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

关于c# - 如何获取控件的屏幕截图? DrawToBitmap 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10741384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com