Vorgehensweise: Codieren und Decodieren eines TIFF-Bilds
Die folgenden Beispiele zeigen, wie Sie ein Tagged Image File Format (TIFF)-Bild mithilfe der spezifischen TiffBitmapDecoder und TiffBitmapEncoder Objekte dekodieren und kodieren.
Decodieren eines TIFF-Bilds
In diesem Beispiel wird das Decodieren eines TIFF-Bilds mithilfe eines TiffBitmapDecoder aus einem Uriveranschaulicht.
// Open a Stream and decode a TIFF image.
Stream^ imageStreamSource = gcnew FileStream("tulipfarm.tif", FileMode::Open, FileAccess::Read, FileShare::Read);
TiffBitmapDecoder^ decoder = gcnew TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions::PreservePixelFormat, BitmapCacheOption::Default);
BitmapSource^ bitmapSource = decoder->Frames[0];
// Draw the Image.
Image^ myImage = gcnew Image();
myImage->Source = bitmapSource;
myImage->Stretch = Stretch::None;
myImage->Margin = System::Windows::Thickness(20);
// 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);
' Open a Stream and decode a TIFF image.
Dim imageStreamSource As New FileStream("tulipfarm.tif", FileMode.Open, FileAccess.Read, FileShare.Read)
Dim decoder As New TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
Dim bitmapSource As BitmapSource = decoder.Frames(0)
' Draw the Image.
Dim myImage As New Image()
myImage.Source = bitmapSource
myImage.Stretch = Stretch.None
myImage.Margin = New Thickness(20)
Codieren eines TIFF-Bilds
Dieses Beispiel zeigt, wie ein BitmapSource mit Hilfe eines TiffBitmapEncoderin einem TIFF-Bild codiert werden kann.
int width = 128;
int height = width;
int stride = width / 8;
array<System::Byte>^ pixels = gcnew array<System::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);
FileStream^ stream = gcnew FileStream("new.tif", FileMode::Create);
TiffBitmapEncoder^ encoder = gcnew TiffBitmapEncoder();
TextBlock^ myTextBlock = gcnew TextBlock();
myTextBlock->Text = "Codec Author is: " + encoder->CodecInfo->Author->ToString();
encoder->Compression = TiffCompressOption::Zip;
encoder->Frames->Add(BitmapFrame::Create(image));
encoder->Save(stream);
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);
Dim width As Integer = 128
Dim height As Integer = width
Dim stride As Integer = width / 8
Dim pixels(height * stride - 1) As Byte
' Define the image palette.
Dim myPalette As BitmapPalette = BitmapPalettes.WebPalette
' Creates a new empty image with the pre-defined palette.
Dim image As BitmapSource = BitmapSource.Create( _
width, height, 96, 96, PixelFormats.Indexed1, myPalette, pixels, stride)
Dim stream As New FileStream("new.tif", FileMode.Create)
Dim encoder As New TiffBitmapEncoder()
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString()
encoder.Compression = TiffCompressOption.Zip
encoder.Frames.Add(BitmapFrame.Create(image))
encoder.Save(stream)
Siehe auch
Zusammenarbeit auf GitHub
Die Quelle für diesen Inhalt finden Sie auf GitHub, wo Sie auch Issues und Pull Requests erstellen und überprüfen können. Weitere Informationen finden Sie in unserem Leitfaden für Mitwirkende.
.NET Desktop feedback