2

Basically, I have a program that save image from my local computer. I preview this image as a big sized. I have Save As button on this preview popup. My problem is, Image saved as byte[], how can I save byte[] as a png file using SaveFileDialog?

Thanks,

The problem solved like that;

var saveFileDialog = new SaveFileDialog
                                 {
                                     DefaultExt = ".png",
                                     DefaultFileName = this.ViewDetailSource.ManagedEntityName + " Image",
                                     FilterIndex = 1,
                                     Filter = "All Files|*.*"
                                 };

        bool? result = saveFileDialog.ShowDialog();

        if (result.HasValue && result.Value)
        {
            using (var filestream = saveFileDialog.OpenFile())
            {
                filestream.Write(this.imagecapableentity.EntityImage, 0, this.imagecapableentity.EntityImage.Length);
            }
        }

EntityImage is byte[]

Thanks all,

mkacar
  • 277
  • 1
  • 7
  • 17
  • The `SaveFileDialog` part is irrelevant. If you call `SaveFileDialog.OpenFile()`, you get a stream to the file, which you can also manually create from code. Your real problem is: what data is in your `byte[]` array? The `Bitmap` class contains a few properties, methods and constructors [that can help you](http://msdn.microsoft.com/en-us/library/zy1a2d14.aspx). You can then save the Bitmap in any format you like. What part are you actually having trouble with? Can you show your current code? – CodeCaster Oct 04 '13 at 15:06
  • I have to use SaveFileDialog in order to "Save As" operation. Main problem was that after opening file "saveFileDialog.OpenFile", I saved the image but I couldn't open saved file from my local drive. I encounter the error like "Its corrupted...". Stream.Write solved my problem. With this way, saved image file opens correctly. Thanks – mkacar Oct 07 '13 at 07:25

5 Answers5

4

The SaveFileDialog is not used to actually save a file, but to gather metainformation on the process of creating a file. Namely, it will help you collect the file path and whether or not the user cancelled the save process or not.

Once you have collected the file path and know that the user is OK with saving the file, you'll have to actually use the File class to create a file: File.WriteAllBytes(...) is a good way of doing it.

myermian
  • 31,823
  • 24
  • 123
  • 215
2

Firstly using SaveFileDialog: https://stackoverflow.com/a/5136341/787828

Then byte[] convert to Image and save: https://stackoverflow.com/a/8946937/787828

Community
  • 1
  • 1
nirmus
  • 4,913
  • 9
  • 31
  • 50
2

just create a FileStream instance with the file name provided by SaveFileDialog, and call its method Write, to write the byte[]

Evgeny Gorb
  • 1,442
  • 2
  • 13
  • 24
0

The SaveFileDialog is only for getting the name under which the user wants to save the file, not for the actual saving. You can use this instead:

System.IO.File.WriteAllBytes(savedialog.FileName, bytearray);

where savedialog is you SaveDialog and bytearray is your image.

Raidri
  • 17,258
  • 9
  • 62
  • 65
-1

You can do it using static void System.IO.File.WriteAllBytes(string path, byte[] bytes)

byte[] buffer = ... ;
File.WriteAllBytes(@"c:\*.png", buffer);
Harry
  • 87,580
  • 25
  • 202
  • 214