Click here to Skip to main content
15,569,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi good morning

in wpf and using C#.net basically i had the imagestream, and also the bitmap , i need to know that how to assign the bitmap to image control (or) directly from imagestream to image control


i had some sample code like
  byte[] ImageInBytes = MyWebClient.DownloadData(a.ToString());
                //   'CREATE A MEMORY STREAM USING THE BYTES           
                System.IO.MemoryStream ImageStream = new System.IO.MemoryStream(ImageInBytes);
                //    'CREATE A BITMAP FROM THE MEMORY STREAM
System.Drawing.Bitmap megatron = new System.Drawing.Bitmap(ImageStream);
Posted
Comments
Mark Salsbery 14-May-11 3:55am    
What format is the data in the stream?

Image.Source is a System.Windows.Media.ImageSource so you need to decode your stream into an ImageSource-derived class object. Once you have that source you can assign it to the Image.Source property.

A couple examples...

// Use a specific decoder

PngBitmapDecoder decoder = new PngBitmapDecoder(ImageStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
BitmapSource source = decoder.Frames[0];

image.Source = source;

// Let the framework pick a decoder

BitmapFrame source = BitmapFrame.Create(ImageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);

image.Source = source;


And get rid of the System.Drawing.Bitmap...that's not WPF :)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-May-11 17:45pm    
I wanted to up-vote it because I disagree with the vote of 3.
"get rid of the System.Drawing.Bitmap" is not a bad advice, but there are cases when one needs it.

The method of using System.Drawing.Bitmap in WPF supported by WPF itself does exist.
Please see my solution.
--SA
Mark Salsbery 14-May-11 18:55pm    
Of course one CAN use System.Drawing.Bitmap. My comment was specific to the OP, based on the code shown in the OP and also the "...(or) directly from imagestream to image control".
Sergey Alexandrovich Kryukov 15-May-11 14:25pm    
Well, that's why I up-voted your solution with my 5...
--SA
Mark Salsbery 15-May-11 14:34pm    
Appreciated thank you :)
Tarun.K.S 15-May-11 14:11pm    
Excellent answer. Pure WPF stuff! 5+
The is a way to convert to convert System.Drawing.Bitmap into System.Windows.Media.Imaging.BitmapSource.

Do the following:

C#
using System;
using System.Windows;
using BitmapSource = System.Windows.Media.Imaging.BitmapSource;
using InteropImaging = System.Windows.Interop.Imaging;
using System.Drawing.Imaging;

//...

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);

public static BitmapSource CreateBitmapSourceFromBitmap(System.Drawing.Bitmap bitmap) {
    IntPtr hBitmap = bitmap.GetHbitmap();
    try {
        return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
            hBitmap,
            IntPtr.Zero,
            Int32Rect.Empty,
            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
    } finally {
        DeleteObject(hBitmap);
    } //exception
} //CreateBitmapSourceFromBitmap


Another way it to create a memory stream, write a bitmap to it and then read from it. You convert by writing using one system and reading by another one. As System.Windows.Interop.Imaging already does the trick you may not need it.

(In principle, you can ignore the problem with deletion of the intermediate bitmap and the resource leak to avoid using P/Invoke. If you do it just fixed number of times (say, once or twice) you can ignore the leak. Be careful.)

—SA
 
Share this answer
 
v2
Comments
Tarun.K.S 15-May-11 14:12pm    
Using Bitmap works but WPF has its own way. 5+
Sergey Alexandrovich Kryukov 15-May-11 14:23pm    
Thank you, Tarun.
Yes, of course, but why Microsoft provided us with System.Windows.Interop.Imaging? Because there are cases when it can be needed.
--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