Click here to Skip to main content
16,013,581 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, I am trying to save my image to the local database. the image is captured by the phone camera (capture.show()). I am trying to convert images to byte array using the code below:

C#
public byte[] convertToByte(BitmapImage img)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                WriteableBitmap btmap = new WriteableBitmap(img.PixelWidth, img.PixelHeight);
                Extensions.SaveJpeg(btmap, ms, img.PixelWidth, img.PixelHeight, 0, 100);
                ms.Seek(0, SeekOrigin.Begin);
                return ms.ToArray();
            };
        }


I cant pass the image to this method as the control that displays this picture is of type
System.Windows.Controls.Image
and the above method accepts Bitmapimage object.

How do can i modify my code?
Posted

1 solution

You can modify your code as below.

C#
<pre>public byte[] convertToByte(Image imgControl)
{
  if(img.Source!=null)
  {
   BitmapImage img = img.Source as bitmapImage;
            using (MemoryStream ms = new MemoryStream())
            {
                WriteableBitmap btmap = new WriteableBitmap(img.PixelWidth, img.PixelHeight);
                Extensions.SaveJpeg(btmap, ms, img.PixelWidth, img.PixelHeight, 0, 100);
                ms.Seek(0, SeekOrigin.Begin);
                return ms.ToArray();
            };
   }
}
 
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