How to: Encode and Decode a BMP Image

 

The following examples show how to decode and encode a Entity with relative path '../Token/TLA%23tla_bmp_md.md' can not be found, for source topic '{"project_id":"3fedad16-eaf1-41a6-8f96-0c1949c68f32","entity_id":"feb5ef27-28ac-40ab-bfc2-e0456990d32c","entity_type":"Article","locale":"en-US"}'. image using the specific BmpBitmapDecoder and BmpBitmapEncoder objects.

This example demonstrates how to decode a Entity with relative path '../Token/TLA2%23tla_bmp_md.md' can not be found, for source topic '{"project_id":"3fedad16-eaf1-41a6-8f96-0c1949c68f32","entity_id":"feb5ef27-28ac-40ab-bfc2-e0456990d32c","entity_type":"Article","locale":"en-US"}'. image using a BmpBitmapDecoder from a Uri.


            // 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);

This example demonstrates how to encode a BitmapSource into a Entity with relative path '../Token/TLA2%23tla_bmp_md.md' can not be found, for source topic '{"project_id":"3fedad16-eaf1-41a6-8f96-0c1949c68f32","entity_id":"feb5ef27-28ac-40ab-bfc2-e0456990d32c","entity_type":"Article","locale":"en-US"}'. image using a BmpBitmapEncoder.

            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);

Imaging Overview

Show: