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

时间:2015-07-10 12:57:19

标签: c# sockets image-processing

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

    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.Draw合并,它仍然(在第一个dataReceive之后)看起来不完整......这实际上是我在服务器上看到的...... enter image description here < / p>

我不知道这里有什么不对......有人可以点亮我的眼睛吗? :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;

1 个答案:

答案 0 :(得分:0)

这是保持像素不透明度时的效果。您应该在没有差异的像素上将Alpha设置为零,并确保使用32位图像(请参阅PixelFormat构造函数的Bitmap参数)。

P.S。确保压缩diff位图,否则没用。