2011-05-18 74 views
2

由於某些數據存儲限制(noSQL),我需要將圖像作爲字符串存儲。 如何將圖像位圖序列化爲字符串並返回。 這裏是我正在做它:從字符串中加載圖片

 

Uri testImageUri = new Uri("/DictionaryBasedVM;component/test.jpg", UriKind.Relative); 
StreamResourceInfo sri = Application.GetResourceStream(testImageUri);    
var stringData = GetString(sri.Stream); 
ImageSource = stringData;   
 

哪裏

ImageControl
只是在XAML中定義的Silverlight的圖像控制。
我使用以下實用功能:

 

     //For testing 
     public static string GetString(Stream stream) 
     { 
      byte[] byteArray = ReadFully(stream); 
      return Encoding.Unicode.GetString(byteArray,0,byteArray.Length); 
     } 
     public static byte[] ReadFully(Stream input) 
     { 
      byte[] buffer = new byte[16 * 1024]; 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       int read; 
       while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
       { 
        ms.Write(buffer, 0, read); 
       } 
       return ms.ToArray(); 
      } 
     } 
 

而以下屬性:

 

     private string _ImageSource = ""; 
     public string ImageSource 
     { 
      set 
      { 
       _ImageSource = value; 
       byte[] byteArray = Encoding.Unicode.GetBytes(value); 
       MemoryStream imageStream = new MemoryStream(byteArray); 
       BitmapImage imageSource = new BitmapImage(); 
       imageSource.SetSource(imageStream); 
       ImageControl.Source = imageSource; 
      } 
      get 
      { 
       return _ImageSource; 
      } 
     } 
 

我得到的錯誤: 「災難性故障(從HRESULT異常:0x8000FFFF(E_UNEXPECTED))」,如圖: enter image description here

即使我不把它存儲爲一個字符串,我仍然好奇爲什麼我不能這樣做。

回答

2

Unicode可能不是用於此目的的最佳編碼。你會更好的Base64編碼byte[]並存儲。

1

您是否嘗試過使用Convert.ToBase64StringConvert.FromBase64String方法?我猜測unicode GetString/GetBytes不起作用,因爲你的字節數組並不與'已知字符'對齊。

+0

真棒,修復它。 +1(自第二個響應)。 PS:其ToBase64String(http://msdn.microsoft.com/en-us/library/dhx0d524(v=VS.95).aspx)和FromBase64String(http://msdn.microsoft.com/en-us/library /7y0k9etd(v=VS.95).aspx) – basarat 2011-05-18 06:45:28

1

Base64會爲字符串添加30%的大小。我認爲這不是必要的。只要你不對字符串做任何事情,你就可以安全地將字節轉換爲字符,反之亦然。下面是一些代碼,似乎工作:

// usage example 
    string encoded = FileToString("myimage.png"); 
    Console.WriteLine(s.Length); 
    FileFromString(encoded, "copy.png"); 

    public static void FileFromString(string input, string filePath) 
    { 
     if (input == null) 
      throw new ArgumentNullException("input"); 

     if (filePath == null) 
      throw new ArgumentNullException("filePath"); 

     using (FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)) 
     { 
      byte[] buffer = FromString(input); 
      stream.Write(buffer, 0, buffer.Length); 
     } 
    } 

    public static byte[] FromString(string input) 
    { 
     if (input == null) 
      throw new ArgumentNullException("input"); 

     char[] cbuffer = input.ToCharArray(); 
     byte[] buffer = new byte[cbuffer.Length]; 
     for (int i = 0; i < buffer.Length; i++) 
     { 
      buffer[i] = (byte)cbuffer[i]; 
     } 
     return buffer; 
    } 

    public static string FileToString(string filePath) 
    { 
     if (filePath == null) 
      throw new ArgumentNullException("filePath"); 

     using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Write)) 
     { 
      return ToString(stream); 
     } 
    } 

    public static string ToString(Stream input) 
    { 
     if (input == null) 
      throw new ArgumentNullException("input"); 

     StringBuilder sb = new StringBuilder(); 
     byte[] buffer = new byte[4096]; 
     char[] cbuffer = new char[4096]; 
     int read; 
     do 
     { 
      read = input.Read(buffer, 0, buffer.Length); 
      for (int i = 0; i < read; i++) 
      { 
       cbuffer[i] = (char)buffer[i]; 
      } 
      sb.Append(new string(cbuffer, 0, read)); 
     } 
     while (read > 0); 
     return sb.ToString(); 
    } 

然而,這是可能的,你存儲字符串可能不喜歡它包含0或其他特殊數字的字符串系統。在這種情況下,base64仍然是一個選項。

+0

不錯的想法,我欣賞+1的努力。不過,我幸運的是沒有空間限制。 – basarat 2011-05-18 16:05:34