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+中发生一般性错误” 首先物理路径 没问题
调试的时候读取权限也没问题啊
哪位帮忙看看 在线等待!。。。。
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();
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");