Search code examples
c#wpfwinformsimagebmp

Load multiple images to a wpf image control


I am working on a winform project which shows images from a device. The problem is that I am converting code from c # winform c # wpf and I'm struggling with the code that displays the images

This is the code in winform that works fine.

void Ps_Sample_Apl_CS_ShowSilhouette(MemoryStream buff)
{
    System.Drawing.Image img = System.Drawing.Image.FromStream(buff);
    img.RotateFlip(RotateFlipType.RotateNoneFlipX);
    ImagePic.Image = img;
    return;
}

This is my code that im trying to show the images but doesnt work in wpf

void Ps_Sample_Apl_CS_ShowSilhouette(MemoryStream buff)
{
    System.Drawing.Image img = System.Drawing.Image.FromStream(buff);

    BitmapImage myBitmapImage = new BitmapImage();
    myBitmapImage.BeginInit();
    myBitmapImage.StreamSource = buff;
    //myBitmapImage.UriSource = new Uri(img.ToString());
    //myBitmapImage.DecodePixelWidth = 200;
    ImagePic.Source = myBitmapImage;
    myBitmapImage.EndInit();

    return;
}

What should I do?


Solution

  • The method should look like this:

    void Ps_Sample_Apl_CS_ShowSilhouette(MemoryStream buff)
    {
        BitmapImage bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.StreamSource = buff;
        bitmapImage.EndInit();
        ImagePic.Source = bitmapImage;
    }