Solved

Send/Receive raw data of Bitmap

Posted on 2006-07-09
8
1,228 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 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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