How to convert byte[] to bitmap

804 views Asked by At

I want to share bitmap between server and client. I've tried a lot, but I've had problems with all the way the bottom part of the image missing.

  1. First way:

             Bitmap bmp = new Bitmap(800, 450);
    
             using (var ms = new MemoryStream(readBuffer))
             {
                 bmp = new Bitmap(ms);
             }
    
  2. Second way:

             using (var ms = new MemoryStream(readBuffer))
             {
                 bmp = new Bitmap(ms);
                 //bmp = Image.FromStream(ms) as Bitmap;
             }
    
  3. This way, the image wasn't converted properly.

             Bitmap bmp = new Bitmap(800, 450, PixelFormat.Format24bppRgb);
    
             BitmapData bmpData = bmp.LockBits(
                                  new Rectangle(0, 0, bmp.Width, bmp.Height),
                                  ImageLockMode.WriteOnly, bmp.PixelFormat);
    
             Marshal.Copy(readBuffer, 0, bmpData.Scan0, readBuffer.Length);
    
             bmp.UnlockBits(bmpData);
    

How can I properly convert to a bitmap where the bottom part has not disappeared? This is the code I used to convert the bitmap to byte.

        Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
        panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height));

        using (var stream = new MemoryStream())
        {
            bmp.Save(stream, ImageFormat.Png);
            sendBuffer = stream.ToArray();
        }
        Array.Resize(ref sendBuffer, 1024*4);
0

There are 0 answers