示例#1
0
        public void MergeChannels()
        {
            // no alpha channel
            if (AlphaData == null && AlphaBitmap == null && ColorData != null)
            {
                if (ColorData.StartsWith(Format.Png.Signature) || ColorData.StartsWith(Format.Bmp.Signature))
                {
                    return;
                }
            }

            Decode();
            if (ColorBitmap != null && ColorBitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                // make sure cb actually has an alpha channel to copy to
                var newColor = new Bitmap(ColorBitmap.Width, ColorBitmap.Height, PixelFormat.Format32bppArgb);
                using (var gr = System.Drawing.Graphics.FromImage(newColor)) {
                    gr.DrawImage(ColorBitmap, new Rectangle(0, 0, ColorBitmap.Width, ColorBitmap.Height));
                }

                ColorBitmap.Dispose();
                ColorBitmap = newColor;
            }

            BitmapUtils.CopyAlphaChannel(AlphaBitmap, ColorBitmap);
            AlphaBitmap?.Dispose();
            AlphaBitmap = null;
        }
示例#2
0
 // TODO call dispose
 public void Dispose()
 {
     ColorBitmap?.Dispose();
     AlphaBitmap?.Dispose();
     GC.SuppressFinalize(this);
 }
示例#3
0
        public bool Encode(AlphaMode am = AlphaMode.Discard, ColorMode cm = ColorMode.MergePng)
        {
            if (IsEncoded)
            {
                return(false);
            }

            Debug.Assert(ColorData == null);
            Debug.Assert(AlphaData == null);

            if (cm == ColorMode.MergePng || cm == ColorMode.MergeGnp)
            {
                MergeChannels();
            }

            // encode color data
            var buffer = new MemoryStream();

            switch (cm)
            {
            case ColorMode.MergePng:
            case ColorMode.Png:
                ColorBitmap.Save(buffer, ImageFormat.Png);
                break;

            case ColorMode.MergeGnp:
            case ColorMode.Gnp:
                ColorBitmap.Save(buffer, ImageFormat.Png);
                buffer.Seek(0).WriteBytes(Format.Gnp.Signature);
                break;

            case ColorMode.Bmp:
                ColorBitmap.Save(buffer, ImageFormat.Bmp);
                break;
            }

            ColorData = buffer.Length > 0 ? buffer.ToArray() : null;

            // encode alpha data
            buffer.Seek(0).SetLength(0);
            switch (am)
            {
            case AlphaMode.Png:
                ColorBitmap.Save(buffer, ImageFormat.Png);
                break;

            case AlphaMode.Gnp:
                ColorBitmap.Save(buffer, ImageFormat.Png);
                buffer.Seek(0).WriteBytes(Format.Gnp.Signature);
                break;

            case AlphaMode.Bmp:
                ColorBitmap.Save(buffer, ImageFormat.Bmp);
                break;
            }

            AlphaData = buffer.Length > 0 ? buffer.ToArray() : null;

            ColorBitmap?.Dispose();
            AlphaBitmap?.Dispose();
            ColorBitmap = null;
            AlphaBitmap = null;

            return(true);
        }