MPV 提供了丰富的各种接口(姑且这么叫吧),其中有个功能是截图功能,使用时,笔者还是颇费了 一番功夫,或许是源于对C语言理解的肤浅。闲话少说,下面先给出文档原文,然后给出代码。
…
screenshot-raw []
Return a screenshot in memory. This can be used only through the client API. The MPV_FORMAT_NODE_MAP returned by this command has the w, h, stride fields set to obvious contents. The format field is set to bgr0 by default. This format is organized as B8G8R8X8 (where B is the LSB). The contents of the padding X are undefined. The data field is of type MPV_FORMAT_BYTE_ARRAY with the actual image data. The image is freed as soon as the result mpv_node is freed. As usual with client API semantics, you are not allowed to write to the image data.
The stride is the number of bytes from a pixel at (x0, y0) to the pixel at (x0, y0 + 1). This can be larger than w * 4 if the image was cropped, or if there is padding. This number can be negative as well. You access a pixel with byte_index = y * stride + x * 4 (assuming the bgr0 format).
The flags argument is like the first argument to screenshot and supports subtitles, video, window.
…
注:其他截图功能比较简单,例如保存到文件中这种方式,我们是要放到内存里。
这里的核心部分是对 MPV_FORMAT_NODE_MAP 的理解,我当时还是仔细研读了一下,现在不想再读了。直接给出处理代码部分,请大家对照代码在再结合上文进行理解。
public Bitmap getScreenShotBmp(string flags)
{
if (_mpvHandle == IntPtr.Zero) return null;
Bitmap bmp;
int w=0, h=0, stride=0;
//获得截图数据,byte数组格式
byte[] imgdat = getScreenShotDat(ref w,ref h, ref stride, flags);
//将byete数组数据转化成图像
bmp=imgLittleKit.setImagePixel(imgdat, w, h,stride/w, PixelFormat.Format32bppRgb);
return bmp;
}
getScreenShotDat 源代码,也是本文的核心部分
//flags:subtitles, video, window
// subtitles and video works well, window : messy picture got ???
public unsafe


1万+

被折叠的 条评论
为什么被折叠?



