Link to home
Start Free TrialLog in
Avatar of ddlam
ddlamFlag for Hong Kong

asked on

Send/Receive raw data of Bitmap

Hi, I am making a simple streaming video.
I need to turn a lot of bitmap into raw data and send it out using UDP Client.
Then, UDP Server receives the raw data and retrive the bitmap.
There is a simple UDPClient:

                UdpClient udpClient = new UdpClient();
                Byte[] inputToBeSent = new Byte[256];
                inputToBeSent = Encoding.ASCII.GetBytes("RRRRR".ToCharArray());
                udpClient.Connect(IPAddress.Parse("127.0.0.1"), 11001);
                int nBytesSent = udpClient.Send(inputToBeSent, inputToBeSent.Length);
                udpClient.Close();

The bitmap object is created by:
                Bitmap bitmapPic = new Bitmap(pictureBox.Image);

What Encoding and Decoding function should I use to generate Bitmap raw data?
ASKER CERTIFIED SOLUTION
Avatar of vo1d
vo1d
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh sorry, i just saw, that that example copies the data by using the marshaller class.
so no need for an unsafe method with pointers ;)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ddlam

ASKER

vo1d, so which code could be simplified?
Avatar of ddlam

ASKER

I am using vb 2005 standard but my aim is make the bmp deliverable as I don't know how to get the raw data of the bitmap and convert the raw data into deliverable format.
Here is a class to convert to/from an image and a byte array:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public class MemoryImage
{

  public Image ByteArrayToImage(byte[] buffer, string fileName)
  {
    MemoryStream ms = new MemoryStream(buffer);
    Image img = Image.FromStream(ms);
    return img;
  }

  public byte[] ImageToByteArray(Image img, ImageFormat format)
  {
    MemoryStream ms = new MemoryStream();
    img.Save(ms, format);
    return ms.ToArray();
  }
}

Bob
to get the bytes of your image, you can use thelearnedone's methods.
but i woul recommend in changing the ImageToByteArray method, because it will have some overhead in memory usage by using the method ToArray().
you have the image data in the memorystream and than have to copy teh data into a bytearray.

to use therse methods:

public Bitmap BitmapFromByteArray(byte[] buffer)
{
  MemoryStream ms = new MemoryStream(buffer);
  Bitmap bmp = Bitmap.FromStream(ms);
  return(bmp);
}

public byte[] BitmapToByteArray(Bitmap bmp)
{    
  Rectangle rect                                               = new Rectangle(0, 0, bmp.Width, bmp.Height);
  System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
  IntPtr ptr                                                       = bmpData.Scan0;
 
  byte[] data= new byte[(bmp.Width * bmp.Height)<<2];
  System.Runtime.InteropServices.Marshal.Copy(ptr, data, 0, data.Length);
  bmp.UnlockBits(bmpData);
  return (data);
}

so thats two methods with which you can work.
as i said before, you should compress that byte array before sending and decompress after receipt but before pushing it into the BitmapFromByteArray method.
Avatar of ddlam

ASKER

Finally, I use ImageConverter. Thanks all.