C# Img类转Bitmap类

来源:百度知道 编辑:UC知道 时间:2024/06/07 06:53:49
请问怎么实现,反过来呢

Bitmap 是以Image为基类的,如果你已经知道了这个Image其实就是Bitmap,那么强制类型转换一下就可以了:
System.Drawing.Bitmapbmp=(System.Drawing.Bitmap)img;

如果不是,那么就重新生成一下吧。

System.Drawing.Image img; //你要转换的Image对象

//--Begin Copy Image to a BitMap
System.Drawing.Bitmap bmp = new Bitmap(img.Width, img.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.DrawImage(img, 0, 0);
g.Flush();
g.Save();
g.Dispose();
//--Copy OK

//这里的bmp就是你要的Bitmap了