45

I want to implement a image editing program, but I can not display the Bitmap in my WPF. For the general editing I need a Bitmap. But I can not display that in a Image.

private void MenuItemOpen_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog openfiledialog = new OpenFileDialog();

    openfiledialog.Title = "Open Image";
    openfiledialog.Filter = "Image File|*.bmp; *.gif; *.jpg; *.jpeg; *.png;";

    if (openfiledialog.ShowDialog() == true)
    {
        image = new Bitmap(openfiledialog.FileName);
    }
}

I load the Image with a OpenFileDialog into the Bitmap. Now I want to set the picture in my WPF. Like so:

Image.Source = image;

I really need a Bitmap to get the color of a special pixel! I need a simple code snipped.

Thank you for your help!

Gerret
  • 2,948
  • 4
  • 18
  • 28
  • 2
    If you want to keep `System.Drawing.Bitamp` instead of using [`System.Windows.Media.Imaging.BitmapImage`](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage(v=vs.110).aspx) then check question: http://stackoverflow.com/questions/94456/load-a-wpf-bitmapimage-from-a-system-drawing-bitmap – dkozl Mar 19 '14 at 07:56

2 Answers2

107

I have used this snipped now to convert the Bitmap to a ImageSource:

BitmapImage BitmapToImageSource(Bitmap bitmap)
{
    using (MemoryStream memory = new MemoryStream())
    {
        bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
        memory.Position = 0;
        BitmapImage bitmapimage = new BitmapImage();
        bitmapimage.BeginInit();
        bitmapimage.StreamSource = memory;
        bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapimage.EndInit();

        return bitmapimage;
    }
}
Gerret
  • 2,948
  • 4
  • 18
  • 28
-1

This should do:

ImageSource imgSource = new BitmapImage(new Uri("HERE GOES YOUR URI"));

image1.Source = imgSource; //image1 is your control

If you need the Bitmap class try using this:

 public ImageSource imageSourceForImageControl(Bitmap yourBitmap)
{
 ImageSourceConverter c = new ImageSourceConverter();
 return (ImageSource)c.ConvertFrom(yourBitmap);
}
Saverio Terracciano
  • 3,885
  • 1
  • 30
  • 42