c# - GDI+ 中发生一般性错误

标签 c# bitmap gdi+

我使用以下方法将图像加载到 Picture Box 中:

picturebox1.Image = Image.FromFile()

我使用以下方法保存它:

Bitmap bm = new Bitmap(pictureBox1.Image);
bm.Save(FileName, ImageFormat.Bmp);

创建新文件时它工作得很好,但是当我尝试替换现有图像时,会抛出以下运行时错误:

A generic error occurred in GDI+

那么我该怎么做才能解决这个问题呢??

最佳答案

那是因为你的picturebox1.Image使用了图片文件,尝试将它保存到不同的文件路径:

picturebox1.Image = Image.FromFile(FileName);
Bitmap bm = new Bitmap(pictureBox1.Image); 
bm.Save(@"New File Name", ImageFormat.Bmp);

编辑:您还可以在第一处添加图像的副本,例如:

picturebox1.Image = new Bitmap(Image.FromFile(FileName));
Bitmap bm = new Bitmap(pictureBox1.Image); 
bm.Save(FileName, ImageFormat.Bmp);//no error will occurs here.

关于c# - GDI+ 中发生一般性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7105561/

相关文章:

c# - 呈现时如何从 WebControl 中删除 span 标记

c# - 遍历 char[] 或 substring() : Efficiency in C#?

c# - 检查夏令时是否有效?

c# - 如何安排我的 Quartz 每 10 分钟运行一次,周六和周日除外

c# - 将 TransformedBitmap 对象保存到磁盘。

c# - .NET GDI+ 中的字体大小差异?

c# - 将 BitmapImage 或 IRandomAccessStream 转换为 Windows 10 UAP 中的字节数组

android - 使用 drawBitmap 缩放位图

C# 缩放 GDI 位置但不缩放字体大小或线条粗细

c# - GDI+曲线 "overflowing"