繁体   English   中英

C#合并通过套接字发送的图像

[英]c# merge images sent over socket

我试图通过套接字发送屏幕截图,所以我使用不安全的指针仅发送差异:

    private unsafe Bitmap GetDiffBitmap(Bitmap bmp, Bitmap bmp2)
    {


        bmpRes = new Bitmap(1920, 1080,bmp.PixelFormat);

        bmData = bmp.LockBits(new Rectangle(0, 0, 1920, 1080), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
        bmData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp2.PixelFormat);
        bmDataRes = bmpRes.LockBits(new Rectangle(0, 0, bmpRes.Width, bmpRes.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);

        IntPtr scan0 = bmData.Scan0;
        IntPtr scan02 = bmData2.Scan0;
        IntPtr scan0Res = bmDataRes.Scan0;


        int stride = bmData.Stride;
        int stride2 = bmData2.Stride;
        int strideRes = bmDataRes.Stride;

        int nWidth = bmp.Width;
        int nHeight = bmp.Height;

        //for(int y = 0; y < nHeight; y++)
        System.Threading.Tasks.Parallel.For(0, nHeight, y =>
        {
            //define the pointers inside the first loop for parallelizing
            byte* p = (byte*)scan0.ToPointer();
            p += y * stride;
            byte* p2 = (byte*)scan02.ToPointer();
            p2 += y * stride2;
            byte* pRes = (byte*)scan0Res.ToPointer();
            pRes += y * strideRes;

            for (int x = 0; x < nWidth; x++)
            {
                //always get the complete pixel when differences are found
                if (p[0] != p2[0] || p[1] != p2[1] || p[2] != p2[2])
                {
                    pRes[0] = p2[0];
                    pRes[1] = p2[1];
                    pRes[2] = p2[2];

                    //alpha (opacity)
                    pRes[3] = p2[3];
                }

                p += 4;
                p2 += 4;
                pRes += 4;
            }

        });



        bmp.UnlockBits(bmData);
        bmp2.UnlockBits(bmData2);
        bmpRes.UnlockBits(bmDataRes);

        return bmpRes;
    }

这是客户端上的呼叫:

   private void startSend()
    {

        Bitmap curr;

        Bitmap pre = screenshot();

        byte []bmpBytes = imageToByteArray(pre);

        SendVarData(handler, bmpBytes);// this is the first send of the whole screen

        while (true)
        {

            curr = screenshot();
            Bitmap diff = GetDiffBitmap(pre, curr);//generate differences.
            bmpBytes = imageToByteArray(diff);
            SendVarData(handler, bmpBytes);//sending the diff image.
            pre = curr; 


        }
    }

SendVarData是一种通过套接字发送字节数组的方法, 在这里就不成问题了。

这就是我在服务器端获取数据的方式:

  public void startListening()
    {

        Bitmap merge = new Bitmap(1920, 1080);
        Graphics g = Graphics.FromImage(merge);
         Bitmap  prev =  byteArrayToImage(ReceiveVarData(client.Client)) as  Bitmap;//getting the first full size image.

        theImage.Image = prev;//assisning it to picturebox.


        while (true)
        {
            byte[]data = ReceiveVarData(client.Client);

           Bitmap  curr = byteArrayToImage(data) as Bitmap;//here is the diffrent image

           //merge and apply differences
                g.DrawImage(prev, 0, 0,1920, 1080);
                g.DrawImage(curr, 0, 0, 1920,1080);

            theImage.Image = merge;

            count++;

            prev = merge;
        }  

    }

我的问题是,尽管我将两个图像与Graphics合并。仍然将其绘制(在第一个dataReceive之后)看起来还不完整...这实际上是我在服务器上看到的。 在此处输入图片说明

我不知道这是怎么回事...有人可以照亮我的眼睛吗? :D @DmitriTrofimov

    if (p[0] != p2[0] || p[1] != p2[1] || p[2] != p2[2])
                {
                    pRes[0] = p2[0];
                    pRes[1] = p2[1];
                    pRes[2] = p2[2];

                    //alpha (opacity)
                    pRes[3] = p2[3];
                }
                else
                    pRes[0] = 0;

这是保持像素不透明度时得到的。 您应该将相同的像素的Alpha设置为零,并确保您正在使用32位图像(请参阅Bitmap构造函数的PixelFormat参数)。

PS请确保您压缩diff位图,否则没有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM