How to save System.Windows.Controls.Image to a local drive

How can I save the System.Windows.Controls.Image file to disk in the location: C: \ data \ 1.jpg Thanks

+3
source share
3 answers

Perhaps try something in this direction:

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 may need to combine with the dimensions in the RenderTargetBitmap to get what you want, but this should do the job. You can use different encoders, not just JpegBitmapEncoder.

+3
source

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

0
source

- , :

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.

0
source

Source: https://habr.com/ru/post/1724369/


All Articles