共用方式為


如何:編碼和解碼 BMP 影像

以下範例示範如何使用特定的 BmpBitmapDecoderBmpBitmapEncoder 物件來對點陣圖 (BMP) 影像進行解碼和編碼。

解碼 BMP 影像

此範例會示範如何使用 Uri 中的 BmpBitmapDecoder 來解碼 BMP 影像。


// Open a Uri and decode a BMP image
System::Uri^ myUri = gcnew System::Uri("tulipfarm.bmp", UriKind::RelativeOrAbsolute);
BmpBitmapDecoder^ decoder2 = gcnew BmpBitmapDecoder(myUri, BitmapCreateOptions::PreservePixelFormat, BitmapCacheOption::Default);
BitmapSource^ bitmapSource2 = decoder2->Frames[0];

// Draw the Image
Image^ myImage2 = gcnew Image();
myImage2->Source = bitmapSource2;
myImage2->Stretch = Stretch::None;
myImage2->Margin = System::Windows::Thickness(20);

// Open a Uri and decode a BMP image
Uri myUri = new Uri("tulipfarm.bmp", UriKind.RelativeOrAbsolute);
BmpBitmapDecoder decoder2 = new BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource2 = decoder2.Frames[0];

// Draw the Image
Image myImage2 = new Image();
myImage2.Source = bitmapSource2;
myImage2.Stretch = Stretch.None;
myImage2.Margin = new Thickness(20);
' Open a Uri and decode a BMP image
Dim myUri As New Uri("tulipfarm.bmp", UriKind.RelativeOrAbsolute)
Dim decoder2 As New BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
Dim bitmapSource2 As BitmapSource = decoder2.Frames(0)

' Draw the Image
Dim myImage2 As New Image()
myImage2.Source = bitmapSource2
myImage2.Stretch = Stretch.None
myImage2.Margin = New Thickness(20)

編碼 BMP 影像

此範例會示範如何使用 BmpBitmapEncoderBitmapSource 編碼為 BMP 影像。

int width = 128;
int height = width;
int stride = width / 8;
array<System::Byte>^ pixels = gcnew array<System::Byte>(height * stride);

List<Color>^ colors = gcnew List<Color>();
colors->Add(Colors::Red);
colors->Add(Colors::Blue);
colors->Add(Colors::Green);
BitmapPalette^ myPalette = gcnew BitmapPalette(colors);

// 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.bmp", FileMode::Create);
   BmpBitmapEncoder^ encoder = gcnew BmpBitmapEncoder();
   TextBlock^ myTextBlock = gcnew TextBlock();
   myTextBlock->Text = "Codec Author is: " + encoder->CodecInfo->Author->ToString();
   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];

// Try creating a new image with a custom palette.
List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
colors.Add(System.Windows.Media.Colors.Red);
colors.Add(System.Windows.Media.Colors.Blue);
colors.Add(System.Windows.Media.Colors.Green);
BitmapPalette myPalette = new BitmapPalette(colors);

// 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 = new FileStream("new.bmp", FileMode.Create);
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
TextBlock myTextBlock = new TextBlock();
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString();
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);

Dim width As Integer = 128
Dim height As Integer = width
Dim stride As Integer = CType(width / 8, Integer)
Dim pixels(height * stride) As Byte

' Try creating a new image with a custom palette.
Dim colors As New List(Of System.Windows.Media.Color)()
colors.Add(System.Windows.Media.Colors.Red)
colors.Add(System.Windows.Media.Colors.Blue)
colors.Add(System.Windows.Media.Colors.Green)
Dim myPalette As New BitmapPalette(Colors)

' Creates a new empty image with the pre-defined palette
Dim image As BitmapSource = System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96, PixelFormats.Indexed1, myPalette, pixels, stride)
Dim stream As New FileStream("new.bmp", FileMode.Create)
Dim encoder As New BmpBitmapEncoder()
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString()
encoder.Frames.Add(BitmapFrame.Create(image))
encoder.Save(stream)

另請參閱