c# - 在 WPF 中创建 16 位+ 灰度图像

标签 c# wpf .net-4.0

我想根据 WPF 程序中的数据值创建 16 位灰度图像。目前我一直在考虑使用带有 PixelFormats.Gray16 设置的 WriteableBitmap。

但是我无法让它工作,并且 Microsoft 页面 (http://msdn.microsoft.com/en-us/magazine/cc534995.aspx) 将 Gray16 格式列为不可通过 WriteableBitmap 写入,但是没有建议如何以这种方式制作一个。

目前我的代码在循环中运行,其中 i 表示图像高度,j 表示宽度,看起来像这样:

short dataValue = GetDataSamplePixelValue(myDataValue);

//the pixel to fill with this data value:
int pixelOffset = ((i * imageWidth) + j) * bytesPerPixel;

//set the pixel colour values:
pixels[pixelOffset] = dataValue;

我确实得到了一张图像,但它只是一堆垂直的黑白线。如果仅使用 8 位灰度数据(在这种情况下,将上例中的 short 更改为 byte),我不会遇到问题。

有谁知道如何使用 WPF 创建每像素 16 位或更高的灰度图像?该图像最终也需要保存。

非常感谢任何建议。

编辑

此外,我还进行了一些编辑,现在使用 Gray16 PixelFormat 获得了一个合理的图像。我很难判断它是否实际上是 16 位,因为图像程序的颜色计数给出了 256,而且我不确定这是否是因为图像受到 WPF 的约束,或者图像程序可能这样做不支持它,因为显然许多图像程序忽略了低 8 位。现在我会坚持我所拥有的。

有关信息,代码如下:

myBitmap = new WriteableBitmap((int)visualRect.Width, (int)visualRect.Height, 96, 96, PixelFormats.Gray16, null);

int bytesPerPixel = myBitmap.Format.BitsPerPixel / 8;
ushort[] pixels = new ushort[(int)myBitmap.PixelWidth * (int)myBitmap.PixelHeight];

//if there is a shift factor, set the background colour to white:
if (shiftFactor > 0)
{
    for (int i = 0; i < pixels.Length; i++)
    {
        pixels[i] = 255;
    }
}

//the area to be drawn to:
Int32Rect drawRegionRect = new Int32Rect(0, 0, (int)myBitmap.PixelWidth, (int)myBitmap.PixelHeight);

//the number of samples available at this line (reduced by one so that the picked sample can't lie beyond the array):
double availableSamples = myDataFile.DataSamples.Length - 1;

for (int i = 0; i < numDataLinesOnDisplay; i++)
{
    //the current line to use:
    int currentLine = ((numDataLinesOnDisplay - 1) - i) + startLine < 0 ? 0 : ((numDataLinesOnDisplay- 1) - i) + startLine;

    for (int j = 0; j < myBitmap.PixelWidth; j++)
    {                             
        //data sample to use:
        int sampleToUse = (int)(Math.Floor((availableSamples / myBitmap.PixelWidth) * j));

        //get the data value:
        ushort dataValue = GetDataSamplePixelValue(sampleToUse);

        //the pixel to fill with this data value:
        int pixelOffset = (((i + shiftFactor) * (int)myBitmap.PixelWidth) + j);

        //set the pixel colour values:
        pixels[pixelOffset] = dataValue;
    }
}

//copy the byte array into the image: 
int stride = myBitmap.PixelWidth * bytesPerPixel;
myBitmap.WritePixels(drawRegionRect, pixels, stride, 0);

在此示例中,startLine 和 shiftFactor 已设置,并且取决于用户正在查看数据文件中的哪个点,在数据文件小于屏幕的情况下,shiftFactor 仅非零,在这种情况下我我使用此值将图像垂直居中。

最佳答案

查找代码中的错误或显示完整代码

下一个使用gray16图像的示例正常工作

  var width = 300;
  var height = 300;

  var bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray16, null);
  var pixels = new ushort[width * height];
  for (var y = 0; y < height; ++y)
    for (var x = 0; x < width; ++x)
    {
      var v = (0x10000*2 * x/width + 0x10000 * 3 * y / height);
      var isMirror = (v / 0x10000) % 2 == 1;
      v = v % 0xFFFF;
      if (isMirror)
        v = 0xFFFF - v;

      pixels[y * width + x] = (ushort)v;
    }

  bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width *2, 0);

  var encoder = new PngBitmapEncoder();
  encoder.Frames.Add(BitmapFrame.Create(bitmap));
  using (var stream = System.IO.File.Create("gray16.png"))
    encoder.Save(stream);

关于c# - 在 WPF 中创建 16 位+ 灰度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9588367/

相关文章:

c# - 将sql查询命令超时设置为max的负面影响

c# - 为什么 c# 编译器从这段代码创建一个 PrivateImplementationDetails?

c# - 如何将多个 ViewModel 添加到主窗口中的用户控件

c# - 使用已知但未给定的时区解析 DateTime

c# - 基于文本框动态更新WPF Slider

wpf - 将枚举与 ObjectDataProvider 绑定(bind)

wpf - MVVM 模型验证和数据绑定(bind)?

C#协变结构理解?

.net-4.0 - 是否有 Microsoft.VisualBasic.FileIO.TextFieldParser 的写对应项?

c# - 放置多个显示错误 :There is already an open DataReader associated with this Command which must be closed first 的 sql 命令时