Image to byte[]

  • Thread starter Thread starter Isaias Formacio Serna
  • Start date Start date
I

Isaias Formacio Serna

This should be an easy question but I simply just can't get it to work.

I have an Image object that I need to convert to byte[], how do I do this???
I don't have the file to read it, I recieve it as a parameter.

I appreciate your help.

Isaias Formacio
 
Isaias said:
This should be an easy question but I simply just can't get it to work.

I have an Image object that I need to convert to byte[], how do I do this???
I don't have the file to read it, I recieve it as a parameter.

I appreciate your help.

Isaias Formacio

Something like;

private void ProcessImage( System.Drawing.Image i) {
System.IO.MemoryStream s = new System.IO.MemoryStream();

i.Save( s, System.Drawing.Imaging.ImageFormat.Jpeg);

byte [] b = s.ToArray();
}

adjusted to your needs.
 
This is how I finally did it. Thanks mikeb.



System.Drawing.ImageConverter ic = new System.Drawing.ImageConverter();

byte[] photo = new byte[1];

photo = (byte[])ic.ConvertTo(imagen, photo.GetType());



mikeb said:
Isaias said:
This should be an easy question but I simply just can't get it to work.

I have an Image object that I need to convert to byte[], how do I do this???
I don't have the file to read it, I recieve it as a parameter.

I appreciate your help.

Isaias Formacio

Something like;

private void ProcessImage( System.Drawing.Image i) {
System.IO.MemoryStream s = new System.IO.MemoryStream();

i.Save( s, System.Drawing.Imaging.ImageFormat.Jpeg);

byte [] b = s.ToArray();
}

adjusted to your needs.
 
You can compact those last two lines into one:

byte[] photo = (byte[])ic.ConvertTo(imagen, photo.GetType());

Otherwise you're creating an array of 1 byte that is immediately discarded.

Isaias Formacio Serna said:
This is how I finally did it. Thanks mikeb.



System.Drawing.ImageConverter ic = new System.Drawing.ImageConverter();

byte[] photo = new byte[1];

photo = (byte[])ic.ConvertTo(imagen, photo.GetType());



mikeb said:
Isaias said:
This should be an easy question but I simply just can't get it to work.

I have an Image object that I need to convert to byte[], how do I do this???
I don't have the file to read it, I recieve it as a parameter.

I appreciate your help.

Isaias Formacio

Something like;

private void ProcessImage( System.Drawing.Image i) {
System.IO.MemoryStream s = new System.IO.MemoryStream();

i.Save( s, System.Drawing.Imaging.ImageFormat.Jpeg);

byte [] b = s.ToArray();
}

adjusted to your needs.
 
Back
Top