0

I'm using some code to show live video from a camera.

In Windows Form (C#) the code worked fine, but in WPF application i getting an error in :

Bitmap img = (Bitmap)eventArgs.Frame.Clone();
Image.Source = img;

the error was : connot convert System.Drawing.Bitmap to System.Wondows.Media.ImageSource. So i converted the Bitmap image using this code:

public ImageSource imageSourceForImageControl(Bitmap BitmapImage)
{
ImageSourceConverter SourceImage = new ImageSourceConverter();
return (ImageSource)SourceImage.ConvertFrom(BitmapImage);
}

Then i got a System.NullReferenceException

Any idea what should i do??

PS : Im a Newbie with C# applications

Juan Carlos
  • 490
  • 5
  • 17

1 Answers1

0

I was using this for quite some time, try this.

public ImageSource imageSourceForImageControl(Bitmap bitmap)
{
    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();
        return (ImageSource)bitmapImage;
    }
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35