ddlam
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("R RRRR".ToCh arArray()) ;
udpClient.Connect(IPAddres s.Parse("1 27.0.0.1") , 11001);
int nBytesSent = udpClient.Send(inputToBeSe nt, 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?
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("R
udpClient.Connect(IPAddres
int nBytesSent = udpClient.Send(inputToBeSe
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
vo1d, so which code could be simplified?
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
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.Bit mapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.Ima geLockMode .ReadOnly, bmp.PixelFormat);
IntPtr ptr = bmpData.Scan0;
byte[] data= new byte[(bmp.Width * bmp.Height)<<2];
System.Runtime.InteropServ ices.Marsh al.Copy(pt r, 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.
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[]
{
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.Bit
IntPtr ptr = bmpData.Scan0;
byte[] data= new byte[(bmp.Width * bmp.Height)<<2];
System.Runtime.InteropServ
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.
ASKER
Finally, I use ImageConverter. Thanks all.
so no need for an unsafe method with pointers ;)