Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hye Everyone,

I m working in Windows Mobile 7,
and i need to convert the image stream otained from the camera capturetask,
into string to save it in my database....

void photoChooser_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
            {
                 //Convert e.ChosenPhoto to string
            }
        }

how can i do this.

i have tried -

private byte[] imageByte = new Byte[e.ChosenPhoto.Length];
string imgStream = e.ChosenPhoto.Read(imageByte, 0, imageByte.Length);

but no result.

how can i convert stream to string and back to stream.

thanks in advance
Posted
Updated 3-Feb-11 1:05am
v2

Don't convert it to a string - convert it to a byte array. Strings have a default conversion which you do not want for image data, bytes don't. Most databases have a VarBinary as well as a VerChar which is designed to hold such data.

Then convert the image to bytes:

public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
If your framework doesn't the include ImageConverter class (and I don't know if it does) you can convert it by writing the image to a stream, and then using the Stream.ToArray method but that is quite a bit less efficient.


"i tried this, but it is giving an exception

ConvertTo not implemented in base TypeConverter.


what should i do.
and also,
how can i convert this byte array back to image."


That probably means it isn't implemented in your framework - not everything is in the mobile frameworks. You can go via the messier stream though:
C#
public static byte[] ImageToByteStream(Image img)
{    byte[] data = new byte[0];
    using (MemoryStream ms = new MemoryStream())
    {
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        ms.Seek(0,0);
        data = ms.ToArray();
    }
    return data;
}
It is probably best if you reverse the process to go back to an image using the Bitmap.FromStream method.
 
Share this answer
 
v2
Comments
DeepsMann 29-Jan-11 7:49am    
thanks,

i tried this, but it is giving an exception

ConvertTo not implemented in base TypeConverter.


what should i do.
and also,
how can i convert this byte array back to image.
OriginalGriff 29-Jan-11 7:59am    
Answer updated
Hi everyone,

You can convert an Image object to a stream as:
private Stream ImageToStream(Image image1)
{
     WriteableBitmap wb = new WriteableBitmap(400,400);
     wb.Render(image1, new TranslateTransform {X=400, Y=400});
     wb.Invalidate();


     Stream myStream = new MemoryStream();
     wb.SaveJpeg(myStream, 400, 400, 0, 70);
     return myStream;
}

Cheers
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900