2013-05-17 43 views
1

您好我發展Deng與本地數據庫 這是一個Windows Phone應用程序是從我的數據庫圖像窗口電話數據庫

[Table] 
    public class DBannotation 
    { 
     [Column(IsPrimaryKey = true, CanBeNull = false)] 
     public string annotguid { get; set; } 
     [Column(IsPrimaryKey = true, CanBeNull = false)] 
     public int foruserid { get; set; } 
     [Column] 
     public int annversion { get; set; } 
     [Column] 
     public string chapath { get; set; } 

}表

我必須將圖片插入數據庫,但我沒有找到我該怎麼做,我必須添加哪種類型的列 在此先感謝您的幫助

回答

0

您應該將圖像存儲在獨立存儲器中,並且爲了避免命名圖像的衝突,您應該使用GUID來命名圖像。當然,在你的表格中,你保存了那個GUID。

public static string SaveImage(Stream imageStream, int orientation, int quality) 
{ 
    var fileName = Guid.NewGuid().ToString(); 
    using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
     if (isolatedStorage.FileExists(fileName)) 
      isolatedStorage.DeleteFile(fileName); 

     IsolatedStorageFileStream fileStream = isolatedStorage.CreateFile(fileName); 
     BitmapImage bitmap = new BitmapImage(); 
     bitmap.SetSource(imageStream); 

     WriteableBitmap wb = new WriteableBitmap(bitmap); 
     wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, orientation, quality); 
     fileStream.Close(); 
    } 
    return fileName; 
} 
+0

這是我的第二個解決辦法,但我的約束是圖像 – hamza437

+0

的數量,您可以隨時查看獨立存儲的配額,並給予一定的反饋給用戶的使用多少間隔/自由。 http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstorage.quota.aspx – Ateik

-1

在表: [列] 公共字節[]圖片{得到;組; }

的XAML:

<Image Source="{Binding Image, Converter={StaticResource BytesToImageConverter}}"....> 



public class BytesToImageConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, string language) 
     { 

      byte[] bytes = value as byte[]; 
      if (bytes == null) return null; 

      BitmapImage image = new BitmapImage(); 
      SetSource(bytes, image); 
      return image; 

     } 

     private async static void SetSource(byte[] source, BitmapImage image) 
     { 
      var stream = await Utils.ToRandomAccessStream(source); 
      await image.SetSourceAsync(stream); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, string language) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
+1

我試過這個解決方案,但是當我保存在數據庫中我有這個異常'System.InvalidOperationException'發生在System.Data.Linq.dll字節數組截斷長度爲8000 – hamza437