原始位图数据/扫描线(镜像驱动程序原始数据)?

发布于 2024-09-08 06:20:48 字数 1555 浏览 8 评论 0原文

我正在使用 DFMirage 的免费镜像驱动程序编写实时控制/远程桌面解决方案。 此处有一个关于如何连接和控制镜像驱动程序的 C# 示例。当然,您需要先安装镜像驱动程序,此处。因此,其概念是,客户端(帮助者)请求屏幕更新,服务器(受害者)使用原始像素编码发送屏幕更新。镜像驱动程序的概念消除了昂贵的轮询屏幕更改的需要,因为镜像驱动程序会实时收到所有屏幕绘制操作的通知。镜像驱动程序接收更新矩形的位置和大小,并且可以简单地查询内存以获取新的像素字节并将其发送。

应该很容易,只是我不知道如何执行我们查询内存以获取新像素字节的部分。该示例展示了如何使用原始位图数据、扫描线和跨度以及所有这些好东西来查询内存以获取整个屏幕的像素:

Bitmap result = new Bitmap(_bitmapWidth, _bitmapHeight, format);
Rectangle rect = new Rectangle(0, 0, _bitmapWidth, _bitmapHeight);
BitmapData bmpData = result.LockBits(rect, ImageLockMode.WriteOnly, format);

// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;

// Declare an array to hold the bytes of the bitmap.
int bytes = bmpData.Stride * _bitmapHeight;
var getChangesBuffer = (GetChangesBuffer)Marshal
    .PtrToStructure(_getChangesBuffer, typeof (GetChangesBuffer));
var data = new byte[bytes];
Marshal.Copy(getChangesBuffer.UserBuffer, data, 0, bytes);

// Copy the RGB values into the bitmap.
Marshal.Copy(data, 0, ptr, bytes);
result.UnlockBits(bmpData);
return result;

这很棒并且工作正常。生成的 Bitmap 对象现在具有整个屏幕的像素。但是,如果我只想提取一个矩形的像素数据,而不是从整个屏幕获取像素数据,我该如何做到这一点呢?我想这更像是一个 rawbitmap-scan-stride 问题,但我输入了所有这些,所以你可能知道这是从哪里来的。那么关于如何获取一部分像素数据而不是整个屏幕的像素数据有什么见解吗?

更新:发现一些有趣的(仅代码部分)。

I'm coding a live control/remote desktop solution using DFMirage's free mirror driver. There is a C# sample on how to interface and control the mirror driver here. You would need the mirror driver installed first, of course, here. So, the concept is, the client (helper) requests a screen update, and the server (victim) sends one, using raw pixel encoding. The concept of a mirror driver eliminates the need to expensively poll for screen changes, because a mirror driver is notified of all screen drawing operations in real-time. The mirror driver receives the location and size of the update rectangle, and can simply query memory for the new pixel bytes and send them.

Should be easy, except that I don't know how to do that part where we query memory for the new pixel bytes. The sample shows how to query memory to grab the pixels of the entire screen using something with raw bitmap data and scan lines and stride and all that good stuff:

Bitmap result = new Bitmap(_bitmapWidth, _bitmapHeight, format);
Rectangle rect = new Rectangle(0, 0, _bitmapWidth, _bitmapHeight);
BitmapData bmpData = result.LockBits(rect, ImageLockMode.WriteOnly, format);

// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;

// Declare an array to hold the bytes of the bitmap.
int bytes = bmpData.Stride * _bitmapHeight;
var getChangesBuffer = (GetChangesBuffer)Marshal
    .PtrToStructure(_getChangesBuffer, typeof (GetChangesBuffer));
var data = new byte[bytes];
Marshal.Copy(getChangesBuffer.UserBuffer, data, 0, bytes);

// Copy the RGB values into the bitmap.
Marshal.Copy(data, 0, ptr, bytes);
result.UnlockBits(bmpData);
return result;

This is great and works fine. The resulting Bitmap object now has the pixels of the entire screen. But if I wanted to just extract a rectangle of pixel data instead of getting the pixel data from the whole screen, how would I be able to do that? I guess this is more of a rawbitmap-scan-stride question, but I typed all of this so you might know where this is coming from. So any insight on how to get just a portion of pixel data instead of the entire screen's pixel data?

Update: Found something interesting (code portion only).

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

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

发布评论

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

评论(1

黯然 2024-09-15 06:20:48

这是一个将矩形区域从某些源图像缓冲区复制到位图的函数:

private static Bitmap ExtractImageRectangle(byte[] sourceBuffer, int sourceStride, PixelFormat sourcePixelFormat, Rectangle rectangle)
{
    Bitmap result = new Bitmap(rectangle.Width, rectangle.Height, sourcePixelFormat);
    BitmapData resultData = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, result.PixelFormat);

    int bytesPerPixel = GetBytesPerPixel(sourcePixelFormat); // Left as an exercise for the reader

    try
    {
        // Bounds checking omitted for brevity
        for (int rowIndex = 0; rowIndex < rectangle.Height; ++rowIndex)
        {
            // The address of the start of this row in the destination image
            IntPtr destinationLineStart = resultData.Scan0 + resultData.Stride * rowIndex;

            // The index at which the current row of our rectangle starts in the source image
            int sourceIndex = sourceStride * (rowIndex + rectangle.Top) + rectangle.Left * bytesPerPixel;

            // Copy the row from the source to the destination
            Marshal.Copy(sourceBuffer, sourceIndex, destinationLineStart, rectangle.Width * bytesPerPixel);
        }
    }
    finally
    {
        result.UnlockBits(resultData);
    }

    return result;
}

然后您可以像这样使用它:

Rectangle roi = new Rectangle(100, 150, 200, 250);
Bitmap result = ExtractImageRectangle(getChangesBuffer.UserBuffer, getChangesBuffer.Stride, getChangesBuffer.PixelFormat, roi);

这里假设 GetChangesBuffer 具有步幅和像素格式的属性源图像缓冲区。它很可能没有,但您应该有一些方法来确定输入图像的步幅和像素格式。在您的示例中,您假设输入图像的步幅等于输出图像的步幅,这是一个棘手的假设。

Here's a function to copy a rectangular area from some source image buffer to a Bitmap:

private static Bitmap ExtractImageRectangle(byte[] sourceBuffer, int sourceStride, PixelFormat sourcePixelFormat, Rectangle rectangle)
{
    Bitmap result = new Bitmap(rectangle.Width, rectangle.Height, sourcePixelFormat);
    BitmapData resultData = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, result.PixelFormat);

    int bytesPerPixel = GetBytesPerPixel(sourcePixelFormat); // Left as an exercise for the reader

    try
    {
        // Bounds checking omitted for brevity
        for (int rowIndex = 0; rowIndex < rectangle.Height; ++rowIndex)
        {
            // The address of the start of this row in the destination image
            IntPtr destinationLineStart = resultData.Scan0 + resultData.Stride * rowIndex;

            // The index at which the current row of our rectangle starts in the source image
            int sourceIndex = sourceStride * (rowIndex + rectangle.Top) + rectangle.Left * bytesPerPixel;

            // Copy the row from the source to the destination
            Marshal.Copy(sourceBuffer, sourceIndex, destinationLineStart, rectangle.Width * bytesPerPixel);
        }
    }
    finally
    {
        result.UnlockBits(resultData);
    }

    return result;
}

You could then use it like this:

Rectangle roi = new Rectangle(100, 150, 200, 250);
Bitmap result = ExtractImageRectangle(getChangesBuffer.UserBuffer, getChangesBuffer.Stride, getChangesBuffer.PixelFormat, roi);

This assumes that GetChangesBuffer has properties for the stride and pixelformat of the source image buffer. It most likely hasn't, but you should have some means to determine the stride and pixel format of your input image. In your example you are assuming the stride of the input image is equal to the stride of your output image, which is a tricky assumption.

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