Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建16位+;WPF中的灰度图像_C#_Wpf_.net 4.0 - Fatal编程技术网

C# 创建16位+;WPF中的灰度图像

C# 创建16位+;WPF中的灰度图像,c#,wpf,.net-4.0,C#,Wpf,.net 4.0,我想从WPF程序中的数据值创建一个16位灰度图像。目前,我一直在考虑使用PixelFormats.Gray16集的WriteableBitmap myBitmap = new WriteableBitmap((int)visualRect.Width, (int)visualRect.Height, 96, 96, PixelFormats.Gray16, null); int bytesPerPixel = myBitmap.Format.BitsPerPixel / 8; ushort[]

我想从WPF程序中的数据值创建一个16位灰度图像。目前,我一直在考虑使用PixelFormats.Gray16集的WriteableBitmap

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);
但是我不能让它工作,还有一个微软的页面(http://msdn.microsoft.com/en-us/magazine/cc534995.aspx)通过WriteableBitmap将Gray16格式列为不可写,但不建议以其他方式制作

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);
当前我的代码在一个循环中运行,其中
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;
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);
我确实得到了一个图像,但它只是一堆垂直的黑白线。如果仅使用8位灰度数据(在上述示例中,
short
更改为
byte
),则没有问题

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);
有人知道如何使用WPF创建每像素16位或更高灰度的图像吗?此图像最终也需要保存

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);
任何建议都将不胜感激

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);
编辑

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);
除此之外,我还做了一些编辑,现在使用Gray16像素格式获得了一幅清晰的图像。但我很难判断它是否为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);
有关信息,代码如下所示:

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);
myBitmap=newwriteablebitmap((int)visualRect.Width,(int)visualRect.Height,96,96,PixelFormats.Gray16,null);
int bytesPerPixel=myBitmap.Format.BitsPerPixel/8;
ushort[]像素=新的ushort[(int)myBitmap.PixelWidth*(int)myBitmap.PixelHeight];
//如果存在偏移因子,请将背景颜色设置为白色:
如果(移位因子>0)
{
对于(int i=0;i

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

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

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);
下一个gray16图像正常工作的示例

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);
  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);
var宽度=300;
var高度=300;
var bitmap=新的可写位图(宽度、高度、96、96、像素格式.Gray16、空);
var像素=新的ushort[宽度*高度];
对于(变量y=0;y
作为参考,屏幕不太可能显示16位灰度图像,而且Windows不太支持这种格式。例如,Windows XP甚至不能在Photo viewer中显示16位灰度图像,尽管Windows 7+可以(我不确定Vista,我没有)

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);
除此之外,.NET open TIF方法不会加载16位灰度图像

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);
加载和保存16位灰度图像的解决方案,通常我建议TIFs使用。然后,您可以选择加载整个TIF,或逐行加载,以及其他方法。我建议逐行加载,因为这样您就可以只保留屏幕上可见的数据,因为现在有些TIF变得非常大,无法由单个阵列保存

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);

因此,最终,不要担心在屏幕上显示16位灰度,它可能会受到系统/监视器功能的限制,而且人眼无论如何也无法分辨这与8位之间的区别。但是,如果您需要加载或保存16位,请使用LibTIFF。

我看不到任何证据表明WriteableBitmap与PixelFormats有问题。Gray16您的问题到底是什么?用一些代码处理图像,并试图解释你得到的无效结果如何?我不确定我是否理解;2^16给出65536种唯一颜色,2^8给出256种。保存图像,加载保存的图像,并在代码中生成您自己的直方图,以查看您是否实际获得了正确的颜色数。如果你不能信任其他的程序,那就自己做吧!
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);