加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – GDI:如何在背景线程上将Graphics对象渲染为位图?

发布时间:2020-12-15 23:52:20 所属栏目:百科 来源:网络整理
导读:我想使用GDI在后台线程上渲染图像.我找到了关于如何使用GDI旋转图像的 this example,这是我想要做的操作. private void RotationMenu_Click(object sender,System.EventArgs e){ Graphics g = this.CreateGraphics(); g.Clear(this.BackColor); Bitmap curBi
我想使用GDI在后台线程上渲染图像.我找到了关于如何使用GDI旋转图像的 this example,这是我想要做的操作.

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,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,200),curBitmap.Width,curBitmap.Height,GraphicsUnit.Pixel);  

    // Dispose of objects
    curBitmap.Dispose();
    g.Dispose(); 
}

我的问题有两个部分:

>你将如何在后台线程上完成this.CreateGraphics()?可能吗?我的理解是在这个例子中是一个UI对象.因此,如果我在后台线程上进行此处理,我将如何创建图形对象?
>一旦我完成处理,我将如何从我正在使用的Graphics对象中提取位图?我无法找到一个如何做到这一点的好例子.

另外:格式化代码示例时,如何添加换行符?如果有人可以给我发表评论,说明我真的很感激.谢谢!

解决方法

要绘制位图,您不希望为UI控件创建Graphics对象.您可以使用FromImage方法为位图创建Graphics对象:

Graphics g = Graphics.FromImage(theImage);

Graphics对象不包含您绘制到它的图形,而只是它在另一个画布上绘制的工具,通常是屏幕,但它也可以是Bitmap对象.

因此,您不先绘制然后提取位图,首先创建位图,然后创建要在其上绘制的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,200);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读