[英]What is the equivalent function for WPF?
I have the following function that works in Windows Form but I will need help to make it work in WPF with the image control. 我具有在Windows窗体中可以使用的以下功能,但我需要帮助以使其在WPF中使用图像控件。
public static Bitmap CreateBitmap(byte[] bytes, int width, int height)
{
var rgbBytes = new byte[bytes.Length * 3];
for (var i = 0; i <= bytes.Length - 1; i++)
{
rgbBytes[(i * 3)] = bytes[i];
rgbBytes[(i * 3) + 1] = bytes[i];
rgbBytes[(i * 3) + 2] = bytes[i];
}
var bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
for (var i = 0; i <= bmp.Height - 1; i++)
{
var p = new IntPtr(data.Scan0.ToInt32() + data.Stride * i);
System.Runtime.InteropServices.Marshal.Copy(rgbBytes, i * bmp.Width * 3, p, bmp.Width * 3);
}
bmp.UnlockBits(data);
return bmp;
}
Any help will be appreciated. 任何帮助将不胜感激。 Thanks
谢谢
Try something like this: 尝试这样的事情:
BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(),
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height));
image.Source = bs;
image
is an Image control. image
是一个Image控件。
Also, please check this question as it mentions some important facts about bmp.GetHbitmap()
leaking a handle. 另外,请检查此问题,因为它提到了有关
bmp.GetHbitmap()
泄漏句柄的一些重要事实。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.