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;
}
}
Related
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?
Hi Developer,
I have an issue to display a bitmap image in my System.Windows.Controls.Image,
Am I using Pixelformat correctly?
I am able to display the image, but it shows me just a black rectangle.
I am trying to get the image from the Scanner.
Thank you very much in advance.
Bitmap b = new Bitmap(displayW, displayH, image.PixelFormat);
VCardScan.MainWindow.AppWindow.setImageSource(BitmapToImageSource(b));
public void setImageSource(BitmapImage bitmap)
{
CardBox.Source = bitmap;
}
BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
System.Console.WriteLine(bitmap.ToString());
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}
I am trying to append image to image source but after executing the code image is not displaying in my page.
Code:
Bitmap bmp = (Bitmap)data.img;
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
imgPhoto.Source = bi;
this is data.img specifications that I want to append to imhPhoto.Source.
After working around this issue, the solution for this question is:
call the function to get BitmapImage and save it in photo variable like this:
BitmapImage photo = byteToImage(byte[] buffer)
this functionto convert byte to BitmapImage
public BitmapImage byteToImage(byte[] buffer)
{
using(var ms = new MemoryStream(buffer))
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
}
return image;
}
finally, append converted photo to image source like this:
imgPhoto.Source = photo;
You can assign path like this.
//for App folder path
//var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)+"\\Image\\pic.jpg";
//for machine path
var path = #"C:\Users\User1\Pictures\pic.jpg";
Bitmap bmp = new Bitmap(path);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
imgPhoto.Source = bi;
If this solves your problem :
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(#"g:\screenshot.png");
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
bi.StreamSource = ms;
bi.EndInit();
imgPhoto.Source = bi;
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?
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