将图像保存到Windows Phone 7中的本地数据库

问题描述:

嘿伙计们,我正在尝试将我的图像保存到本地数据库。图像由手机摄像头捕获(capture.show())。我正在尝试使用以下代码将图像转换为字节数组:



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:

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

并且上面的方法接受Bitmapimage对象。



如何修改我的代码?

and the above method accepts Bitmapimage object.

How do can i modify my code?

您可以修改以下代码。



You can modify your code as below.

<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();
            };
   }
}