问题在使用savefiledialog保存图像 [英] Issue while saving image using savefiledialog

查看:131
本文介绍了问题在使用savefiledialog保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用savefiledialog保存图像。 Canvas是图片框和加载的图像是位图。当我尝试将它保存在文件被创建,但不知何故损坏。因为当我尝试againt加载在不同浏览器中的图像或显示这是行不通的 - 我指的是保存的文件已损坏。有一个用于保存图像的方法。

 私人无效saveFileDialog1_FileOk(对象发件人,发送CancelEventArgs E)
{

System.IO.FileStream FS =
(System.IO.FileStream)saveFileDialog1.OpenFile();


{
开关(saveFileDialog1.FilterIndex)
{
案例1:
canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
中断;
案例2:
canvas.Image.Save(saveFileDialog1.FileName,System.Drawing.Imaging.ImageFormat.Jpeg);
中断;
案例3:
canvas.Image.Save(saveFileDialog1.FileName,System.Drawing.Imaging.ImageFormat.Png);
中断;
案例4:
canvas.Image.Save(saveFileDialog1.FileName,System.Drawing.Imaging.ImageFormat.Tiff);
中断;
}

}
赶上(异常前)
{
的System.Console.WriteLine(异常+ EX);
}



我应该也不在话下属性过滤器。 saveFileDialog1.Filter有值:

  BMP(* .BMP)| * .BMP | JPEG(* .JPEG)| *。 JPEG | PNG格式(* .png)| *。PNG | TIFF(* .TIFF)| * .TIFF 


解决方案

我要问,为什么你行

  System.IO.FileStream FS = 
(System.IO.FileStream)saveFileDialog1.OpenFile();



但事实证明,这正是导致你的问题就行了。要打开的文件到的FileStream 。虽然它是开放的,你可以使用 canvas.Image.Save 将图像写入到同一个文件。



它抛出一个异常,但因为你只写例外控制台你看不到它。



只是删除​​我提到的线,你的代码将工作。


I'm using savefiledialog to save an image. Canvas is picturebox and the loaded image is bitmap. When I try to save it the file is created but somehow corrupted. Cause when I try againt load the image or show in different viewer it doesn't work - I mean the saved file is corrupted. There is an method for saving image.

 private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

           System.IO.FileStream fs =
                (System.IO.FileStream)saveFileDialog1.OpenFile();

           try
           {
               switch (saveFileDialog1.FilterIndex)
               {
                   case 1:
                       canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
                       break;
                   case 2:
                       canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                       break;
                   case 3:
                       canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
                       break;
                   case 4:
                       canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Tiff);
                       break;
               }

           }
           catch (Exception ex) 
           {
               System.Console.WriteLine("Exception " + ex);
           }

I should also mention the property Filter. saveFileDialog1.Filter has value:

bmp (*.bmp)|*.bmp|jpeg (*.jpeg)|*.jpeg|png (*.png)|*.png|tiff (*.tiff)|*.tiff

解决方案

I was gonna ask why you have the line

System.IO.FileStream fs =
    (System.IO.FileStream)saveFileDialog1.OpenFile();

But as it turns out, that's exactly the line causing your problems. You are opening the file to a FileStream. While it's open, you use canvas.Image.Save to write the image to that same file.

It throws an exception, but since you just write the exception to the console you don't see it.

Just remove the line I mentioned and your code will work.

这篇关于问题在使用savefiledialog保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆