[英]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.