关于C#保存图片格式问题

private void 保存toolStripButton_Click(object sender, EventArgs e)
{
DialogResult result;
string filename;
SaveFileDialog save = new SaveFileDialog();
save.CheckFileExists = false;
save.Filter = "jpg文件(*.jpg)|*.jpg|jpeg文件(*.jpeg)|*.jpeg|bmp文件(*.bmp)|*.bmp|gif文件(*.gif)|*.gif|ico文件(*.ico)|*.ico|png文件(*.png)|*.png|tif(文件)(*.tif)|*.tif|wmf文件(*.wmf)|*.wmf";
result = save.ShowDialog();
filename = save.FileName;
if (result == DialogResult.OK)
{
if (filename == string.Empty)
{
MessageBox.Show("Invaild file name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
string path = save.FileName;
this.pictureBox1.Image.Save(path, ImageFormat.Bmp);
}
}
}

我想一次可选择多个格式保存,
this.pictureBox1.Image.Save(path, ImageFormat.Bmp);
对于这一句的bmp对这个功能有什么影响?

这个就真实决定了你保存出来的是什么格式的图片,即使你上面选择的文件名是比如1.jpg,但用上面的代码保存出来的仍然是一个bmp图(或者说保存的是一个扩展名为jpg的bmp格式的图片),这种文件扩展名与实际格式不符的情况,虽然可以在网页中或者acdsee之类的软件中查看,但是如果用photoshop之类的打开就会报错。追问

请问怎么才能真正的保存多种格式呢?

追答

差不多就是这样,自己补全格式

else
{
string fileExt=System.IO.Path.GetExtension(filename).ToLower();
ImageFormat format = ImageFormat.Png;                 
                    switch (fileExt.ToLower())
                    {
                        case ".png":
                            format = ImageFormat.Png;
                            break;
                        case ".bmp":
                            format = ImageFormat.Bmp;
                            break;
                        case ".gif":
                            format = ImageFormat.Gif;
                            break;
                     }
this.pictureBox1.Image.Save(filename, format);
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-21
没有,你想保存多次就写多句该语句就好了,比如你想同时再保存一份png文件,就这个改语句下面添加this.pictureBox1.Image.Save(path, ImageFormat.Png);
第2个回答  2013-05-21
要做类型转换
相似回答