[英]Displaying TIF image using byte array to writeable bitmap(C#)
我有一个tiff图片的字节数组。 我想从中形成3张图像,稍后再做,但问题是我无法按原样显示原始图像。
这是我的xaml:
<Image Grid.Row="0" Grid.Column="0" Name="ReferenceImage"/>
xaml.cs代码:
public MainWindow()
{
InitializeComponent();
ImagePath = @"G:\TiffImage\Test.TIF"
DisplayAllImages();
}
private void DisplayAllImages()
{
byte[] imageSize = File.ReadAllBytes(ImagePath);
ReferenceImage.Source = DisplayAllImages(imageSize, 64, 64);
}
private WriteableBitmap DisplayAllImages(byte[] imageData,int height,int width)
{
if (imageData != null)
{
PixelFormat format = PixelFormats.Gray8;
WriteableBitmap wbm = new WriteableBitmap(height, width, 96, 96, format, null);
wbm.WritePixels(new Int32Rect(0, 0, height, width), imageData, 1*width, 0);
return wbm;
}
else
{
return null;
}
}
我的主要目的是仅以这种方式使用字节数组显示图像,以便我可以根据需要提取字节数组以形成其他图像。
图像文件包含编码的位图数据。 为了访问原始像素,您首先必须解码位图:
BitmapSource bitmap;
using (var fileStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read))
{
bitmap = BitmapFrame.Create(
fileStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
现在,您可以通过调用BitmapSource的CopyPixels
方法来获取原始像素:
var width = bitmap.PixelWidth;
var height = bitmap.PixelHeight;
var stride = (width * bitmap.Format.BitsPerPixel + 7) / 8;
var imageData = new byte[height * stride];
bitmap.CopyPixels(imageData, stride, 0);
我没有得到期望的图像。虽然我能够生成几乎匹配75%但不完全匹配的图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.