C#保存GIF透明的方法

31 篇文章 1 订阅

转换图形为GIF格式,获取GIF的的颜色表。发现只有颜色索引表16的alpha是0 就是透明色。

根据图形的ALPHA为0的情况下 来设置颜色索引表。

使用方式  如果要保存图形为透明把具体位置的ALPHA的值设置为0

  1.     Bitmap _Bitmap24 = (Bitmap)Image.FromFile(@"C:/bfx/24.bmp");
  2.             pictureBox1.Image = ToImageGif(_Bitmap24);
  3.             Bitmap _BitmapGDI = new Bitmap(200, 200);
  4.             Graphics g = Graphics.FromImage(_BitmapGDI);
  5.             g.DrawString("ASDF"new Font("宋体", 20), Brushes.Yellow, new Point(0, 0));
  6.             pictureBox2.Image = ToImageGif(_BitmapGDI);
  7.             pictureBox2.Image.Save(@"c:/1.GIF", ImageFormat.Gif);

把具体方法

 

  1.   private static Bitmap ToImageGif(Bitmap MyBitMap)
  2.         {
  3.             int _Width = MyBitMap.Width;
  4.             int _Height = MyBitMap.Height;
  5.           
  6.             System.IO.MemoryStream _MemoryStream = new MemoryStream();
  7.             MyBitMap.Save(_MemoryStream, ImageFormat.Gif);
  8.             Bitmap _MyBitMap256 = (Bitmap)Image.FromStream(_MemoryStream);  //保存成GIF 256色
  9.             _MemoryStream.Dispose();            
  10.             //获取颜色索引
  11.             System.Drawing.Imaging.ColorPalette _ColorPalette = _MyBitMap256.Palette;
  12.             Hashtable _ColorHash = new Hashtable();
  13.             for (int i = 0; i != _ColorPalette.Entries.Length; i++)
  14.             {
  15.                
  16.                 string _Key=_ColorPalette.Entries[i].R.ToString("X02")+_ColorPalette.Entries[i].G.ToString("X02")+_ColorPalette.Entries[i].B.ToString("X02");
  17.                 
  18.                 if(_ColorHash[_Key]==null)_ColorHash.Add(_Key, i);
  19.             }
  20.             
  21.             建立新的图形
  22.             Bitmap _Newbitmap = new Bitmap(_Width, _Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
  23.             _Newbitmap.Palette = _ColorPalette;  //设置索引
  24.             Rectangle _Rect = new Rectangle(0, 0, _Width, _Height);
  25.             //获取IMAGE数据
  26.             System.Drawing.Imaging.BitmapData _BitmapData = _Newbitmap.LockBits(_Rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
  27.             byte[] _Bits = new byte[_Width * _Height];
  28.             Int32 _BitsInt = 0;
  29.             if (_BitmapData.Stride > 0)
  30.             {
  31.                 _BitsInt = _BitmapData.Scan0.ToInt32();
  32.             }
  33.             else
  34.             {
  35.                 _BitsInt = _BitmapData.Scan0.ToInt32() + _BitmapData.Stride * (_Height - 1);
  36.             }
  37.             int _Stride = Math.Abs(_BitmapData.Stride);
  38.             for (int i = 0; i != _Height; i++)
  39.             {
  40.                 for (int y = 0; y != _Width; y++)
  41.                 {
  42.                     int _8BppPixel = i * _Stride + y;
  43.                     Color _TempColor=_MyBitMap256.GetPixel(y, i);
  44.                     int _Alpha = MyBitMap.GetPixel(y, i).A;
  45.                     if (_Alpha == 0)
  46.                     {
  47.                         _Bits[_8BppPixel] = (byte)16;
  48.                     }
  49.                     else
  50.                     {
  51.                         string _Key = _TempColor.R.ToString("X02") + _TempColor.G.ToString("X02") + _TempColor.B.ToString("X02");
  52.                         object _ValueIndex = _ColorHash[_Key];
  53.                         if (_ValueIndex == null) MessageBox.Show("Error");
  54.                         _Bits[_8BppPixel] = (byte)((int)_ValueIndex);
  55.                     }
  56.               
  57.                 }
  58.             }
  59.             CopyArrayTo(_BitsInt, _Bits, _Height * _Stride);
  60.             _Newbitmap.UnlockBits(_BitmapData);
  61.             return _Newbitmap;
  62.         }
  63.        
  64.         [DllImport("KERNEL32.DLL", EntryPoint = "RtlMoveMemory", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  65.         public static extern void CopyArrayTo([In(), MarshalAs(UnmanagedType.I4)] Int32 hpvDest, [In(), Out()]byte[] hpvSource, int cbCopy);
  66.        

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值