将一个大图片 截取一小部分 然后报错 GDI+ 中发生一般性错误 怎么解决?
dicpath += Name;
if (File.Exists(dicpath))
{
//原图片文件
System.Drawing.Image fromImage = System.Drawing.Image.FromFile(dicpath);
//创建新图位图
Bitmap bitmap = new Bitmap(55, 50);
//创建作图区域
Graphics graphic = Graphics.FromImage(bitmap);
//截取原图相应区域写入作图区
graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, 150, 150), GraphicsUnit.Pixel);
//从作图区生成新图
Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
//保存图片
string[] arr = dicpath.Split('.');
string name = arr[0] + "_Small." + arr[1];
saveImage.Save(name);
//释放资源
graphic.Dispose();
bitmap.Dispose();
fromImage.Dispose();
}
dicpath += Name;
if (File.Exists(dicpath))
{
//原图片文件
System.Drawing.Image fromImage = System.Drawing.Image.FromFile(dicpath);
//创建新图位图
Bitmap bitmap = new Bitmap(55, 50);
//创建作图区域
Graphics graphic = Graphics.FromImage(bitmap);
//截取原图相应区域写入作图区
graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, 150, 150), GraphicsUnit.Pixel);
//从作图区生成新图
Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
//保存图片
string[] arr = dicpath.Split('.');
string name = arr[0] + "_Small." + arr[1];
saveImage.Save(name);
//释放资源
graphic.Dispose();
bitmap.Dispose();
fromImage.Dispose();
}
多半是不能识别原始图片另外,作图是针对 bitmap 进行的,所以直接保存 bitmap 就行了,不必(Image.FromHbitmap)绕个圈子,因为 Bitmap 自己就有 Save 方法