0

Im trying to set the Source of the Image Control, im getting the Bitmap from an Event every 2 seconds . What ive tried first is setting the Source without binding.

public void FiletransferImage(string _id, Bitmap _Image)
    {
        try {

                imgDesktop.Source = ToBitmapSource(_Image);
        }
        catch (Exception ex) {
            MessageBox.Show(ex.Message);
        }
    }


    public static BitmapSource ToBitmapSource(System.Drawing.Bitmap source)
    {
        BitmapSource bitSrc = null;

        var Bitmap_ = source.GetHbitmap();

        try {

            bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                Bitmap_,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
        }
        catch (Exception ex) {
            bitSrc = null;
        }
        finally {

        }

        return bitSrc;
    }

but the Image Control doesnt display the Image. Ive tried also to Bind the Image

Image Name="imgDesktop" Source={Binding ImgSource}">


public ImageSource YourImage {get;set;}

anybody an idea what im making wrong?

EDIT: Ive found an solution i converted a the byte array to a Bitmap Image and that worked for me:

public void GetImage(byte[] buffer)
        {
BitmapImage img = new BitmapImage();
        img.BeginInit();
        img.StreamSource = new MemoryStream(buffer);
        img.EndInit();
        this.imgDesktop.Source = img;
}
iNCEPTION
  • 45
  • 1
  • 7
  • First, you are [leaking memory](http://stackoverflow.com/questions/1546091/wpf-createbitmapsourcefromhbitmap-memory-leak). But more importantly, what is your code doing after calling **FiletransferImage**? Is it by any chance putting the UI thread to sleep? –  Dec 17 '13 at 19:27
  • Other possibilities would be the Image control itself not having the correct size (being to small or its ActualWidth/ActualHeight being 0) - also pay attention to the fact that WPF does not operate with absolute pixel values but with logical pixel dimensions based on 96 DPI. –  Dec 17 '13 at 19:32
  • Then, there is also the possibility that `Bitmap_` actually is an empty bitmap... –  Dec 17 '13 at 19:33
  • yeah ive checked the Bitmap and went through the debugger but everything seems alright the, the only problem is that nothing happens. The event is fired every 2 seconds with a new Image. – iNCEPTION Dec 17 '13 at 19:36
  • Okay, let's isolate different potential causes. First create some image resources in VS (just for testing, you can throw them away when finished) and let `FiletransferImage` feed them to the Image control. Do not use the Bitmap object for that. If this works, your problem is with the provided Bitmap objects and their conversion. If it doesn't work, something is fishy in your UI. If you have tried that, follow up here so we can properly target the cause of the problem... –  Dec 17 '13 at 19:40

0 Answers0