GDI +:你怎么渲染图形对象在后台线程位图? [英] GDI+: how do you render a Graphics object to a bitmap on a background thread?
问题描述
我想使用GDI +在后台线程渲染图像。我发现本例中如何旋转使用GDI +的图像的,这是我喜欢做的操作。
I'd like to use GDI+ to render an image on a background thread. I found this example on how to rotate an image using GDI+, which is the operation I'd like to do.
private void RotationMenu_Click(object sender, System.EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
Bitmap curBitmap = new Bitmap(@"roses.jpg");
g.DrawImage(curBitmap, 0, 0, 200, 200);
// Create a Matrix object, call its Rotate method,
// and set it as Graphics.Transform
Matrix X = new Matrix();
X.Rotate(30);
g.Transform = X;
// Draw image
g.DrawImage(curBitmap,
new Rectangle(205, 0, 200, 200),
0, 0, curBitmap.Width,
curBitmap.Height,
GraphicsUnit.Pixel);
// Dispose of objects
curBitmap.Dispose();
g.Dispose();
}
我的问题有两个部分:
My question has two parts:
-
你会如何完成
this.CreateGraphics()
在后台线程?是否可以?我的理解是,一个UI对象是这个
在这个例子中。所以,如果我在后台线程这样处理,我将如何创建一个图形对象?
How would you accomplish
this.CreateGraphics()
on a background thread? Is it possible? My understanding is that a UI object isthis
in this example. So if I'm doing this processing on a background thread, how would I create a graphics object?
我怎么会那么提取的Graphics对象,位图我使用的是一旦我做了处理?我一直没能找到该怎么做一个很好的例子。
How would I then extract a bitmap from the Graphics object I'm using once I'm done processing? I haven't been able to find a good example of how to do that.
另外:格式化代码样本时,如何添加换行符?如果有人可以给我留下了评论,解释说我会很感激。谢谢!
Also: when formatting a code sample, how do I add newlines? If someone could leave me a comment explaining that I'd really appreciate it. Thanks!
推荐答案
要绘制一个位图,你不希望创建一个图形
对象的UI控件。创建使用 FromImage
方法的位图图形
目标:
To draw on a bitmap you don't want to create a Graphics
object for an UI control. You create a Graphics
object for the bitmap using the FromImage
method:
Graphics g = Graphics.FromImage(theImage);
A 图形
对象不包含您绘制它的图形,而不是它只是借鉴其他画布,这通常是在屏幕的一个工具,但它也可以是一个位图
对象。
A Graphics
object doesn't contain the graphics that you draw to it, instead it's just a tool to draw on another canvas, which is usually the screen, but it can also be a Bitmap
object.
所以,你不画,然后再提取位图,首先要创建位图,然后创建图形
对象借鉴它:
So, you don't draw first and then extract the bitmap, you create the bitmap first, then create the Graphics
object to draw on it:
Bitmap destination = new Bitmap(200, 200);
using (Graphics g = Graphics.FromImage(destination)) {
Matrix rotation = new Matrix();
rotation.Rotate(30);
g.Transform = rotation;
g.DrawImage(source, 0, 0, 200, 200);
}
这篇关于GDI +:你怎么渲染图形对象在后台线程位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!