0

I want to render a cropped image on a Media.DrawingContext. I thought of the DrawingContext.DrawDrawing(Media.Drawing) method, because as a Media.Drawing I would pass Media.Drawing.ImageDrawing because its constructor ImageDrawing(Media.ImageSource, Rect) permits to select a part of the ImageSource.

My original image is loaded in a Drawing.Image (can be loaded from another class if needed). So I want to convert my Drawing.Image to a Media.ImageSource.

I researched about derived classes of Media.ImageSource, but I found nothing in both Media.DrawingImage and Media.Imaging.BitmapSource.

Thanks for any answer !

aybe
  • 15,516
  • 9
  • 57
  • 105
Julien Vernay
  • 295
  • 3
  • 13

1 Answers1

4

This is more like a hack than a direct conversion: but it should give the equivalent result:

Basically:

  • Save the image to a stream
  • Rewind the stream
  • Tell the Wpf image to use the stream as its stream source

Here is a class that does that:

using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace StackCsWpf
{
    public class ImageUtils
    {
        public static ImageSource ToImageSource(System.Drawing.Image image, ImageFormat imageFormat)
        {
            BitmapImage bitmap = new BitmapImage();

            using (MemoryStream stream = new MemoryStream())
            {
                // Save to the stream
                image.Save(stream, imageFormat);

                // Rewind the stream
                stream.Seek(0, SeekOrigin.Begin);

                // Tell the WPF BitmapImage to use this stream
                bitmap.BeginInit();
                bitmap.StreamSource = stream;
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.EndInit();
            }

            return bitmap;
        }
    }
}

Now as an illustration I can use the method above to display the image in a Wpf Image component.

/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        System.Drawing.Image winFormImg = System.Drawing.Image.FromFile("leaves.jpg");
        Image1.Source = ImageUtils.ToImageSource(winFormImg, ImageFormat.Jpeg);
    }
}

Image1 is a simple Wpf Image component that I dragged from the toolbox into my Wpf App main window's grid.

It renders nicely:

image conversion

Ref: Msdn forums

Clemens
  • 123,504
  • 12
  • 155
  • 268
alainlompo
  • 4,414
  • 4
  • 32
  • 41
  • It doesn't make much sense to use JPEG as intermediate format. If you need transparency, use PNG, otherwise BMP because it has no compression and is lossless. Besides that, if you are actually loading bitmaps from files, it makes no sense at all to first create a System.Drawing.Image. You could directly create a BitmapImage from a file URI or a FileStream. – Clemens Mar 06 '18 at 08:18
  • @Clemens, thank you. Good point. The format is passed as parameter to the method. We can use any other format, I just used it as illustration. I understand that the OPs input format was a Winform Image and we wanted to get an Wpf ImageSource at the end. – alainlompo Mar 06 '18 at 09:48