tags:

views:

149

answers:

3

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

A: 

German but there is code to convert an FrameworkElement to an System.Drawing.Image which can be easily saved. Link

Sebastian Sedlak
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
A: 

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.

jomtois
A: 

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