Fast conversion of Bitmap to ImageSource [duplicate]

我们两清 提交于 2021-02-05 12:17:20

问题


I'm programming in WPF(c#) for image processing purpose. What is fastet way for converting Bitmap to ImageSource?


回答1:


Try converting it to a BitmapImage first:

public BitmapImage ConvertBitmap(System.Drawing.Bitmap bitmap)
    {         
        MemoryStream ms = new MemoryStream();
        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        ms.Seek(0, SeekOrigin.Begin);
        image.StreamSource = ms;
        image.EndInit();

        return image;
    }

Then:

public void MyMethod(System.Drawing.Bitmap myBitmap)
{
    var myImage = new Image();
    myImage.Source = ConvertBitmap(myBitmap);
}

You didn't explain where the Bitmap was coming from so I had to leave that part out.



来源:https://stackoverflow.com/questions/37890121/fast-conversion-of-bitmap-to-imagesource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!