Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Send/Receive raw data of Bitmap

Posted on 2006-07-09
8
Medium Priority
?
1,284 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 11

Accepted Solution

by:
vo1d earned 800 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 600 total points
ID: 17068941
If you have 2005, you can compress without a 3rd party DLL.

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

885 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