Bitmap to BitmapImage Conversion Not Working - WPF
-
Hello. I am using following code to convert bitmap to BitmapImage, in order to show on
System.Windows.Control.Image
control.public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap) { BitmapImage bitmapImage = null; using (var memory = new System.IO.MemoryStream()) { bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp); memory.Position = 0; bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.EndInit(); return bitmapImage; } } public void ShowPicture(Bitmap bmp) { pictureBox.Source = ConvertBitmapToBitmapImage(bmp); return; }
It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine. What is wrong with my code. Thanks for any input.
This world is going to explode due to international politics, SOON.
-
Hello. I am using following code to convert bitmap to BitmapImage, in order to show on
System.Windows.Control.Image
control.public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap) { BitmapImage bitmapImage = null; using (var memory = new System.IO.MemoryStream()) { bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp); memory.Position = 0; bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.EndInit(); return bitmapImage; } } public void ShowPicture(Bitmap bmp) { pictureBox.Source = ConvertBitmapToBitmapImage(bmp); return; }
It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine. What is wrong with my code. Thanks for any input.
This world is going to explode due to international politics, SOON.
I don't know the answer to this, but Googling suggests others might: c# - loading the source of a BitmapImage in WPF? - Stack Overflow[^] Essentially, there's a suggestion that StreamSource is only accessed when needed by which time your memory stream will have been disposed. The suggestion is to set CacheOption or something. I'd so some Googling!
Regards, Rob Philpott.
-
I don't know the answer to this, but Googling suggests others might: c# - loading the source of a BitmapImage in WPF? - Stack Overflow[^] Essentially, there's a suggestion that StreamSource is only accessed when needed by which time your memory stream will have been disposed. The suggestion is to set CacheOption or something. I'd so some Googling!
Regards, Rob Philpott.
I did google. I know it is some small mistake that I am not seeing. But I did google alot. Thanks again.
This world is going to explode due to international politics, SOON.
-
Hello. I am using following code to convert bitmap to BitmapImage, in order to show on
System.Windows.Control.Image
control.public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap) { BitmapImage bitmapImage = null; using (var memory = new System.IO.MemoryStream()) { bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp); memory.Position = 0; bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.EndInit(); return bitmapImage; } } public void ShowPicture(Bitmap bmp) { pictureBox.Source = ConvertBitmapToBitmapImage(bmp); return; }
It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine. What is wrong with my code. Thanks for any input.
This world is going to explode due to international politics, SOON.
Here's the converter I wrote years ago. It converts a
Bitmap
toImageSource
(specifically aBitmapFrame
):using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;namespace ResourcesLibrary
{
[ValueConversion(typeof(Bitmap), typeof(ImageSource))]
public class BitmapToImageSourceConverter : IValueConverter
{
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Bitmap bmp = value as Bitmap;
if (bmp == null)
return null;
using (Stream str = new MemoryStream())
{
bmp.Save(str, System.Drawing.Imaging.ImageFormat.Bmp);
str.Seek(0, SeekOrigin.Begin);
BitmapDecoder bdc = new BmpBitmapDecoder(str, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
return bdc.Frames[0];
}
}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); }
}
}"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton
-
Hello. I am using following code to convert bitmap to BitmapImage, in order to show on
System.Windows.Control.Image
control.public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap) { BitmapImage bitmapImage = null; using (var memory = new System.IO.MemoryStream()) { bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp); memory.Position = 0; bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.EndInit(); return bitmapImage; } } public void ShowPicture(Bitmap bmp) { pictureBox.Source = ConvertBitmapToBitmapImage(bmp); return; }
It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine. What is wrong with my code. Thanks for any input.
This world is going to explode due to international politics, SOON.
Compared to my working code:
bitmapimage.BeginInit(); bitmapimage.StreamSource = memory; **bitmapimage.CacheOption = BitmapCacheOption.OnLoad;** bitmapimage.EndInit();
and
window.Content = imageControl;
window.UpdateLayout();
window.ShowDialog();Don't know enough about your app ... you may be able to apply .UpdateLayout() somewhere else in the visual tree.
-
Hello. I am using following code to convert bitmap to BitmapImage, in order to show on
System.Windows.Control.Image
control.public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap) { BitmapImage bitmapImage = null; using (var memory = new System.IO.MemoryStream()) { bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp); memory.Position = 0; bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.EndInit(); return bitmapImage; } } public void ShowPicture(Bitmap bmp) { pictureBox.Source = ConvertBitmapToBitmapImage(bmp); return; }
It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine. What is wrong with my code. Thanks for any input.
This world is going to explode due to international politics, SOON.
this should work:
/// /// makes the image uniform between operations, size wise and concerning pixelformat.
///
///
private BitmapImage SetModifiedImage(System.Drawing.Bitmap bmp){
bmp = CreateNonIndexedImage(bmp);
BitmapImage bmpi = new BitmapImage();
bmpi.BeginInit();
bmpi.StreamSource = new System.IO.MemoryStream((byte[])(new System.Drawing.ImageConverter()).ConvertTo(bmp, typeof(byte[])));
bmpi.EndInit();
return bmpi;
}/// /// Removes the index color table of the image and makes it uniform.
///
///
///
private System.Drawing.Bitmap CreateNonIndexedImage(System.Drawing.Bitmap bmp){
System.Drawing.Bitmap newbmp = new System.Drawing.Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newbmp);
g.DrawImage(bmp, 0, 0);
return newbmp;
}You don't need the "CreateNonIndexedImage" normally. I had different sets of input and the difference in the PixelFormat gave me problems when working on the bytes of the bitmap. Hope this helps.
V.
(MQOTD rules and previous solutions)