Solved

Send/Receive raw data of Bitmap

Posted on 2006-07-09
8
1,235 Views
Last Modified: 2007-12-19
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?
0
Comment
Question by:ddlam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 11

Accepted Solution

by:
vo1d earned 200 total points
ID: 17068656
you can compress the bytestream of the bitmap with a lossless encoding like zip does.
you can found an opensource implementation of ziplib here:
http://www.icsharpcode.net/OpenSource/SharpZipLib/

to get the raw data of that bitmap, use the lockbits / unlockbits functionality of the bitmap class.
by using lockbits on a bitmap, you will get a bitmapdata object which holds the pixels rawdata.

here is an example how to use that functionallity:

private void LockUnlockBitsExample(PaintEventArgs e)
{

    // Create a new bitmap.
    Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");

    // Lock the bitmap's bits.  
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    System.Drawing.Imaging.BitmapData bmpData =
        bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
        bmp.PixelFormat);
         
    // Get the address of the first line.
   IntPtr ptr = bmpData.Scan0;

    // Declare an array to hold the bytes of the bitmap.
    // This code is specific to a bitmap with 24 bits per pixels.
    int bytes = bmp.Width * bmp.Height * 3;
    byte[] rgbValues = new byte[bytes];

    // Copy the RGB values into the array.
    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

    // Set every red value to 255.  
    for (int counter = 0; counter < rgbValues.Length; counter+=3)
        rgbValues[counter] = 255;
 
    // Copy the RGB values back to the bitmap
    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

    // Unlock the bits.
    bmp.UnlockBits(bmpData);

    // Draw the modified image.
    e.Graphics.DrawImage(bmp, 0, 150);

}

you can fasten that implementation , because you only wanna copy the data.
u can read each pixel info(rgb) by reading an int instead of reading each byte of one pixel.
all that can be done in an unsafe method by using pointers.
let me know, if you are interested in it.
0
 
LVL 11

Expert Comment

by:vo1d
ID: 17068660
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 ;)
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 150 total points
ID: 17068941
If you have 2005, you can compress without a 3rd party DLL.

Bob
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 9

Author Comment

by:ddlam
ID: 17078013
vo1d, so which code could be simplified?
0
 
LVL 9

Author Comment

by:ddlam
ID: 17079505
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17080599
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
0
 
LVL 11

Expert Comment

by:vo1d
ID: 17082870
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.
0
 
LVL 9

Author Comment

by:ddlam
ID: 17099694
Finally, I use ImageConverter. Thanks all.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question