Show / Hide Table of Contents

Практическое руководство. Кодирование и декодирование изображения в формате TIFF

Следующие примеры показывают, как декодировать и кодировать Формат TIFF (Tagged Image File Format) изображений, используя заданный TiffBitmapDecoder и TiffBitmapEncoder объектов.

Пример

В этом примере показано, как декодировать TIFF изображений с помощью TiffBitmapDecoder из Uri.

// Open a Stream and decode a TIFF image.
Stream imageStreamSource = new FileStream("tulipfarm.tif", FileMode.Open, FileAccess.Read, FileShare.Read);
var decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];

// Draw the Image.
var myImage = new Image();
myImage.Source = bitmapSource;
myImage.Stretch = Stretch.None;
myImage.Margin = new Thickness(20);

Пример

В этом примере показаны способы кодирования BitmapSource в TIFF изображений с помощью TiffBitmapEncoder.

int width = 128;
int height = width;
int stride = width / 8;
byte[] pixels = new byte[height * stride];

// Define the image palette.
BitmapPalette myPalette = BitmapPalettes.WebPalette;

// Creates a new empty image with the pre-defined palette.

BitmapSource image = BitmapSource.Create(
    width,
    height,
    96,
    96,
    PixelFormats.Indexed1,
    myPalette,
    pixels,
    stride);

var stream = new FileStream("new.tif", FileMode.Create);
var encoder = new TiffBitmapEncoder();
var myTextBlock = new TextBlock();
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString();
encoder.Compression = TiffCompressOption.Zip;
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);

См. также

  • Общие сведения об обработке изображений
Back to top Неофициальная документация по .NET на русском языке. Лицензия: CC-BY 4.0. Основано на документации по .NET с Microsoft Docs
Generated by DocFX