且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Windows应用程序中将表单内容转换为图像

更新时间:2023-10-13 09:06:52

在这里,您可以...保持表单在屏幕上可见. 矩形对象是表单相对于屏幕的矩形区域.然后可以保存位图.

Here you go... keep the form visible on the screen..
Rectangle object is the rectangle area of the form with respect to screen. You can save the Bitmap then.

private Bitmap CaptureImage(Control wnd, Rectangle rc)
{
	Bitmap result;
	if (rc.Width > 0 && rc.Height > 0)
	{
		Bitmap bitmap = null;
		Image[] array = new Bitmap[1];
		using (Graphics graphics = wnd.CreateGraphics())
		{
			bitmap = new Bitmap(rc.Width, rc.Height, graphics);
			using (Graphics graphics2 = Graphics.FromImage(bitmap))
			{
				graphics2.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);
			}
		}
		array[0] = bitmap;
		result = bitmap;
	}
	else
	{
		result = null;
	}
	return result;
}


除了Vivek的解决方案:

如何获得覆盖所有表格的矩形?使用Control.PointToScreen:

In addition to the solution by Vivek:

How to get the Rectangle which covers all the Form? Use Control.PointToScreen:

class MyForm : Form {
    //...
    Rectangle FormToScreen() {
        return new Rectangle(
            this.PointToScreen(new Point(0, 0)),
            new Size(this.Width, this.Height));
    }
}



—SA



—SA