简体   繁体   English

WPF的等效功能是什么?

[英]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()泄漏句柄的一些重要事实。

Finally, this article might be of help. 最后, 本文可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么是WPF ListBox的DataSource的等价物? - What is the equivalent of DataSource for a WPF ListBox? WPF等同于WinForm字符串? - What is WPF equivalent of WinForm Strings? wpf 中以下代码的等效项是什么? - What would be the equivalent of the following code in wpf? 用于数据表的Winforms代码-WPF等价于什么? - Winforms code for datatable - what's the WPF equivalent? 什么是WPF中Silverlight的FindElementsInHostCoordinates等价物? - What is the Silverlight's FindElementsInHostCoordinates equivalent in WPF? 什么相当于 WPF 应用程序中的 Application.DoEvents() - What is equivalent to Application.DoEvents() in WPF applications 什么是Winform ToolStripContainer的WPF等效项? - What's the WPF equivalent of a Winform ToolStripContainer? 在UWP中,什么是DependencyProperty类WPF的DependencyType属性? - In UWP, what is the equivalent of DependencyType property of DependencyProperty Class of WPF? Winforms Drawing.rectangle.union 的 WPF 等效项是什么? - What is the WPF equivalent of Winforms Drawing.rectangle.union? 在WPF中,相当于Windows窗体中的Suspend / ResumeLayout()和BackgroundWorker() - In WPF, what is the equivalent of Suspend/ResumeLayout() and BackgroundWorker() from Windows Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM