从帧缓冲区字节有效地制作裁剪的位图图像。 [英] Efficiently make a cropped bitmap image from frame buffer bytes.

查看:98
本文介绍了从帧缓冲区字节有效地制作裁剪的位图图像。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它接受实时视频输入并将其映射到WPF图像(这工作得很好);问题是我只需要显示视频的裁剪区域。

I am developing an application that takes live video input and maps it onto a WPF Image (this is working just fine); the problem is that I need only a cropped region of the video to display.

从我正在使用的库中,我可以获得帧缓冲(int32ptr到原始数据)以及其他必要的信息,如图像高度,宽度和格式。

From the library I'm using, I can get the framebuffer (int32ptr to raw data) plus the other necessary information, like image height, width, and format.

以下完美地生成一个完整的框架:

The following works perfectly to generate a full frame:

 Dim source As BitmapSource = BitmapSource.Create(
                    av_props.vidProps.nWidth,
                    Math.Abs(av_props.vidProps.nHeight),
                    96, 96,
                    PixelFormats.Bgr32, Nothing,
                    New IntPtr(framePointer), frameDataSize,
                    av_props.vidProps.nRowBytes
                )

显然:av_props是来自的对象视频库; framePointer和frameData大小是你期望的。  上面的代码完美无瑕。

Obviously: av_props is an object from the video library; framePointer and frameData size are what you expect them to be.  The above code works flawlessly.

现在我想要一个裁剪后的图像,让我们说一个从上到左开始为100的200乘200节。

Now I want a cropped image, let's say a 200 by 200 section starting 100 from both the top and left.

Dim rect As New Int32Rect(100, 100, 200, 200)
Dim cutout As CroppedBitmap = New CroppedBitmap(source, rect)

这也很有效。  问题归结为效率问题。  使用CroppedBitmap,似乎应该可以跳过创建完整位图的中间步骤,仅用于裁剪它。  语法类似于:

This also works perfectly.  The question comes down to one of efficiency.  Using a CroppedBitmap, it seems like it should be possible to skip the intermediate step of creating a full bitmap only to crop it.  The syntax would be something like:

dim cropped as new CroppedBitmap()
cropped.copy(rect, new Int32Ptr(frameBuffer), frameDataSize, av_props.vidProps.nRowBytes)

问题当然是"裁剪"的。未初始化 - 例如,它不知道像素格式,因此它甚至无法判断每个像素的字节数。  这些值是只读的;使用"cropped.BeginInit()"不允许你设置

The problem, of course, is "cropped" is not initialized - it doesn't know the pixel format, for example, so it can't even tell how many bytes per pixel.  These values are read-only; using "cropped.BeginInit()" does not allow you to set them.

这将与另一个对时间敏感的程序一起运行,所以我试图尽可能高效。  我一直在阅读各种BitmapSource子类,但我找不到任何东西。

This will be running alongside another time-sensitive program, so I'm trying to be as efficient as possible.  I've been reading through the various BitmapSource sub-classes, and am not finding anything.

BTW,帧捕获在它自己的线程中,所以我需要"冻结" ;我被允许将它应用于WPF图像之前的BitmapSource。  顺便说一句,我是一个很长时间的开发人员,但很短的时间.Net开发人员,所以大部分这些东西对我来说都是新手,所以如果这应该有一个明显的答案,请原谅
我。

BTW, the frame capture is in it's own thread, so I need to "freeze" the BitmapSource before I am allowed to apply it to a WPF Image.  BTW, I am a long time developer, but short time .Net developer, so most of this stuff is new to me, so forgive me if this should have an obvious answer.

推荐答案


问题,当然,是"裁剪"的未初始化 - 例如,它不知道像素格式,因此它甚至无法判断每个像素的字节数。  这些值是只读的;使用"cropped.BeginInit()"不允许你设置

The problem, of course, is "cropped" is not initialized - it doesn't know the pixel format, for example, so it can't even tell how many bytes per pixel.  These values are read-only; using "cropped.BeginInit()" does not allow you to set them.

如果你继承CroppedBitmap并创建一个不需要源的自定义构造函数,那么您可以在初始化期间(Source对象)访问CroppedBitmap基础,并且可以调用
创建 < span class ="pun">()在该实例上,传入必需的参数(您将包含在自定义构造函数签名中)。无论是否提高性能,都必须通过实验来确定
。我没有使用过这两个类,但它看起来大致如下:

If you subclass CroppedBitmap and create a custom constructor that does not require a source, then you have access to the CroppedBitmap base during initialisation (a Source object) and you could call .Create() on that instance, passing in the required parameters (which you would include in your custom constructor signature). Whether that improves the performance or not would have to be determined by experiment. I haven't used either of those classes, but it would look roughly like:

Public Class MyCroppedBitmap(Int32, Int32, Double, Double, PixelFormat, BitmapPalette, IntPtr, Int32, Int32, Int32Rect)
    Inherits CroppedBitmap
    MyBase.Create(Int32, Int32, Double, Double, PixelFormat, BitmapPalette, IntPtr, Int32, Int32)
    SourceRect = Int32Rect
End Class





这篇关于从帧缓冲区字节有效地制作裁剪的位图图像。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆