0

我正在构建一个应用程序,除其他外,让用户拍摄一张照片,然后将照片和其他信息(如这张照片拍摄地点的位置 - 使用手机的 GPS 等)保存在数据库。

我使用字符串将图片保存到数据库。到现在为止还挺好。我的问题是,在用户捕获图片后,我无法在任何地方找到图片的路径(以便稍后向用户显示) 我知道如果我使用图像而不是字符串,我可以显示图片但是然后我无法将其保存到数据库中。

此外,我使用图片字符串(应该是图片的路径)作为我表中的 primaryKey 列,如果字符串为空,这肯定是个问题。

在互联网上检查后,我发现你不能在模拟器上使用 GeoCoordinateWatcher(用于 GPS),所以我不得不使用一个随机的地方。这让我想到,模拟器拍的照片可能没有路径??

我的部分代码:(相机事件和将所有内容保存到数据库的 bottun)

   void c_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            MessageBox.Show(e.ChosenPhoto.Length.ToString());


            //Code to display the photo on the page in an image control named myImage.
            BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
            bmp.SetSource(e.ChosenPhoto);
            myImage.Source = bmp;//display the picture right after taking it. before saving to DB
            p.UrlImage = bmp.UriSource.AbsolutePath;//Do not Work!

        }
    }

    private void saveToDB_Click(object sender, RoutedEventArgs e)
    {

        p.Description = DesriptionList.SelectedValue.ToString();//description of the pic
        p.Date = DateTime.Today;//date picture taken

        GeoCoordinateWatcher myWatcher = new GeoCoordinateWatcher();
        var myPosition = myWatcher.Position;
        p.Location = myPosition.Location.Altitude+" "+myPosition.Location.Latitude;//do not work with emulator
        p.Location = "Some Where Over The Rainbow";
        MainPage.m._bl.addPic(p);//add pic to DB
        MessageBox.Show("Added Successfully! :)");
        NavigationService.Navigate(new Uri(@"/Intro.xaml", UriKind.Relative));//go to another page
    }
}

我的课:

  [Table]
public class Picture
{
    public Picture()
    {

    }

    public Picture(string l, string d, string url, DateTime da)
    {
        Location = l;
        Description = d;
        UrlImage = url;
        Date = da;
    }
    string location;

    [Column]
    public string Location
    {
        get { return location; }
        set { location = value; }
    }
    string urlImage;

    [Column (IsPrimaryKey=true)]
    public string UrlImage
    {
        get { return urlImage; }
        set { urlImage = value; }
    }
    DateTime date;
    [Column]
    public DateTime Date
    {
        get { return date; }
        set { date = value; }
    }
    string description;
    [Column]
    public string Description
    {
        get { return description; }
        set { description = value; }
    }
}

}

无论如何-我想知道我是否可以通过某种方式获得路径...而且-如果我无法获得路径-Windows是否有“更好”的模拟器?这个模拟器不能做很多事情,这很烦人,因为我没有 WP 来检查我的应用程序。谢谢!

4

1 回答 1

0

您已经在 e.ChosenPhoto 中获得了拍摄图像的流。你只需要保存它。

var imageBytes = new byte[e.ChosenPhoto.Length];
e.ChosenPhoto.Read(imageBytes, 0, imageBytes.Length);

using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication()) {
  using (var stream = isoFile.CreateFile("myImage.jpg")) {
    stream.Write(imageBytes, 0, imageBytes.Length);
  }
}

编辑:

关于模拟器,它没有任何错误或限制。

拍摄的图像存储在一个临时文件中,稍后可能会消失,这就是为什么如果您想再次显示该图像,您需要将其本地保存在隔离存储中。

关于 GPS,您可以使用其他工具(只需单击模拟器右侧的“>>”按钮即可设置您在实际设备上找到的各种设置,例如加速度计、位置、网络等。对于 GeoWatcher您可以在地图上定义一组点,这些点将像设备的实际 GPS 位置发生变化一样进行回放。

于 2013-05-13T22:04:02.170 回答