如何从C#中的图形对象获取的位图/图像? [英] How to get the bitmap/image from a Graphics object in C#?
问题描述
我想知道缓冲区的中间状态的图形对象是借鉴一些东西。我怎么弄个位图或者说,它是借鉴了图像的?
I want to know what the intermediate state of the buffer where the Graphics object is drawing some stuff. How do I get hold of the bitmap or the image that it is drawing on?
推荐答案
我真的不知道,如果我知道你要问什么,因为你的问题很清楚。
I'm not really sure if I understand what you're asking for, as your question is very unclear.
,那么答案是,有没有直接的方法这样做。绘制图形
对象是一个单向操作。
If you want to know how to save the contents of a Graphics
object to a bitmap, then the answer is that there's no direct approach for doing so. Drawing on a Graphics
object is a one-way operation.
更好的选择是创建一个新的位图
对象,获得该位图图形
对象,并绘制直接到它。下面code是一个你可以做到这一点的例子:
The better option is to create a new Bitmap
object, obtain a Graphics
object for that bitmap, and draw directly onto it. The following code is an example of how you might do that:
// Create a new bitmap object
using (Bitmap bmp = new Bitmap(200, 300))
{
// Obtain a Graphics object from that bitmap
using (Graphics g = Graphics.FromImage(bmp))
{
// Draw onto the bitmap here
// ....
g.DrawRectangle(Pens.Red, 10, 10, 50, 50);
}
// Save the bitmap to a file on disk, or do whatever else with it
// ...
bmp.Save("C:\\MyImage.bmp");
}
这篇关于如何从C#中的图形对象获取的位图/图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!