正しく表示できるように、12 ビットのグレー イメージを 8 ビット表現に変換したいと考えています。方法は次のとおりです。
byte[] dstData = null;
BitmapImage displayableBitmapImage = null;
if (numberOfBitsUsed == 12 || numberOfBitsUsed == 16)
{
int height = image.PixelHeight;
int width = image.PixelWidth;
int bytesPerPixel= ((image.Format.BitsPerPixel + 7) / 8);
int stride = width * bytesPerPixel;
// get the byte array from the src image
byte[] srcData = new byte[height * stride];
image.CopyPixels(srcData, stride, 0);
// create the target byte array
dstData = new byte[height * width];
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
int ndx = (j * stride) + (i * bytesPerPixel);
//get the values from the src buffer
byte val1 = srcData[ndx + 1];
//convert the value to 8bit representation
if (numberOfBitsUsed == 12)
{
byte val2 = srcData[ndx];
//the combined value of 2 bytes
ushort val = (ushort)((val1 << 8) | val2);
//shift by 4 ( >> 4 )
dstData[i + j * width] = (byte)(val >> 4);
}
else if (numberOfBitsUsed == 16)
{
// shift by 8 ( >> 8 ) is not required, just use the upper byte...
dstData[i + j * width] = val1;
}
}
}
}
現在、バイト配列から適切な 8 ビット BitmapImage を作成する方法がわかりません。私はこのように試しました:
BitmapImage bi = null;
if (dstData != null)
{
bi = new BitmapImage();
MemoryStream stream = new MemoryStream(dstData);
stream.Seek(0, SeekOrigin.Begin);
try
{
bi.BeginInit();
bi.StreamSource = stream;
bi.EndInit();
}
catch (Exception ex)
{
return null;
}
}
しかし、System.NotSupportedException が発生し続けます。バイト配列から BitmapImage を正しく作成するにはどうすればよいですか?
タビナ