0

BitmapSource.Create について質問があります。次のコードがありますが、期待どおりに動作していません。

  reader.BaseStream.Position += BytesInMetadata;

  var rawData = new UInt16[NumberOfPixels];

  // Read in the raw image data in 16 bit format.
  NumberOfPixels.Times((Action<int>)(i => rawData[i] = reader.ReadUInt16()));

  var stats = new MsiStats()
  {
    Mean = rawData.Average(v => (Double)v),
    StdDev = rawData.StandardDeviation(v => (Double)v),
    Min = rawData.Min(),
    Max = rawData.Max()
  };

  // Convert the 16-bit image to an 8-bit image that can actually be displayed.
  var scaledData = ScaleData(rawData, 4.0f, CType);

  GCHandle handle = GCHandle.Alloc(scaledData, GCHandleType.Pinned);
  using (var bmp = new Bitmap(2048, 2048, 2048, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, handle.AddrOfPinnedObject()))
  {
    bmp.Save(@"C:\Users\icyr\Work Folders\COBRA_I-3\CAST Data\myOGBitmap.bmp");
  }
  handle.Free();

  var src = BitmapSource.Create(NumberOfColumns, NumberOfRows,
                                96, 96,
                                PixelFormats.Gray8, null,
                                scaledData,
                                NumberOfRows);

  using (var fileStream = new FileStream(@"C:\<somefolder>\myBitmap.bmp", FileMode.OpenOrCreate))
  {
    BitmapEncoder enc = new BmpBitmapEncoder();
    enc.Frames.Add(BitmapFrame.Create(src));
    enc.Save(fileStream);
  }

独自の画像ファイルから 12 ビット値を読み取り、8 ビットに変換してから、bitmapsource オブジェクトとして保存しています。ただし、それを読み返す (または以下で行うように保存する) と、保存されます... 間違っています。私はそれを説明する方法さえわかりません。Matlab で保存された画像を読み取ると、Bitmapsource オブジェクトから保存されたファイルには、17 の倍数のピクセル値しかありません。scaledData オブジェクトから保存されたファイルには、値の全範囲があります。

何が起きてる?残念ながら、私は自分が書いていないコードのフレームワーク内で作業しており、プロジェクト全体をオーバーホールしたい場合を除いて (そうしていませんし、時間もありません)、引き続き使用できるようにする必要があります。私のデータストレージ用の BitmapSource オブジェクト。

ここで何をすべきか途方に暮れているので、なぜこれが起こっているのか、そして最小限の変更でそれを防ぐ方法をよりよく理解していただけることを願っています.

4

1 に答える 1