简体   繁体   English

使用字节数组将TIF图像显示为可写位图(C#)

[英]Displaying TIF image using byte array to writeable bitmap(C#)

I've a byte array of a tiff image. 我有一个tiff图片的字节数组。 I want to form 3 images from it,that i'll do later but the problem is i'm not able to display the original image as it is. 我想从中形成3张图像,稍后再做,但问题是我无法按原样显示原始图像。

Here is my xaml: 这是我的xaml:

<Image Grid.Row="0" Grid.Column="0" Name="ReferenceImage"/>

xaml.cs code: 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;
            }

        }

My main aim is to display image using byte array like this way only, so that i can extract byte array to form other image as per requirements. 我的主要目的是仅以这种方式使用字节数组显示图像,以便我可以根据需要提取字节数组以形成其他图像。

The image file contains encoded bitmap data. 图像文件包含编码的位图数据。 In order to access the raw pixels you would first have to decode the bitmap: 为了访问原始像素,您首先必须解码位图:

BitmapSource bitmap;

using (var fileStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read))
{
    bitmap = BitmapFrame.Create(
        fileStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}

Now you would get the raw pixels by calling the CopyPixels method of the BitmapSource: 现在,您可以通过调用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM