How to store System.Windows.Controls.Image to local disk

c#, image, wpf

Solution

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.

Problem

How can i store a System.Windows.Controls.Image to disk say at location: C:\data\1.jpg Thanks

Original source

Related problems