2016-09-16 20 views
0

BitmapImagebyte[]に変換する必要があります。このデータをSQLiteデータベースに保存し、それを逆に読み込むことができます。UWPのBitmapImageをUWPで変換してsqliteに挿入

<Image x:Name="image" HorizontalAlignment="Left" Height="254" Margin="50,117,0,0" VerticalAlignment="Top" Width="244" Source="Assets/LockScreenLogo.png"/>

私のC#コード::これは動作しない

public sealed partial class MainPage : Page 
{ 
    public MainPage() 
    { 
     this.InitializeComponent(); 
     BitmapImage test = new BitmapImage(); 
     test = (BitmapImage)image.Source; 
     image.Source = test; 

     byte[] array = ImageToByte(test); 
     Database.CreateDB(); 
     Database.InsertData(array); 
    } 



    #region convert 
    public byte[] ImageToByte(BitmapImage image) 
    { 

     //WriteableBitmap wb; 
     using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream()) 
     { 
      image.SetSource(ms); 

      WriteableBitmap wb = new WriteableBitmap(244, 254); 
      wb.SetSource(ms); 

      using (Stream stream = wb.PixelBuffer.AsStream()) 
      using (MemoryStream memoryStream = new MemoryStream()) 
      { 
       stream.CopyTo(memoryStream); 
       return memoryStream.ToArray(); 
      } 
     } 
    } 

    public BitmapImage ByteToImage(byte[] array) 
    { 
     BitmapImage image = new BitmapImage(); 
     using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream()) 
     { 
      stream.AsStreamForWrite().Write(array, 0, array.Length); 
      stream.Seek(0); 
      image.SetSource(stream); 
     } 
     return image; 
    } 
    #endregion 

} 

私のXAMLは、イメージを持っています。

例外メッセージ:

は「型 『System.ArgumentNullException』の例外がmscorlib.ni.dllで発生したが、ユーザーコード

追加情報で処理されませんでした:値がnullにすることはできません。」

誰かが私を助けることができますか?

+0

[UWPでBitmapImageをバイト配列に変換](http://stackoverflow.com/questions/36108056/convert-a-bitmapimage-to-byte-array-in-uwp)の可能な複製 – Clemens

答えて

0

残念ながら、イメージのソース(StorageItemやURLなど)がない限り不可能です。

ピクセルにアクセスするには(後で処理します)の代わりにWriteableBitmapオブジェクトが必要です。

関連する問題