Click here to Skip to main content
15,887,421 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am trying to store image into the database on my phone and I found out that its best I convert into byte and store it. currently I am using this code:

C#
BitmapImage bimg = new BitmapImage();
                bimg.SetSource(this.ImageToStream(image));
                byte[] bytearray = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    WriteableBitmap wbitmp = new WriteableBitmap(bimg);
                    wbitmp.SaveJpeg(ms, wbitmp.PixelWidth, wbitmp.PixelHeight, 0, 100);
                    ms.Seek(0, SeekOrigin.Begin);
                    bytearray = ms.GetBuffer();
                }
                string str = Convert.ToBase64String(bytearray);
                sounds.SoundPic = str;

and this method:

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;

        }


And when I save it into my database, I got this error: String truncation: max=100, len=4172.

I tried increasing my database column size but I got hit by another error stating that it cannot take in 5000 (thats what I put).

Any pointers anyone??
Thanks! =)
Posted
Updated 21-Apr-12 23:08pm
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