65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 WPF 显示原始图像

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.65/5 (12投票s)

2010年4月14日

CPOL

3分钟阅读

viewsIcon

98464

downloadIcon

8804

一篇描述如何使用 WPF 显示 8 位和 16 位矩形原始图像的文章。

DisplayRawImage.PNG

引言和背景

在屏幕上显示图像就像为图像处理爱好者编写 Hello World 程序一样。这里尝试使用 WPF 来显示一个矩形灰度原始图像。该程序允许用户在指定后查看原始图像文件

  • 它是 8 位还是 16 位,以及
  • 图像的尺寸(宽度和高度)。

原始图像文件是一个仅以自上而下、从左到右方式排列像素数据的文件;它没有任何标题。在没有标题的情况下,需要以上两部分信息来查看文件。由于图像的尺寸可能导致相应的图像无法在屏幕上显示,因此我们还提供了滚动条(如果需要)来滚动浏览图像。可以使用 C# 和 .NET 显示图像。在 WPF 中查看图像与在 C# 世界中查看图像有本质的不同。

这个小应用程序是 使用 C# 显示 16 位图像 的扩展。在那篇文章中,C# 语言被用来显示原始图像。在这里,通过 WPF 实现了同样的事情。

图像显示代码的描述

原始图像文件格式可能是最简单的灰度图像文件格式。众所周知,它不包含任何标题,而是直接包含像素值。因此,用户必须输入图像的宽度和高度。

用户界面相当简单明了,包含一些 Button 和一个 Image 控件。 DockPanelStackPanel 用于整齐地排列它们。

由于这是原始图像,因此不能在此处使用用于加载图像的常用 WPF 方法。我们需要使用 BinaryReader 读取像素值。读取文件后,根据文件大小(以及它是 8 位图像还是 16 位图像)计算像素数量。之后,会调用一个 ImageDimensions 对话框,提示用户输入图像的宽度和高度。这个 ImageDimensions 对话框给出了宽度和高度的初始猜测(它计算因子),并允许用户更改它们。在此基础上,创建图像,然后显示。以上所有内容都包含在下面给出的代码片段中。

private void DisplayImage16(string fileName)
{
    // Open a binary reader to read in the pixel data. 
    // We cannot use the usual image loading mechanisms since this is raw 
    // image data.
    try
    {
        BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open));
        ushort pixShort;
        int i;
        long iTotalSize = br.BaseStream.Length;
        int iNumberOfPixels = (int)(iTotalSize / 2);

        // Get the dimensions of the image from the user
        ID = new ImageDimensions(iNumberOfPixels);
        if (ID.ShowDialog() == true)
        {
            width = Convert.ToInt32(ID.tbWidth.Text);
            height = Convert.ToInt32(ID.tbHeight.Text);
            canvas.Width = width;
            canvas.Height = height;
            img.Width = width;
            img.Height = height;
            pix16 = new ushort[iNumberOfPixels];

            for (i = 0; i < iNumberOfPixels; ++i)
            {
                pixShort = (ushort)(br.ReadUInt16());
                pix16[i] = pixShort;
            }
            br.Close();

            int bitsPerPixel = 16;
            stride = (width * bitsPerPixel + 7) / 8;

            // Single step creation of the image
            bmps = BitmapSource.Create(width, height, 96, 96, 
                                PixelFormats.Gray16, null, pix16, stride);
            img.Source = bmps;
            bnSaveJPG.IsEnabled = true;
            bnSavePNG.IsEnabled = true;
        }
        else
        {
            br.Close();
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message, "Error", 
               MessageBoxButton.OK, MessageBoxImage.Error);
    }
}

使用类似的方法来显示 8 位图像。使用 PngBitmapEncoderJpegBitmapEncoder,可以轻松地将这些图像保存为 PNG 或 JPEG。

用于输入图像尺寸的对话框如下所示

ImageDimension.PNG

在附件中,有两个尺寸为 800 x 600 的矩形原始图像,每个图像具有 8 位和 16 位深度。

闭包

这就完成了显示矩形原始图像的简单 WPF 应用程序。这可以扩展到显示其他文件格式,例如 DICOM。

致谢

作者要感谢 Nagesh Rao 测试该应用程序。

历史

  • 版本 1:2010 年 4 月 12 日。
  • 版本 2:2010 年 4 月 27 日。
© . All rights reserved.