How can i store a System.Windows.Controls.Image to disk say at location: C:\data\1.jpg Thanks
Asked
Active
Viewed 7,553 times
3 Answers
4
Maybe try something along the lines of this method:
private void SaveImageToJPEG(Image ImageToSave, string Location)
{
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)ImageToSave.Source.Width,
(int)ImageToSave.Source.Height,
100, 100, PixelFormats.Default);
renderTargetBitmap.Render(ImageToSave);
JpegBitmapEncoder jpegBitmapEncoder = new JpegBitmapEncoder();
jpegBitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
using (FileStream fileStream = new FileStream(Location, FileMode.Create))
{
jpegBitmapEncoder.Save(fileStream);
fileStream.Flush();
fileStream.Close();
}
}
You might need to mess around with the sizes in RenderTargetBitmap to get what you want, but this should get the job done. You can use different encoders than just JpegBitmapEncoder too.

Jon Comtois
- 1,824
- 1
- 22
- 29
-
2[You don't need to call close as dispose will do it for you.](http://stackoverflow.com/q/911408/299327) – Ryan Gates Apr 12 '13 at 20:34
0
Question is still unanswered so I'll paraphrase the example previously provided:
public System.Drawing.Image ConvertControlsImageToDrawingImage(System.Windows.Controls.Image imageControl)
{
RenderTargetBitmap rtb2 = new RenderTargetBitmap((int)imageControl.Width, (int)imageControl.Height, 90, 90, PixelFormats.Default);
rtb2.Render(imageControl);
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb2));
Stream ms = new MemoryStream();
png.Save(ms);
ms.Position = 0;
System.Drawing.Image retImg = System.Drawing.Image.FromStream(ms);
return retImg;
}
From there you can use one of the Save() methods provided by the System.Drawing.Image class.

Cory Charlton
- 8,868
- 4
- 48
- 68
-1
German but there is code to convert an FrameworkElement to an System.Drawing.Image which can be easily saved. Link

Sebastian
- 821
- 4
- 8
-
Great general-purpose solution, but maybe it's easier to get the image out of an Image control, rather than rendering the entire control? :) Feels like too much :) – OregonGhost Dec 01 '09 at 09:38