2

jpgファイルを読み取り、後で画像コントロールに表示する必要があります。以下は完全に機能します。

imgTwo.Source = FetchImage(@"C:\Image075.jpg");

public BitmapSource FetchImage(string URLlink)
{
      JpegBitmapDecoder decoder = null;
      BitmapSource bitmapSource = null;
      decoder = new JpegBitmapDecoder(new Uri(URLlink, UriKind.Absolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
      bitmapSource = decoder.Frames[0];
      bitmapSource.Freeze();
      return bitmapSource;
}

私の問題は、この画像をByte [](varbinary(MAX)としてデータベースに保持し、上記のようにファイルから直接ではなく、そこから読み取る必要があることです。したがって、入力としてByte[]を使用する必要があります。 URLlink文字列の代わりにこの関数に追加するか、BitmapSourceをByte []として保存します。これを行うにはどうすればよいですか?

4

1 に答える 1

4

JpegBitmapDecoderを受け入れる2番目のコンストラクターStreamがあります。あなたの:MemoryStreamを含むものを渡すだけですbyte[]

using(var stream = new MemoryStream(yourByteArray))
{
    decoder = new JpegBitmapDecoder(stream,
                                    BitmapCreateOptions.PreservePixelFormat,
                                    BitmapCacheOption.OnLoad);
}
于 2011-08-25T08:54:28.947 に答える