Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello...

I have store data in Bitmap or Byte. How to display image using this Bitmap or Byte.??
I have Use this function.

private System.Drawing.Bitmap ImageToBitmap(byte[] colorData, int imageWidth, int imageHeight, int imagePixelDataLength)
{
byte[] pixeldata = colorData;
System.Drawing.Bitmap bmap = new System.Drawing.Bitmap(imageWidth, imageHeight, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
System.Drawing.Imaging.BitmapData bmapdata = bmap.LockBits(
new System.Drawing.Rectangle(0, 0, imageWidth, imageHeight),
System.Drawing.Imaging.ImageLockMode.WriteOnly,
bmap.PixelFormat);
IntPtr ptr = bmapdata.Scan0;

byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
bmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Close();
byteArray = stream.ToArray();
// Display Image Here or Other
}
//img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

Marshal.Copy(pixeldata, 0, ptr, imagePixelDataLength);
bmap.UnlockBits(bmapdata);
return bmap;
}
Posted
Comments
Sergey Alexandrovich Kryukov 8-Jan-15 12:01pm    
Why would you do all that?
—SA
Christian Amado 8-Jan-15 12:06pm    
A really good question from you, Sergey.
Sergey Alexandrovich Kryukov 8-Jan-15 12:12pm    
Thank you. Please see my other comments and Solution 2.
—SA

Why would you use System.Drawing.Bitmap image in WPF? Maybe, according to the title of your question, you really need "to display image in WPF"? But then it does not have to be System.Drawing.Bitmap. WPF has its own image types. Look at this class hierarchy:
http://msdn.microsoft.com/en-us/library/system.windows.media.imagesource%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource%28v=vs.110%29.aspx#inheritanceContinued[^].

Sometimes really have System.Drawing.Bitmap and need to convert it to show in WPF, which makes sense in certain cases. But you are not doing that. From your function, you are trying to the instance of System.Drawing.Bitmap (which is useless for using in WPF) and save it to file. Not clear why. (And this is the way to mislead the function users, to have some side effect, saving a file in your case, instead of just returning a value.)
For interoperability between System.Drawing and WPF, please see:
http://msdn.microsoft.com/en-us/library/system.windows.interop.imaging%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.interop.interopbitmap%28v=vs.110%29.aspx[^].

I'm not discussing any detail on this topic, just because this is hardly what you are trying to do. You are trying to utilize some data, colorData. You absolutely don't need to write it in System.Drawing.Bitmap. You need to write it to some object which you could utilize in WPF, which is a very different thing.
First of all, I don't know how your data is organized. It's up to you to sort it out and use appropriate pixel representation. You can use the class System.Windows.Media.Imaging.WriteableBitmap.
Please read the documentation: http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
You can display image from this way:
C#
ImageSource imageSource = new BitmapImage(new Uri("C:\\FileName.gif"));

image1.Source = imageSource;

Another way:
C#
Bitmap bitmap = YourProject.Properties.Resources.YourImage;
object source = new ImageSourceConverter().ConvertFrom(bitmap.GetHbitmap());
ImageSource imageSource = (ImageSource) source;
image1.Icon = imageSource;

Or this way[^] (From .resx file)

There are many examples on the net, just search it.

Hope it helps.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Jan-15 12:10pm    
This is all good, but this is not what OP actually wants to do. I also was mislead by that code using System.Drawing.Bitmap. But if you consider the question title with ins and outs, you will understand that the problem is unrelated to System.Drawing and conversion (which is totally useless) and is reduced to writing data to WPF bitmap.

Please see Solution 2. The solution itself is only in the last paragraph; two first paragraphs just discuss the confusion... :-)

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900