Example #1
0
        /// <summary>
        /// Generates the BMP icon image data for this image.
        /// </summary>
        /// <returns>The BMP icon image data for this image.</returns>
        public byte[] GetData()
        {
            BitmapSource result = null;

            if (GenerateTransparencyMap)
            {
                // The transparency map has to be above the actual image, so we just copy the original image's data at the bottom
                // All bytes before that will be set to 0, which is what we want (0 == Visible)
                byte[] buffer = new byte[OriginalImage.PixelWidth * OriginalImage.PixelHeight * 4 * 2];
                OriginalImage.CopyPixels(buffer, OriginalImage.PixelWidth * 4, buffer.Length / 2);

                result = BitmapSource.Create(
                    OriginalImage.PixelWidth, OriginalImage.PixelHeight * 2,
                    OriginalImage.DpiX, OriginalImage.DpiY,
                    OriginalImage.Format,
                    OriginalImage.Palette,
                    buffer,
                    OriginalImage.PixelWidth * 4);
            }
            else
            {
                result = OriginalImage;
            }

            using (var stream = new MemoryStream())
            {
                var encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(result));
                encoder.Save(stream);

                // Remove the BMPFILEHEADER, turning it into a Memory BMP.
                return(stream.GetBuffer().Skip(BmpFileHeaderSize).ToArray());
            }
        }