我一直在绘制设备上下文,现在我希望能够将设备上下文的内容保存到图像中。如果直接从位图保存不是最好的方法,那么我如何从设备上下文到位图?我试图在C#中做到这一点。
编辑:感谢SeriesOne,我能够修改他的代码,将DC保存到BMP中。以下是我改变它的方式:
Rectangle bmpRect = new Rectangle(0, 0, 640, 480);
// Create a bitmap
using (Bitmap bmp = new Bitmap(bmpRect.Width, bmpRect.Height))
{
Graphics gfx = Graphics.FromHdc(hdcScreen);
bmp.Save("C:\\MyBitmap.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
gfx.Dispose();
}
答案 0 :(得分:0)
// Set the size/location of your bitmap rectangle.
Rectangle bmpRect = new Rectangle(0, 0, 640, 480);
// Create a bitmap
using (Bitmap bmp = new Bitmap(bmpRect.Width, bmpRect.Height))
{
// Create a graphics context to draw to your bitmap.
using (Graphics gfx = Graphics.FromImage(bmp))
{
//Do some cool drawing stuff here
gfx.DrawEllipse(Pens.Red, bmpRect);
}
//and save it!
bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\MyBitmap.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
如果要将位图保存为文件,这很有效。这使用GDI +(主要是软件渲染),因此在渲染静态文件时性能不是很大的问题。
您还可以使用它在渲染控件时创建屏幕外图形缓冲区。在这种情况下,您只需删除save语句,并将位图内容写入控件设备上下文。