Tuesday, December 12, 2006

How to convert Image in Binary Stream/Byte array

Following code explains how to get an image from a website as stream and convert it to byte array. Then from byte array to System.Drawing.Image.

The important thing to notice is, when we get stream from web, it gives error in getting length of the stream. The helper method gets the length and convert the stream to byte array.

Following is the code:


private void buttonProcess_Click(object sender, EventArgs e)
{
string url = @"http://localhost:1137/WebSiteImage/TestImage.jpg";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
byte[] byteData;
using (Stream stream = response.GetResponseStream())
{
byteData = ReadStream(stream, 1000);
}
using (MemoryStream mStream = new MemoryStream(byteData))
{
Image image = Image.FromStream(mStream);
this.pictureBoxTest.Image = image;
}
}


private byte[] ReadStream(Stream stream, int initialLength)
{
if (initialLength < 1)
{
initialLength = 32768;
}
byte[] buffer = new byte[initialLength];
int read = 0;
int chunk;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte == -1)
{
return buffer;
}
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
byte[] bytes = new byte[read];
Array.Copy(buffer, bytes, read);
return bytes;
}

8 comments:

  1. Hi Ashish,

    Nice to see your blog and the articles are really good...

    Happy Blogging..!!

    ReplyDelete
  2. Hello. Was searching and I stumbled across your code. was wondering if this implementation would work across Java ME. And also how to facilitate it in a PHP enabled mobile download. Please repl. Thank you. Demola

    ReplyDelete
  3. Very good article, helped me a lot
    Thank you

    ReplyDelete
  4. Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

    ReplyDelete
  5. Hello I'd like to thank you for such a terrific quality forum!
    Just thought this would be a perfect way to introduce myself!

    Sincerely,
    Robin Toby
    if you're ever bored check out my site!
    [url=http://www.partyopedia.com/articles/hanukkah-party-supplies.html]hanukkah Party Supplies[/url].

    ReplyDelete
  6. Nice fill someone in on and this mail helped me alot in my college assignement. Gratefulness you seeking your information.

    ReplyDelete
  7. Hi ashish,am supposed to upload an image to picasa web album from google app engine.Am able to get the byte stream of the image and store it into a byte array.Any idea how to send this byte array of the image to picasa web album.any help is greatly appreciated..thnx.

    ReplyDelete
  8. You may need to use picasa SDK. I hope following should help you:

    Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username, albumid));
    System.IO.FileInfo fileInfo = new System.IO.FileInfo(file);
    System.IO.FileStream fileStream = fileInfo.OpenRead();
    PicasaEntry entry = (PicasaEntry) service.Insert(postUri, fileStream, "image/jpeg", file);
    fileStream.Close();

    ReplyDelete