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
Debugging Automatic Properties
Is There a C# Case Insensitive Equals Operator
Cross Platform (PHP to C# .Net) Encryption/Decryption with Rijndael
How to Verify Text Is Bold Using Selenium on an Angular Website with C#
Webbrowser Control IE8 Compatibility Mode On/Off Switch
Signalr(V2.2.0) Ondisconnected Set User Offline
How to Call (Iron)Python Code from a C# App
How Does Creating a Instance of Class Inside of the Class Itself Works
How to Access Winform Textbox Control from Another Class
What Is the Connection String for Localdb for Version 11
Pair-Wise Iteration in C# or Sliding Window Enumerator
Could Not Load File or Assembly System.Runtime.Compilerservices.Unsafe
Drawing Glitches When Using Creategraphics Rather Than Paint Event Handler for Custom Drawing
Sqldataadapter VS SQLdatareader
To Return Iqueryable<T> or Not Return Iqueryable<T>
How to Read an Excel File in C# Without Using Microsoft.Office.Interop.Excel Libraries
What Is an Algorithm to Diff the Two Strings in the Same Way That So Does on the Version Page