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();

fileStream.Close();
}
}


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
encoder.Frames.Add(BitmapFrame.Create(yourImage));
using (var stream = dlg.OpenFile())
{
    encoder.Save(stream);
}

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