Using Image Control in Wpf to Display System.Drawing.Bitmap

Using Image control in WPF to display System.Drawing.Bitmap

You can use the Source property of the image. Try this code...

ImageSource imageSource = new BitmapImage(new Uri("C:\\FileName.gif"));

image1.Source = imageSource;

How to Display a Bitmap in a WPF Image

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;
}
}

Convert drawing.bitmap to windows.controls.image

Do not create a System.Drawing.Bitmap from that file. Bitmap is WinForms, not WPF.

Instead, create a WPF BitmapImage

public ImageSource GetPhotoFile()
{
var photoFile = GetFile("photo_file");
var photo = new BitmapImage();

using (var stream = new MemoryStream(photoFile))
{
photo.BeginInit();
photo.CacheOption = BitmapCacheOption.OnLoad;
photo.StreamSource = stream;
photo.EndInit();
}

return photo;
}

Then assign the returned ImageSource to the Source property of the Image control:

imgphoto.Source = ReadData.GetPhotoFile();

display a System.Drawing.Bitmap object in a System.Windows.Controls.Image control

You need to convert it to an ImageSource of some kind first. See this question.

You can use a ValueConverter to do this on the fly in the binding itself.

Load a WPF BitmapImage from a System.Drawing.Bitmap

Thanks to Hallgrim, here is the code I ended up with:

ScreenCapture = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(),
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(width, height));

I also ended up binding to a BitmapSource instead of a BitmapImage as in my original question

using XAML to bind to a System.Drawing.Image into a System.Windows.Image control

Found a way I'm happy with. Using Reed Copsey's pointer and this tutorial I've wrapped the code as a IValueConverter.

Here's the converter from System.Drawing.Image to System.Windows.Media.ImageSource;

using System;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Windows.Data;

namespace System.Windows.Media
{
/// <summary>
/// One-way converter from System.Drawing.Image to System.Windows.Media.ImageSource
/// </summary>
[ValueConversion(typeof(System.Drawing.Image), typeof(System.Windows.Media.ImageSource))]
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
// empty images are empty...
if (value == null) { return null; }

var image = (System.Drawing.Image)value;
// Winforms Image we want to get the WPF Image from...
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
bitmap.BeginInit();
MemoryStream memoryStream = new MemoryStream();
// Save to a memory stream...
image.Save(memoryStream, ImageFormat.Bmp);
// Rewind the stream...
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
bitmap.StreamSource = memoryStream;
bitmap.EndInit();
return bitmap;
}

public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return null;
}
}
}

Then you need to bring the image converter into XAML as a resource;

xmlns:med="clr-namespace:System.Windows.Media"
...

<ListView.Resources>
<med:ImageConverter x:Key="imageConverter" />
</ListView.Resources>

Then you can use it in XAML to bind directly to the Image, using the new converter;

<Image Source="{ Binding Path=Image, Converter={StaticResource imageConverter} }" />


Related Topics



Leave a reply



Submit