首页 新闻 会员 周边 捐助

C# 图片格式更改后保存时 提示 “GDI+中发生一般性错误”

0
悬赏园豆:20 [已解决问题] 解决于 2012-09-12 14:46

private void button1_Click(object sender, EventArgs e)
{
string path = "";
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开";
ofd.Filter = "所有文件|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
path = ofd.FileName;
}
if (!File.Exists(path))
{
MessageBox.Show("文件不存在!");
}
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(path);
bm.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);//把jpg转成bmp
bm.Dispose();
}

当运行到save 方法是 总是报 “GDI+中发生一般性错误” 首先物理路径 没问题 

调试的时候读取权限也没问题啊 

哪位帮忙看看 在线等待!。。。。

Smile_Xu的主页 Smile_Xu | 初学一级 | 园豆:2
提问于:2012-09-11 11:51
< >
分享
最佳答案
0

 string path = "";
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "打开";
            ofd.Filter = "所有文件|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path = ofd.FileName;
            }
            if (!File.Exists(path))
            {
                MessageBox.Show("文件不存在!");
                return;
            }
            Image img = Image.FromFile(path);
            System.Drawing.Bitmap bm = new System.Drawing.Bitmap(img.Width,img.Height);
            Graphics g = Graphics.FromImage(bm);
            g.Clear(Color.White);
            g.DrawImage(img, 0, 0);
            img.Dispose();
            File.Delete(path);
            path = path.Substring(0,path.IndexOf(".")-1) + ".bmp";
            bm.Save(path,System.Drawing.Imaging.ImageFormat.Bmp);//把jpg转成bmp
            g.Dispose();
            bm.Dispose();

收获园豆:10
程序员的人生 | 菜鸟二级 |园豆:235 | 2012-09-11 17:16
其他回答(1)
0

bm.Save时path还是原来那张图片,bm还没释放,自己去替换自己就报错了。

System.Drawing.Bitmap bm = new System.Drawing.Bitmap(path);
            string newName = path.Replace(".jpg", ".bmp");
            bm.Save(newName, System.Drawing.Imaging.ImageFormat.Bmp);//把jpg转成bmp 
            bm.Dispose();
            File.Replace(path, newName, path + "_bak");
收获园豆:10
向往-SONG | 园豆:4853 (老鸟四级) | 2012-09-11 13:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册