将多个图像加载到wpf图像控件

时间:2014-02-05 21:59:11

标签: c# wpf winforms image bmp

我正在开展一个winform项目,该项目显示来自设备的图像。 问题是我正在从c#winform c#wpf转换代码,而我正在努力使用显示图像的代码

这是winform中运行良好的代码。

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;
}

这是我试图显示图像但在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;
}

我该怎么办?

1 个答案:

答案 0 :(得分:0)

该方法应如下所示:

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;
}