BitmapImage from ByteArray/MemoryStream: Pixel format not supported - c#

I am converting a BitmapImage from byte array, and it works fine, except with one specific .tif file. It gives an exception at EndInit():
The bitmap pixel format is unsupported. (Exception from HRESULT: 0x88982F80)
The code used to read the byte array to BitmapImage is:
public BitmapImage LoadImage(byte[] ImageData)
var image = new BitmapImage();
using (var mem = new MemoryStream(imageData))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit(); //Exception here
}
image.Freeze();
return image;
}
What does this mean? The image opens just fine in windows, so it is not corrupted or anything. Is there a way to convert the image to supported pixel format, before the EndInit?

Related

How can I decode a byte array using a JpegBitmapDecoder

I cannot decode images back from their encoded form as a (Jpeg) byte array retrieved from my database to be used as image sources for my WPF application.
The code I am using to encode them as a Jpeg byte array is as follows:
public byte[] bytesFromBitmap(BitmapImage bit)
{
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bit));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
return data;
}
This is taking my Image taken directly from a webpage and assigned to an Image control like so:
var img = new BitmapImage(new Uri(entity.Image.ImageSrc)); //the entity has been saved in my DB, having been parsed from html
pbImage.Source = img;
This works just fine, I encode the BitmapImage and it saves just fine. But when I retrieve it from the DB and try to display it in another window, I cannot get it to work after trying every example I can see online - all either render nothing, or a black box or a visual mess not at all similar to the image I encoded.
Neither of the following have worked for me:
public BitmapSource GetBMImage(byte[] data)
{
using (var ms = new MemoryStream(data))
{
JpegBitmapDecoder decoder = new JpegBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource frame = decoder.Frames[0];
return frame;
}
}
public static BitmapImage ImageFromBytes(byte[] imageData)
{
if (imageData == null)
{
return null;
}
else
{
var image = new BitmapImage();
using (var mem = new MemoryStream())
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
}
image.Freeze();
return image;
}
} //this throws a 'No imaging component suitable to complete this operation was found' exception
Among other uses of memory streams and decoders I just can't get this to work - can anyone help?

How to create BitmapImage from a bmp file byte array?

In my wpf application I get a byte array of a bmp file.
I want to create a new System.Windows.Media.Imaging.BitmapImage.
I created MemoryStream from the byte array, but it doesn't work with SetSource.
Any suggestions ?
Add reference:
using System.IO;
Use the following code.
MemoryStream ms = new MemoryStream(imageArray);
Image image = Image.FromStream(ms);
For WPF
public static BitmapImage GetBitmapImage(byte[] imageArray)
{
using (var stream = new MemoryStream(imageArray))
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
return bitmapImage;
}
}

Outofmemory exception when do bitmapimage dispose

I have created WPF windows application for display more images using grid. My below code getting OutOfMemory Exception when I run my application.exe.
byte[] buffer = File.ReadAllBytes(path);
File.Delete(path);
if (buffer == null)
return null;
using (MemoryStream mStream = new MemoryStream(buffer))
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = mStream;
bi.EndInit();
bitmap = bi;
bitmap.Freeze();
mStream.Close();
mStream.Dispose();
}
I found some solution from stackoverflow and changed my coding as following below,
BitmapImage image = new BitmapImage();
{
image.BeginInit();
// image.CreateOptions = BitmapCreateOptions.n;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(path);
image.EndInit();
File.Delete(path);
bitmap = image;
image.UriSource = null;
image = null;
}
But this code getting exception as that image used by another process or cant open from locked file.
I am totally confused why my application often caused by OutOfMemory or used by another process exception?
Taken from the comments, you are doing something wrong. You are initializing an obejct ob type BitmapImage and instantly declare it as null. So everything you have declared beforehand has gone down the crapper.
you should leverage the using() statements functionality here. If the code leaves this statement the GarbageCollector will automatically take over and dispose everything for you:
using(BitmapImage image = new BitmapImage())
{
image.BeginInit();
// image.CreateOptions = BitmapCreateOptions.n;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(path);
image.EndInit();
bitmap = image.Clone();
}

Unable to display Image created from Base64 BitmapImage

I want to display an Image in a StackPanel from a base64 string but it's not working correctly.
string base64encodedImage = el.Value;
byte[] imageData = Convert.FromBase64String(base64encodedImage);
Image imageSection = new Image();
BitmapImage image = new BitmapImage();
using (MemoryStream memStream = new MemoryStream(imageData))
{
image.BeginInit();
image.StreamSource = memStream;
image.EndInit();
}
imageSection.Source = image;
panel.Children.Add(imageSection);
Example Base64 Image:
iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==
The BitmapImage gets a width and height, the Image however does not and nothing is displayed, what am I doing wrong?
Refer to this post to find the correct way to get the BitmapImage from byte array.

Fill WPF rectangle with Image

I created new WPF control, added a Rectangle to it, and everyhing works alright, its drawn like it should be. But I just cant paint the rectangle with an actual Image.
BitmapImage bi = GetImage();
ImageBrush imgBrush= new ImageBrush(bi);
this.rectangle.Fill = imgBrush;
But this code just makes the rectangle transparent, except the stroke.
This is the GetImage() method:
BitmapImage bi;
using (MemoryStream ms = new MemoryStream())
{
bi = new BitmapImage();
bi.CacheOption = BitmapCacheOption.OnLoad;
texture.SaveAsPng(ms, texture.Width, texture.Height);
ms.Seek(0, SeekOrigin.Begin);
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
ms.Close();
}
return bi;
texture is an Texture2D class, that is made before this code.
If I return Bitmap insted of BitmapImage here and then save that Bitmap the picture is drawn correctly.
Thank you for your help
This is the correct way to convert Bitmap to BitmapImage:
using(MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
}
Thanks to "Pawel Lesnikowski", he posted the anwser in the following topic:
Load a WPF BitmapImage from a System.Drawing.Bitmap

Categories

Resources