curiousity curiousity - 4 years ago 207
C# Question

How to save BitmapImage via SaveFileDialog from WPF?

The way I try implement it via standart Windows.Forms (to get valide DialogResult.OK)

System.Windows.Forms.SaveFileDialog dlg = new System.Windows.Forms.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
// dlg.DefaultExt = ".jpg"; // Default file extension
dlg.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
if(dlg.ShowDialog()== System.Windows.Forms.DialogResult.OK)
if (dlg.DialogResult.HasValue && splashDialog.DialogResult.Value)
string fName = dlg.FileName;
if (dlg.FileName != "")
System.IO.Stream fileStream = (System.IO.FileStream)dlg.OpenFile();


This is using Windows forms but it saves blank image((

Answer Source

You can do something like that:

var encoder = new JpegBitmapEncoder(); // Or PngBitmapEncoder, or whichever encoder you want
using (var stream = dlg.OpenFile())

BTW, there is a SaveFileDialog in WPF too, you don't have to use the one from Windows Forms

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download