Solved

Send/Receive raw data of Bitmap

Posted on 2006-07-09
8
1,211 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
 
LVL 9

Author Comment

by:ddlam
ID: 17078013
vo1d, so which code could be simplified?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 36
c# code 19 59
cs.Designer Issue 2 18
Is setting this variable making a duplicate in memory 4 13
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now