2017-04-03 99 views
1

我需要在我的通用Windows应用程序中显示.pgm图像。 XAML图像控件不直接支持.pgm图像,所以我需要解决它。在通用Windows应用程序(c#,XAML)中显示便携式灰度图(PGM)图像

在c#中打开.pgm文件的互联网上有很多例子,但所有这些都依赖于使用通用Windows平台不支持的Bitmap对象(System.Drawing和System.Windows.Media库不能使用)。

我已经得到了读取图像宽度和高度的代码,并读取字节数组中的像素(包含代表灰色阴影的值0-255)。

下一步将使用任何可以最终传递给XAML Image.Source(并在合理的时间内完成)的对象从byte []数组中绘制图像。

我能够做到的最好的是显示this 但实际的图片应该看起来像this(由于某种原因,它显示的图像和4倍的颜色是错误的)。

我使用的代码:

public int width; 
    public int height; 
    public int maxVal; //255 
    public byte[] pixels; 

    public async Task<WriteableBitmap> ToWriteableBitmap() 
    { 
     WriteableBitmap writeableBitmap = new WriteableBitmap(width, height); 
     using (Stream stream = writeableBitmap.PixelBuffer.AsStream()) 
     { 
      await stream.WriteAsync(pixels, 0, pixels.Length); 
     } 
     return writeableBitmap; 
    } 

应该有关系,我也提供我用读.PGM文件到PgmImage对象的代码,但我敢肯定,这工作得很好:

public static async Task<PgmImage> LoadFromFile(string file) 
    { 
     FileStream ifs = null; 
     await Task.Run(() => 
     { 
      Task.Yield(); 
      ifs = new FileStream(file, FileMode.Open, FileAccess.Read); 
     }); 
     BinaryReader br = new BinaryReader(ifs); 

     string magic = NextNonCommentLine(br); 
     //if (magic != "P5") 
     // throw new Exception("Unknown magic number: " + magic); 

     string widthHeight = NextNonCommentLine(br); 
     string[] tokens = widthHeight.Split(' '); 
     int width = int.Parse(tokens[0]); 
     int height = int.Parse(tokens[1]); 

     string sMaxVal = NextNonCommentLine(br); 
     int maxVal = int.Parse(sMaxVal); 

     byte[] pixels = new byte[height * width]; 
     for (int i = 0; i < height * width; i++) 
     { 
      pixels[i] = br.ReadByte(); 
     } 
     return new PgmImage(width, height, maxVal, pixels); 
    } 

    static string NextAnyLine(BinaryReader br) 
    { 
     string s = ""; 
     byte b = 0; // dummy 
     while (b != 10) // newline 
     { 
      b = br.ReadByte(); 
      char c = (char)b; 
      s += c; 
     } 
     return s.Trim(); 
    } 

    static string NextNonCommentLine(BinaryReader br) 
    { 
     string s = NextAnyLine(br); 
     while (s.StartsWith("#") || s == "") 
      s = NextAnyLine(br); 
     return s; 
    } 

(它是这一个稍微修改版本:jamesmccaffrey.wordpress.com/2014/10/21/a-pgm-image-viewer-using-c)。 我应该提到,我更喜欢不依赖任何第三方库或Nu​​Get软件包的解决方案,但我绝望,因此对任何解决方案都开放。

回答

0

WritableBitmap.PixelBuffer使用RGBA颜色空间,这意味着每个像素都用字节数组中的四个字节来描述,但从PGM图像生成的数组只使用一个字节来描述一个像素。 通过简单地扩展数组4次(并将每个像素的alpha值设置为最大值255),我设法获得了正确的显示。

public async Task<WriteableBitmap> ToWriteableBitmap() 
    { 
     WriteableBitmap writeableBitmap = new WriteableBitmap(width, height); 

     byte[] expanded = new byte[pixels.Length * 4]; 
     int j = 0; 
     for (int i = 0; i< pixels.Length; i++) 
     { 
      expanded[j++] = pixels[i]; 
      expanded[j++]= pixels[i]; 
      expanded[j++] = pixels[i]; 
      expanded[j++] = 255; //Alpha 
     } 

     using (Stream stream = writeableBitmap.PixelBuffer.AsStream()) 
     { 
      await stream.WriteAsync(expanded, 0, expanded.Length); 
     } 
     return writeableBitmap; 
    } 
0

我测试了你的代码并重现了你的问题。问题是WriteableBitmap无法通过xmal控制完美加载。

我试图使用SoftwareBitmap来存储PgmByteData从pgm文件加载。它工作正常。

您的像素在字节数组中包含值0-255代表灰色阴影。因此,您可以使用BitmapPixelFormat.Gray8作为BitmapPixelFormat。您的云创建SoftwareBitmap作为以下代码。

SoftwareBitmap softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Gray8, (int)width, (int)height); 
      softwareBitmap.CopyFromBuffer(pgmByteData.AsBuffer()); 

目前,图像控制仅支持使用BGRA8编码和预乘以图像或没有alpha通道。尝试显示图像之前,请测试以确保其格式正确,如果不正确,则使用SoftwareBitmap静态转换方法将图像转换为支持的格式。

欲了解更多信息,请参阅Use SoftwareBitmap with a XAML Image control

if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || 
    softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight) 
{ 
    softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); 
} 

var source = new SoftwareBitmapSource(); 
await source.SetBitmapAsync(softwareBitmap); 

// Set the source of the Image control 
imageControl.Source = source; 

code sample已上传。请检查。

+0

谢谢尼科为您提供快速和详细的答案。我还通过扩展byte []来适应BGRA编码,找到了自己的解决方案。我已经发布了代码作为答案;也许有人会觉得它有用。 – zvjeverica

相关问题