C# 如何将图形转换为图像或位图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12882363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:47:49  来源:igfitidea点击:

How to convert Graphics into Image or Bitmap?

c#bitmap

提问by James Andrew Cruz

How to convert Graphics into Image or Bitmap?

如何将图形转换为图像或位图?

I have this code and it successfully crops my image in a picturebox but when I try to save it into a database.. it's empty.

我有这个代码,它成功地在图片框中裁剪了我的图像,但是当我尝试将它保存到数据库中时..它是空的。

Bitmap sourceBitmap = new Bitmap(pctImage.Image, pctImage.Width, pctImage.Height);
Graphics g = frmAdd.pctImage.CreateGraphics();

Rectangle rectCropArea;
rectCropArea = new Rectangle(50, 3, 230, 240);

g.DrawImage(sourceBitmap, new Rectangle(0, 0, frmAdd.pctImage.Width, frmAdd.pctImage.Height), rectCropArea, GraphicsUnit.Pixel);
sourceBitmap.Dispose();

What should I do with this one? Thanks.

我该怎么办?谢谢。

回答by Ravindra Bagale

like this:

像这样:

  Bitmap bmp = new Bitmap(100,100,graphics);

回答by Caster Troy

Use a structure like this:

使用这样的结构:

using (Bitmap bitmap = new Bitmap(rectangle.Width, rectangle.Height))
        {
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
               //draw image..
            }
            return bitmap;
        }