Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 原始位图数据/扫描线(镜像驱动程序原始数据)?_C#_Driver_Remote Desktop - Fatal编程技术网

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

C# 原始位图数据/扫描线(镜像驱动程序原始数据)?,c#,driver,remote-desktop,C#,Driver,Remote Desktop,我正在使用DFMirage的免费镜像驱动程序编写一个实时控制/远程桌面解决方案。有一个关于如何接口和控制镜像驱动程序的C#示例。当然,您需要先安装镜像驱动程序。因此,概念是,客户端(助手)请求屏幕更新,服务器(受害者)使用原始像素编码发送屏幕更新。镜像驱动程序的概念消除了对屏幕更改进行昂贵轮询的需要,因为镜像驱动程序会实时通知所有屏幕绘制操作。镜像驱动程序接收更新矩形的位置和大小,可以简单地查询内存中的新像素字节并发送它们 这应该很容易,只是我不知道如何在内存中查询新的像素字节。该示例演示了如何

我正在使用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;
这是伟大的,工作良好。生成的
位图
对象现在具有整个屏幕的像素。但是,如果我只想提取一个矩形的像素数据,而不是从整个屏幕上获取像素数据,我该怎么做呢?我想这更像是一个问题,但我输入了所有这些,这样你可能知道这是从哪里来的。那么,对于如何只获取一部分像素数据而不是整个屏幕的像素数据,有什么见解吗


更新:找到某些内容(仅限代码部分)。

这里有一个函数,可以将某个源图像缓冲区中的矩形区域复制到
位图中:

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;
}
这假设
GetChangesBuffer
具有源图像缓冲区的步长和像素格式属性。很可能没有,但您应该有一些方法来确定输入图像的步幅和像素格式。在您的示例中,假设输入图像的步幅等于输出图像的步幅,这是一个复杂的假设

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