C# 位图 BitmapImage bitmapsource

发布于 2024-10-02 10:45:53 字数 1633 浏览 6 评论 0原文

我有一些在网上找到的代码。

unsafe static Bitmap SaveFrame(IntPtr pFrame, int width, int height)
    {
        try
        {
            int x, y;
            int linesize = width * 3;
            byte* scan0 = (byte*)Marshal.ReadIntPtr(pFrame);

            IntPtr bufferPtr = Marshal.AllocHGlobal(linesize * height);
            byte* buffer = (byte*)bufferPtr;

            for (y = 0; y < height; y++)
            {
                for (x = 0; x < linesize; x = x + 3)
                {
                    *buffer++ = (byte)*(scan0 + y * linesize + x + 2);
                    *buffer++ = (byte)*(scan0 + y * linesize + x + 1);
                    *buffer++ = (byte)*(scan0 + y * linesize + x);
                }
            }
            Bitmap b = new Bitmap(width, height, linesize, System.Drawing.Imaging.PixelFormat.Format24bppRgb, bufferPtr);



            return b;
        }
        catch (Exception ex) { throw new Exception(ex.Message); }
    }

上面的代码给了我一个有效的位图,但是我正在使用 WPF 并希望它位于 BitmapImage 中

而不经过这个过程,我尝试了

byte[] ptr = ....
Marshal.Copy(pFrame, ptr , 0, ptr .Length);
BitmapImage aBitmapImage = new BitmapImage(); 
aBitmapImage.BeginInit();
aBitmapImage.StreamSource = new MemoryStream(ptr); //FLastImageMemStream;//
aBitmapImage.EndInit();

不起作用的代码...

我也尝试过,

System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96,
    System.Windows.Media.PixelFormats.Rgb24, null, bufferPtr,linesize * height,
    width * 3 ));

但也没有给我看起来是一个图像(将其分配给 Image 的 Source 属性后)

任何人都可以给我任何提示吗? 谢谢 艾伦

I have some code I found somewhere on the Net.

unsafe static Bitmap SaveFrame(IntPtr pFrame, int width, int height)
    {
        try
        {
            int x, y;
            int linesize = width * 3;
            byte* scan0 = (byte*)Marshal.ReadIntPtr(pFrame);

            IntPtr bufferPtr = Marshal.AllocHGlobal(linesize * height);
            byte* buffer = (byte*)bufferPtr;

            for (y = 0; y < height; y++)
            {
                for (x = 0; x < linesize; x = x + 3)
                {
                    *buffer++ = (byte)*(scan0 + y * linesize + x + 2);
                    *buffer++ = (byte)*(scan0 + y * linesize + x + 1);
                    *buffer++ = (byte)*(scan0 + y * linesize + x);
                }
            }
            Bitmap b = new Bitmap(width, height, linesize, System.Drawing.Imaging.PixelFormat.Format24bppRgb, bufferPtr);



            return b;
        }
        catch (Exception ex) { throw new Exception(ex.Message); }
    }

This above code gives me a valid Bitmap, However I'm using WPF and want it to be in a BitmapImage

Without going through this process, Im trying the code

byte[] ptr = ....
Marshal.Copy(pFrame, ptr , 0, ptr .Length);
BitmapImage aBitmapImage = new BitmapImage(); 
aBitmapImage.BeginInit();
aBitmapImage.StreamSource = new MemoryStream(ptr); //FLastImageMemStream;//
aBitmapImage.EndInit();

which does not work...

I have also tried

System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96,
    System.Windows.Media.PixelFormats.Rgb24, null, bufferPtr,linesize * height,
    width * 3 ));

which also does not give me an image it seems (after assigning it to the Source property of Image)

Can anyone give me any hints?
Thanks
Allen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

将数据直接加载到 BitmapImage 中不起作用,因为它需要图像文件格式的数据,就像您在 .bmp 或 .png 文件中看到的那样。您改为提供原始像素数据。

您的第二种方法看起来应该可行,但有一些不必要的步骤。您找到的代码将像素数据从 BGR24 重写为 RGB24,但您应该能够直接将其加载为 BGR24:

System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96,
    System.Windows.Media.PixelFormats.Bgr24, null, pFrame, linesize * height,
    width * 3 ));

无论如何,您是否有任何关于为什么它不给您图像的详细信息?创建位图时是否遇到任何异常?是否给出了错误的颜色或错误的尺寸?您确定所有源数据都在那里吗?创建 BitmapSource 后,您会在其属性中看到什么?

Loading the data directly into the BitmapImage didn't work because it's expecting data in an image file format, like you'd see in a .bmp or .png file. You supplied raw pixel data instead.

Your second approach looks like it should work but has some unnecessary steps to it. The code you found is rewriting the pixel data from BGR24 to RGB24, but you should be able to just load it in directly as BGR24:

System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96,
    System.Windows.Media.PixelFormats.Bgr24, null, pFrame, linesize * height,
    width * 3 ));

Anyway, do you have any details on why it's not giving you an image? Did you get any exceptions when creating the bitmap? Did it give the wrong colors or wrong size? Are you sure all the source data is there? What do you see on the properties of the BitmapSource after you create it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文