我想使用 GDI+ 在后台线程上渲染图像。我发现这个例子关于如何使用 GDI+ 旋转图像,这是我想做的操作。
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();
}
我的问题有两个部分:
你会如何完成this.CreateGraphics()
在后台线程上?是否可以?我的理解是 UI 对象是this
在这个例子中。因此,如果我在后台线程上进行此处理,我将如何创建图形对象?
完成处理后,如何从我正在使用的 Graphics 对象中提取位图?我还没有找到一个很好的例子来说明如何做到这一点。
另外:格式化代码示例时,如何添加换行符?如果有人能给我留言解释我真的很感激。谢谢!
要在位图上绘图,您不想创建Graphics
UI 控件的对象。你创建一个Graphics
使用位图对象FromImage
method:
Graphics g = Graphics.FromImage(theImage);
A Graphics
对象不包含您绘制的图形,它只是在另一个画布上绘制的工具,该画布通常是屏幕,但也可以是Bitmap
object.
因此,您不是先绘制然后提取位图,而是先创建位图,然后创建Graphics
要在其上绘制的对象:
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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)