C#制作透明色GIF动画的类

利用C#2005为你的软件制作小精灵 利用C#2005为你的软件制作小精灵 我们平时在使用软件时,有时会看到一些软件附带的小精灵,如:瑞星杀毒软件的狮子,OFFICE系列软件中的孙悟空,太白金星等。这些小精灵,动作夸张,滑稽可爱,又能附带软件的某些主要功能,可以说:占“地”少,功能大,大大方便了软件的操作。如果我们也为自己设计的软件加上小精灵,那将会令我们的软件增色无穷。本文将从制作原理和制作过程两部分的内容来讲述利用C# 阅读详情

前几天,关注了下GIF的文件结构

发现一个GIF文件大小错很多.发现主要是3个地方的问题.

一是色彩表的问题.GIF有全局色彩表和图形色彩表.

二是GIF图形的大小并不一定是一样的 桢1可能是100*100的 桢2可能是10*10 比如天空是黑色100*100 星星在是10*10

三是描述区,没什么作用就是描述

 

图形一样大小不一样的就这两个地方区别大.

 

用C#来实现下

并没用LZW是通过.NET来实现LZW的.这里只是进行了文件头的更改和合并.

如果你用这个类打开一个GIF 你通过SAVE方法再保存一次 描述类会放到图形类的后面.

 

使用方式

  1.        Zgke.MyImage.ImageGif _Gif = new Zgke.MyImage.ImageGif(20, 32);
  2.             int _Temp =(int)'A';
  3.             for (int i = 0; i != 26; i++)
  4.             {
  5.                 Bitmap _BitMap = new Bitmap(20, 32);
  6.                 Graphics _G = Graphics.FromImage(_BitMap);
  7.                 char T = (char)_Temp;
  8.                 _G.DrawString(T.ToString(), new Font("宋体", 20), Brushes.Black, 0, 0);
  9.                 _Gif.AddImage(_BitMap, 1, true, Zgke.MyImage.ImageGif.DisposalMethod.NoDisposalImage);
  10.                 _G.Dispose();
  11.                 _Temp++;
  12.                 _BitMap.Dispose();
  13.             }
  14.             
  15.             pictureBox1.Image = _Gif.Image;
  16.             _Gif.SaveFile(@"C:/1.gif");

 

下面是全部的类

 

 

 

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
namespace Zgke.MyImage
{
    /// <summary>
    /// GIF操作类
    /// zgke@sina.com
    /// QQ:116149
    /// </summary>
    public class ImageGif
    {   
        private Header m_Header;
        private LogicalScreen m_LogicalScreen;
        private ColorTable m_GlobalColorTable;
        private IList<ExtensionIntroducer> m_ExtensionIntroducer = new List<ExtensionIntroducer>();
        private ApplicationExtension m_ApplicationExtension;
        private IList<GraphicControlExtension> m_GraphicControlExtension = new List<GraphicControlExtension>();
        /// <summary>
        /// 当前位置
        /// </summary>
        private int m_Index = 0;
        private bool m_Open = false;
        /// <summary>
        /// 是否正常打开
        /// </summary>
        public bool OpenOK { get { return m_Open; } }
        /// <summary>
        /// 图形数量
        /// </summary>
        public int ImageCount { get { return m_GraphicControlExtension.Count; } }
        /// <summary>
        /// 获取GIF图形
        /// </summary>
        public Image Image
        {
            get
            {
                if (m_GraphicControlExtension.Count == 0) return null;
                MemoryStream _MemoryImage = new MemoryStream();
                _MemoryImage.Position = 0;
                byte[] _Temp = m_Header.GetByte();
                _MemoryImage.Write(_Temp, 0, _Temp.Length);
                _Temp = m_LogicalScreen.GetByte();
                _MemoryImage.Write(_Temp, 0, _Temp.Length);
                _Temp = m_GlobalColorTable.GetByte();
                _MemoryImage.Write(_Temp, 0, _Temp.Length);
                _Temp = m_ApplicationExtension.GetByte();
                _MemoryImage.Write(_Temp, 0, _Temp.Length);
                for (int i = 0; i != m_GraphicControlExtension.Count; i++)  //保存图形
                {
                    _Temp = m_GraphicControlExtension[i].GetByte();
                    _MemoryImage.Write(_Temp, 0, _Temp.Length);
                }
                for (int i = 0; i != m_ExtensionIntroducer.Count; i++)  //保存描述
                {
                    _Temp = m_ExtensionIntroducer[i].GetByte();
                    _MemoryImage.Write(_Temp, 0, _Temp.Length);
                }
                _MemoryImage.WriteByte(0x3B);
                return Image.FromStream(_MemoryImage);
            }           
        }
        /// <summary>
        /// 由文件打开
        /// </summary>
        /// <param name="p_GifFileName"></param>
        public ImageGif(string p_GifFileName)
        {
            System.IO.FileStream _FileStream = new FileStream(p_GifFileName, FileMode.Open);
            byte[] _GifByte = new byte[_FileStream.Length];
            _FileStream.Read(_GifByte, 0, _GifByte.Length);
            _FileStream.Close();
            m_Header = new Header(_GifByte, ref m_Index, ref m_Open);
            if (m_Open == false) return;
            m_LogicalScreen = new LogicalScreen(_GifByte, ref m_Index, ref m_Open);
            if (m_Open == false) return;
            m_GlobalColorTable = new ColorTable(m_LogicalScreen.GlobalPal, m_LogicalScreen.GlobalColorSize, _GifByte, ref m_Index, ref m_Open);
            if (m_Open == false) return;
            //固定位置信息读取完成
            m_Open = false;
            while (true)   //不知道有多少个模块一直循环吧
            {
                #region 判断位置
                switch (_GifByte[m_Index])
                {
                    case 0x21:
                        m_Index += 2;   //坐标移动到类别后面  取类类别用-1方式 少一行代码
                        switch (_GifByte[m_Index - 1])
                        {
                            case 0xFE:
                                m_ExtensionIntroducer.Add(new ExtensionIntroducer(_GifByte, ref m_Index, ref m_Open));
                                if (m_Open == false) return;   //获取失败每必要继续了直接返回
                                break;
                            case 0xFF:
                                m_ApplicationExtension = new ApplicationExtension(_GifByte, ref m_Index, ref m_Open);
                                if (m_Open == false) return;
                                break;
                            case 0xF9:         //图形在这个区 LZW数据
                                m_GraphicControlExtension.Add(new GraphicControlExtension(_GifByte, ref m_Index, ref m_Open));
                                if (m_Open == false) return;
                                break;
                            default:         //找不到类别 直接结束 
                                System.Windows.Forms.MessageBox.Show(_GifByte[m_Index - 1].ToString("X02"));
                                return;
                        }
                        break;
                    case 0x3B:
                        m_Open = true;
                        return;
                    default:
                        m_Open = false;
                        System.Windows.Forms.MessageBox.Show(_GifByte[m_Index].ToString("X02"), m_Index.ToString());
                        return;
                }
                #endregion
            }
        }
        /// <summary>
        /// 建立新的图形
        /// </summary>
        /// <param name="p_Width"></param>
        /// <param name="p_Height"></param>
        public ImageGif(ushort p_Width, ushort p_Height)
        {
            m_Header = new Header();
            m_LogicalScreen = new LogicalScreen(p_Width, p_Height);
            m_ApplicationExtension = new ApplicationExtension();
            m_ExtensionIntroducer.Add(new ExtensionIntroducer("http://blog.csdn.net/zgke"));  //这东西可以很多个 描述就是描述 没发现什么作用
           
        }
        #region 添加图形 删除 插入
        /// <summary>
        /// 添加一个图形  
        /// </summary>
        /// <param name="Image">图形</param>
        /// <param name="p_DelayTime">显示时间</param>
        /// <param name="p_UserColorTable">是否使用全局色彩表 true使用 false 不使用</param>
        private GraphicControlExtension GetGraphicsControl(Image p_Image, ushort p_DelayTime, bool p_UserColorTable, DisposalMethod p_Disposal)
        {
           
            MemoryStream _MemOry = new MemoryStream();
            Image _Image = ToImageGif((Bitmap)p_Image, Color.White);   //透明的方法可以加到这里
            _Image.Save(_MemOry, ImageFormat.Gif);        //保存成GIF图形
            int _Index = 783;   //开始位置781 0x21 0xF9 已经计算
            bool _Open = false;
            GraphicControlExtension _Graphics = new GraphicControlExtension(_MemOry.GetBuffer(), ref _Index, ref _Open);
            _Graphics.DelayTime = p_DelayTime;
            _Graphics.TransparentColorFlag = true;
            _Graphics.TransparentColorIndex = 16;
            ColorTable _Table = new ColorTable();
            Image _AddImage = Image.FromStream(_MemOry);  //获取压缩后的图形
            for (int i = 0; i != _AddImage.Palette.Entries.Length; i++)      //获取图形颜色表
            {
                _Table.ColorTableList.Add(_AddImage.Palette.Entries[i]);
            }
            if (m_GlobalColorTable == null) m_GlobalColorTable = _Table;     //不管怎么样都要全局色彩表
            if (p_UserColorTable == false)
            {
                _Graphics.ColorTable = _Table;      //根据需要设置全局颜色表              
                _Graphics.UserColorTable = !p_UserColorTable;
            }
            _Graphics.Disposal = p_Disposal;
            return _Graphics;
        }
        /// <summary>
        /// 添加一个图形 图形可以大于虚拟屏幕 就是显示不出来而已........
        /// </summary>
        /// <param name="Image">图形</param>
        /// <param name="p_DelayTime">显示时间</param>
        /// <param name="p_UserColorTable">是否使用全局色彩表 true使用 false 不使用</param>
        public void AddImage(Image p_Image, ushort p_DelayTime, bool p_UserColorTable, DisposalMethod p_Disposal)
        {
            GraphicControlExtension _Info = GetGraphicsControl(p_Image, p_DelayTime, p_UserColorTable, p_Disposal);
            if (_Info != null) m_GraphicControlExtension.Add(_Info);
        }
        /// <summary>
        /// 删除一个图形
        /// </summary>
        /// <param name="p_Index"></param>
        public void RemoveImage(int p_Index)
        {
            if (p_Index > m_GraphicControlExtension.Count - 1) return;
            m_GraphicControlExtension.RemoveAt(p_Index);
        }
        /// <summary>
        /// 插入一个图形
        /// </summary>
        /// <param name="Image">图形</param>
        /// <param name="p_DelayTime">显示时间</param>
        /// <param name="p_UserColorTable">是否使用全局色彩表 true使用 false 不使用</param>
        public void InsertImage(Image p_Image, ushort p_DelayTime, bool p_UserColorTable, int p_Index, DisposalMethod p_Disposal)
        {
            if (p_Index > m_GraphicControlExtension.Count - 1) return;
            GraphicControlExtension _Info = GetGraphicsControl(p_Image, p_DelayTime, p_UserColorTable, p_Disposal);
            if (_Info != null) m_GraphicControlExtension.Insert(p_Index, _Info);
        }
        #endregion
        /// <summary>
        /// 设置一个图形播放的时间
        /// </summary>
        /// <param name="p_Index">索引</param>
        /// <param name="p_DelayTime">播放时间</param>
        public void SetImageTime(int p_Index, ushort p_DelayTime)
        {
            if (p_Index > m_GraphicControlExtension.Count - 1) return;
            m_GraphicControlExtension[p_Index].DelayTime = p_DelayTime;
        }
        /// <summary>
        /// 设置一个图形开始位置
        /// </summary>
        /// <param name="p_Index">索引</param>
        /// <param name="p_X">X坐标</param>
        /// <param name="p_Y">Y坐标</param>
        public void SetImageLocatch(int p_Index, Point p_Point)
        {
            if (p_Index > m_GraphicControlExtension.Count - 1) return;
            m_GraphicControlExtension[p_Index].Point = p_Point;
        }
        /// <summary>
        /// 设置处理模式
        /// </summary>
        /// <param name="p_Index">索引</param>
        /// <param name="p_DisposalMethod">处理模式</param>
        public void SetImageDisposalMethod(int p_Index, DisposalMethod p_DisposalMethod)
        {
            if (p_Index > m_GraphicControlExtension.Count - 1) return;
            m_GraphicControlExtension[p_Index].Disposal = p_DisposalMethod;
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="p_FileName"></param>
        public void SaveFile(string p_FileName)
        {
            if (m_GraphicControlExtension.Count == 0) return ;
            FileStream _File = new FileStream(p_FileName, FileMode.Create);
            _File.Position = 0;
            byte[] _Temp = m_Header.GetByte();
            _File.Write(_Temp, 0, _Temp.Length);
            _Temp = m_LogicalScreen.GetByte();
            _File.Write(_Temp, 0, _Temp.Length);
            _Temp = m_GlobalColorTable.GetByte();
            _File.Write(_Temp, 0, _Temp.Length);
            _Temp = m_ApplicationExtension.GetByte();
            _File.Write(_Temp, 0, _Temp.Length);
            for (int i = 0; i != m_GraphicControlExtension.Count; i++)  //保存图形
            {
                _Temp = m_GraphicControlExtension[i].GetByte();
                _File.Write(_Temp, 0, _Temp.Length);
            }
            for (int i = 0; i != m_ExtensionIntroducer.Count; i++)  //保存描述
            {
                _Temp = m_ExtensionIntroducer[i].GetByte();
                _File.Write(_Temp, 0, _Temp.Length);
            }
            _File.WriteByte(0x3B);
            _File.Close();
        }
        #region 获取图形
        /// <summary>
        /// 内部方法 保存一个图形
        /// </summary>
        /// <param name="m_Stream">数据流</param>
        /// <param name="p_Index">图形索引</param>
        /// <returns>图形</returns>
        private Stream GetImage(Stream m_Stream, int p_Index)
        {
            if (p_Index < 0 || p_Index > ImageCount) return null;
            m_Stream.Position = 0;
            byte[] _Temp = m_Header.GetByte();
            m_Stream.Write(_Temp, 0, _Temp.Length);
            _Temp = m_LogicalScreen.GetByte();
            m_Stream.Write(_Temp, 0, _Temp.Length);
            _Temp = m_GlobalColorTable.GetByte();
            m_Stream.Write(_Temp, 0, _Temp.Length);
            _Temp = m_ApplicationExtension.GetByte();
            m_Stream.Write(_Temp, 0, _Temp.Length);
            _Temp = m_GraphicControlExtension[p_Index].GetByte();
            m_Stream.Write(_Temp, 0, _Temp.Length);
            for (int i = 0; i != m_ExtensionIntroducer.Count; i++)  //保存描述
            {
                _Temp = m_ExtensionIntroducer[i].GetByte();
                m_Stream.Write(_Temp, 0, _Temp.Length);
            }
            m_Stream.WriteByte(0x3B);
            return m_Stream;
        }
        /// <summary>
        /// 根据索引获取图形
        /// </summary>
        /// <param name="p_Index">索引</param>
        /// <returns>图形</returns>
        public Image GetImage(int p_Index)
        {
            MemoryStream _Memory = new MemoryStream();
            return Image.FromStream(GetImage(_Memory, p_Index));
        }
        /// <summary>
        /// 根据索引获取图形
        /// </summary>
        /// <param name="p_Index">索引</param>
        /// <returns>图形</returns>
        public void GetImage(int p_Index, string p_FileName)
        {
            FileStream _File = new FileStream(p_FileName, FileMode.Create);
            GetImage(_File, p_Index);
            _File.Close();
        }
        #endregion
        /// <summary>
        /// 处理模式
        /// </summary>
        public enum DisposalMethod
        {
            /// <summary>
            /// 不使用处置方法 一般的GIF都是这个
            /// </summary>
            NoDisposalMethod = 0,
            /// <summary>
            /// 不处置图形,把图形从当前位置移去
            /// </summary>
            NoDisposalImage = 1,
            /// <summary>
            /// 回复到背景色
            /// </summary>
            RestoreBackgroundColor = 2,
            /// <summary>
            /// 回复到先前状态
            /// </summary>
            RestoreFrontState = 3,
            /// <summary>
            /// 为定义4-7
            /// </summary>
            Null
        }
        #region 类定义
        /// <summary>
        /// GIF文件头
        /// </summary>
        private class Header
        {
            /// <summary>
            ///文件头 必须是GIF
            /// </summary>
            private byte[] _Signature = new byte[] { 0x47, 0x49, 0x46 };
            /// <summary>
            /// 版本信息 
            /// </summary>
            private byte[] _Version = new byte[] { 0x38, 0x39, 0x61 };
            public Header(byte[] p_Byte, ref int p_Index, ref bool p_Open)
            {
                p_Open = false;
                if (p_Byte[0] != _Signature[0] || p_Byte[1] != _Signature[1] || p_Byte[2] != _Signature[2])
                {
                    return;
                }
                _Version[0] = p_Byte[3];
                _Version[1] = p_Byte[4];
                _Version[2] = p_Byte[5];
                p_Index += 6;
                p_Open = true;
            }
            public Header()
            {
            }
            /// <summary>
            /// 返回版本号 
            /// </summary>
            public string Version
            {
                get
                { return Encoding.ASCII.GetString(_Version); }
                set
                {
                    if (value == "87a") _Version[1] = 0x37;
                }
            }
            public byte[] GetByte()
            {
                byte[] _Temp = new byte[6];
                _Temp[0] = _Signature[0];
                _Temp[1] = _Signature[1];
                _Temp[2] = _Signature[2];
                _Temp[3] = _Version[0];
                _Temp[4] = _Version[1];
                _Temp[5] = _Version[2];
                return _Temp;
            }
        }
        /// <summary>
        /// 逻辑屏幕标识符
        /// </summary>
        private class LogicalScreen
        {
            /// <summary>
            /// 图形宽
            /// </summary>
            private byte[] _Width = new byte[2];
            /// <summary>
            /// 图形高
            /// </summary>
            private byte[] _Height = new byte[2];
            /// <summary>
            /// 第8位 - 全局颜色列表标志(Global Color Table Flag),当置位时表示有全局颜色列表,pixel值有意义.
            /// 第5位 第6位 第7位 颜色深度(Color ResoluTion),cr+1确定图象的颜色深度.
            /// 第4位 分类标记 如果置位表示全局颜色列表分类排列.
            /// 第1位 第2位 第3位 全局颜色列表大小,pixel+1确定颜色列表的索引数(2的pixel+1次方).
            /// </summary>
            private byte[] _BitArray = new byte[1];
            /// <summary>
            /// 背景色索引
            /// </summary>
            private byte _Blackground = 0;
            /// <summary>
            /// 像素宽高比
            /// </summary>
            private byte _PixelAspectRadio = 0;
            public LogicalScreen(byte[] p_Byte, ref int p_Index, ref bool p_Open)
            {
                p_Open = false;
                _Width[0] = p_Byte[p_Index];
                _Width[1] = p_Byte[p_Index + 1];
                _Height[0] = p_Byte[p_Index + 2];
                _Height[1] = p_Byte[p_Index + 3];
                _BitArray[0] = p_Byte[p_Index + 4];
                _Blackground = p_Byte[p_Index + 5];
                _PixelAspectRadio = p_Byte[p_Index + 6];
                p_Index += 7;
                p_Open = true;
            }
            public LogicalScreen(ushort p_Width, ushort p_Height)
            {
                Width = p_Width;
                Height = p_Height;
                _Blackground = 0;
                _PixelAspectRadio = 0;
                _BitArray[0] = 135;
            }
            /// <summary>
            /// 高
            /// </summary>
            public ushort Width { get { return BitConverter.ToUInt16(_Width, 0); } set { _Width = BitConverter.GetBytes(value); } }
            /// <summary>
            /// 宽
            /// </summary>
            public ushort Height { get { return BitConverter.ToUInt16(_Height, 0); } set { _Height = BitConverter.GetBytes(value); } }
            /// <summary>
            /// 背景索引
            /// </summary>
            public byte Blackground { get { return _Blackground; } set { _Blackground = value; } }
            /// <summary>
            /// 像素宽高比
            /// </summary>
            public byte PixelAspectRadio { get { return _PixelAspectRadio; } set { _PixelAspectRadio = value; } }
            /// <summary>
            /// 全局颜色列表标志
            /// </summary>
            public bool GlobalPal
            {
                get
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    return _BitList[7];
                }
                set
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    _BitList[7] = value;
                    _BitList.CopyTo(_BitArray, 0);
                }
            }
            /// <summary>
            /// RGB颜色值是否按照使用率进行从高到底的 次序排序的
            /// </summary>
            public bool SortFlag
            {
                get
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    return _BitList[3];
                }
                set
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    _BitList[3] = value;
                    _BitList.CopyTo(_BitArray, 0);
                }
            }
            /// <summary>
            /// 色彩表数量
            /// </summary>
            public int GlobalColorSize
            {
                get
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    BitArray _Bit = new BitArray(3);
                    _Bit[0] = _BitList[0];
                    _Bit[1] = _BitList[1];
                    _Bit[2] = _BitList[2];
                    byte[] _Count = new byte[1];
                    _Bit.CopyTo(_Count, 0);
                    return (int)Math.Pow(2, _Count[0] + 1);
                }
            }
            /// <summary>
            /// 彩色分辨率
            /// </summary>
            public byte ColorResolution
            {
                get
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    BitArray _Bit = new BitArray(3);
                    _Bit[0] = _BitList[4];
                    _Bit[1] = _BitList[5];
                    _Bit[2] = _BitList[6];
                    byte[] _Count = new byte[1];
                    _Bit.CopyTo(_Count, 0);
                    return _Count[0];
                }
            }
            public byte[] GetByte()
            {
                byte[] _Temp = new byte[7];
                _Temp[0] = _Width[0];
                _Temp[1] = _Width[1];
                _Temp[2] = _Height[0];
                _Temp[3] = _Height[1];
                _Temp[4] = _BitArray[0];
                _Temp[5] = _Blackground;
                _Temp[6] = _PixelAspectRadio;
                return _Temp;
            }
        }
        /// <summary>
        /// 色彩表
        /// </summary>
        private class ColorTable
        {
            private IList<Color> _ColorTable = new List<Color>();
            public ColorTable(bool p_GlobalPal, int p_ColorNumb, byte[] p_Byte, ref int p_Index, ref bool p_Open)
            {
                p_Open = false;
                if (p_GlobalPal)
                {
                    for (int i = 0; i != p_ColorNumb; i++)
                    {
                        _ColorTable.Add(Color.FromArgb((int)p_Byte[p_Index], (int)p_Byte[p_Index + 1], (int)p_Byte[p_Index + 2]));
                        p_Index += 3;
                    }
                }
                p_Open = true;
            }
            public ColorTable()
            {
            }
            /// <summary>
            /// 获取全局色彩表
            /// </summary>
            /// <returns></returns>
            public IList<Color> ColorTableList { get { return _ColorTable; } set { _ColorTable = value; } }
            public byte[] GetByte()
            {
                byte[] _Temp = new byte[_ColorTable.Count * 3];
                for (int i = 0; i != _ColorTable.Count; i++)
                {
                    _Temp[(i * 3)] = (byte)_ColorTable[i].R;
                    _Temp[(i * 3) + 1] = (byte)_ColorTable[i].G;
                    _Temp[(i * 3) + 2] = (byte)_ColorTable[i].B;
                }
                return _Temp;
            }
        }
        /// <summary>
        /// 注释~~~
        /// </summary>
        private class ExtensionIntroducer
        {
            private byte[] _Header = new byte[] { 0x21, 0xFE };
            private MemoryStream _Text = new MemoryStream();
            public ExtensionIntroducer(byte[] p_Byte, ref int p_Index, ref bool p_Open)
            {
                p_Open = false;
                while (true)
                {
                    if (p_Index > p_Byte.Length - 1) return;
                    if (p_Byte[p_Index] == 0) break;
                    _Text.WriteByte(p_Byte[p_Index]);
                    p_Index++;
                }
                _Text.WriteByte(0);
                p_Index++;
                p_Open = true;
            }
            public ExtensionIntroducer(string p_Text)
            {
                Text = p_Text;
            }
            public string Text
            {
                get { return Encoding.ASCII.GetString(_Text.GetBuffer()); }
                set { _Text = new MemoryStream(Encoding.ASCII.GetBytes(value)); _Text.WriteByte(0); }
                //设置数据的时候一定要再写个0
            }
            public byte[] GetByte()
            {
                byte[] _Temp = new byte[2 + _Text.Length];
                _Temp[0] = _Header[0];
                _Temp[1] = _Header[1];
                _Text.Position = 0;
                for (int i = 0; i != _Text.Length - 1; i++)
                {
                    _Temp[2 + i] = (byte)_Text.ReadByte();
                }
                return _Temp;
            }
        }
        /// <summary>
        /// 应用程序扩展
        /// </summary>
        private class ApplicationExtension
        {
            private byte[] _Header = new byte[] { 0x21, 0xFF };
            private byte _BlockSize = 0x0B;  //11大小
            private byte[] _Identifier = new byte[8]; //8个ASCII
            private byte[] _AuthenticationCode = new byte[3]; //3个ASCII
            private MemoryStream _Data = new MemoryStream();
            public ApplicationExtension(byte[] p_Byte, ref int p_Index, ref bool p_Open)
            {
                p_Open = false;
                _BlockSize = p_Byte[p_Index];
                _Identifier[0] = p_Byte[p_Index + 1];
                _Identifier[1] = p_Byte[p_Index + 2];
                _Identifier[2] = p_Byte[p_Index + 3];
                _Identifier[3] = p_Byte[p_Index + 4];
                _Identifier[4] = p_Byte[p_Index + 5];
                _Identifier[5] = p_Byte[p_Index + 6];
                _Identifier[6] = p_Byte[p_Index + 7];
                _Identifier[7] = p_Byte[p_Index + 8];
                _AuthenticationCode[0] = p_Byte[p_Index + 9];
                _AuthenticationCode[1] = p_Byte[p_Index + 10];
                _AuthenticationCode[2] = p_Byte[p_Index + 11];
                p_Index += 12;
                while (true)
                {
                    if (p_Index + 1 > p_Byte.Length - 1) return;
                    if (p_Byte[p_Index] == 0 && p_Byte[p_Index + 1] != 0) break;
                    _Data.WriteByte(p_Byte[p_Index]);
                    p_Index++;
                }
                _Data.WriteByte(0);
                p_Index++;
                p_Open = true;
            }
            public ApplicationExtension()
            {
                _Identifier[0] = 0x4E;
                _Identifier[1] = 0x45;
                _Identifier[2] = 0x54;
                _Identifier[3] = 0x53;
                _Identifier[4] = 0x43;
                _Identifier[5] = 0x41;
                _Identifier[6] = 0x50;
                _Identifier[7] = 0x45;
                _AuthenticationCode[0] = 0x32;
                _AuthenticationCode[1] = 0x2E;
                _AuthenticationCode[2] = 0x30;
                _Data.Position = 0;
                _Data.WriteByte(3);
                _Data.WriteByte(1);
                _Data.WriteByte(232);
                _Data.WriteByte(3);
                _Data.WriteByte(0);
            }
            public byte[] GetByte()
            {
                byte[] _Temp = new byte[14 + _Data.Length];
                _Temp[0] = _Header[0];
                _Temp[1] = _Header[1];
                _Temp[2] = _BlockSize;
                _Temp[3] = _Identifier[0];
                _Temp[4] = _Identifier[1];
                _Temp[5] = _Identifier[2];
                _Temp[6] = _Identifier[3];
                _Temp[7] = _Identifier[4];
                _Temp[8] = _Identifier[5];
                _Temp[9] = _Identifier[6];
                _Temp[10] = _Identifier[7];
                _Temp[11] = _AuthenticationCode[0];
                _Temp[12] = _AuthenticationCode[1];
                _Temp[13] = _AuthenticationCode[2];
                _Data.Position = 0;
                for (int i = 0; i != _Data.Length - 1; i++)
                {
                    _Temp[14 + i] = (byte)_Data.ReadByte();
                }
                return _Temp;
            }
        }
        /// <summary>
        /// 图形控制扩展
        /// </summary>
        private class GraphicControlExtension
        {
            private byte[] _Header = new byte[] { 0x21, 0xF9 };
            private byte _BlockSize = 0x4; //4大小
            private byte[] _BitArray = new byte[1];
            private byte[] _DelayTime = new byte[2];
            private byte _TransparentColorIndex = 0;
            private byte _BlockEnd = 0;
            private ImageDescriptor _Image;
            public GraphicControlExtension(byte[] p_Byte, ref int p_Index, ref bool p_Open)
            {
                p_Open = false;
                _BlockSize = p_Byte[p_Index];
                _BitArray[0] = p_Byte[p_Index + 1];
                _DelayTime[0] = p_Byte[p_Index + 2];
                _DelayTime[1] = p_Byte[p_Index + 3];
                _TransparentColorIndex = p_Byte[p_Index + 4];
                _BlockEnd = p_Byte[p_Index + 5];
                p_Index += 6;
                _Image = new ImageDescriptor(p_Byte, ref p_Index, ref p_Open);
            }
            /// <summary>
            /// 设置颜色索引表
            /// </summary>
            public ColorTable ColorTable { get { return _Image.ColorList; } set { _Image.ColorList = value; } }
            /// <summary>
            /// 设置颜色索引表
            /// </summary>
            public bool UserColorTable { get { return _Image.LocalColorTableFlag; } set { _Image.LocalColorTableFlag = value; } }
            /// <summary>
            /// 设置颜色索引表
            /// </summary>
            public int UserColorTableNumb { get { return _Image.PixelNumb; } set { _Image.PixelNumb = value; } }
            /// <summary>
            /// 播放时间
            /// </summary>
            public ushort DelayTime { get { return BitConverter.ToUInt16(_DelayTime, 0); } set { _DelayTime = BitConverter.GetBytes(value); } }
            /// <summary>
            /// 设置透明色
            /// </summary>
            public byte TransparentColorIndex { get { return _TransparentColorIndex; } set { _TransparentColorIndex = value; } }
            /// <summary>
            /// 置位表示使用透明颜色
            /// </summary>
            public bool TransparentColorFlag
            {
                get
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    return _BitList[0];
                }
                set
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    _BitList[0] = value;
                    _BitList.CopyTo(_BitArray, 0);
                }
            }
            /// <summary>
            /// 指出是否期待用户有输入之后才继续进行下去
            /// </summary>
            public bool UseInputFlag
            {
                get
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    return _BitList[1];
                }
                set
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    _BitList[1] = value;
                    _BitList.CopyTo(_BitArray, 0);
                }
            }
            /// <summary>
            /// 处理模式
            /// </summary>
            public DisposalMethod Disposal
            {
                get
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    BitArray _Bit = new BitArray(3);
                    _Bit[0] = _BitList[2];
                    _Bit[1] = _BitList[3];
                    _Bit[2] = _BitList[4];
                    byte[] _Count = new byte[1];
                    _Bit.CopyTo(_Count, 0);
                    switch (_Count[0])
                    {
                        case 1:
                            return DisposalMethod.NoDisposalMethod;
                        case 2:
                            return DisposalMethod.NoDisposalImage;
                        case 3:
                            return DisposalMethod.RestoreBackgroundColor;
                        case 4:
                            return DisposalMethod.RestoreFrontState;
                        default:
                            return DisposalMethod.Null;
                    }
                }
                set
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    bool[] _Value = new bool[3];
                    switch (value)
                    {
                        case DisposalMethod.NoDisposalMethod:
                            _Value[0] = true;
                            break;
                        case DisposalMethod.NoDisposalImage:
                            _Value[1] = true;
                            break;
                        case DisposalMethod.RestoreBackgroundColor:
                            _Value[0] = true;
                            _Value[1] = true;
                            break;
                        case DisposalMethod.RestoreFrontState:
                            _Value[2] = true;
                            break;
                        case DisposalMethod.Null:
                            break;
                    }
                    BitArray _Bit = new BitArray((int)value);
                    _BitList[2] = _Value[0];
                    _BitList[3] = _Value[1];
                    _BitList[4] = _Value[2];
                    byte[] _Count = new byte[1];
                    _BitList.CopyTo(_Count, 0);
                    _BitArray[0] = _Count[0];
                }
            }
            /// <summary>
            /// 获取数据
            /// </summary>
            /// <returns></returns>
            public byte[] GetByte()
            {
                byte[] _ImageByte = _Image.GetByte();
                byte[] _Bytes = new byte[8 + _ImageByte.Length];
                _Bytes[0] = _Header[0];
                _Bytes[1] = _Header[1];
                _Bytes[2] = _BlockSize;
                _Bytes[3] = _BitArray[0];
                _Bytes[4] = _DelayTime[0];
                _Bytes[5] = _DelayTime[1];
                _Bytes[6] = _TransparentColorIndex;
                _Bytes[7] = _BlockEnd;
                _Image.PixelNumb = 256;
                Array.Copy(_ImageByte, 0, _Bytes, 8, _ImageByte.Length);
                return _Bytes;
            }
            public Point Point
            {
                get
                {
                    return new Point(_Image.Left, _Image.Top);
                }
                set
                {
                    _Image.Left = (ushort)value.X;
                    _Image.Top = (ushort)value.Y;
                }
            }
        }
        /// <summary>
        /// 图形区域
        /// </summary>
        private class ImageDescriptor
        {
            private byte[] _Header = new byte[] { 0x2C };
            private byte[] _Left = new byte[2];
            private byte[] _Top = new byte[2];
            private byte[] _Width = new byte[2];
            private byte[] _Height = new byte[2];
            private byte[] _BitArray = new byte[1];
            private ColorTable _ColorTable = new ColorTable();
            private MemoryStream _Lzw = new MemoryStream();
            public ImageDescriptor(byte[] p_Byte, ref int p_Index, ref bool p_Open)
            {
                p_Open = false;
                if (p_Byte[p_Index] != _Header[0]) return;
                _Left[0] = p_Byte[p_Index + 1];   //790
                _Left[1] = p_Byte[p_Index + 2];
                _Top[0] = p_Byte[p_Index + 3];
                _Top[1] = p_Byte[p_Index + 4];
                _Width[0] = p_Byte[p_Index + 5];
                _Width[1] = p_Byte[p_Index + 6];
                _Height[0] = p_Byte[p_Index + 7];
                _Height[1] = p_Byte[p_Index + 8];
                _BitArray[0] = p_Byte[p_Index + 9];  //798
                p_Index += 10;
                _ColorTable = new ColorTable(LocalColorTableFlag, PixelNumb, p_Byte, ref p_Index, ref p_Open);
                _Lzw.WriteByte(p_Byte[p_Index]);
                p_Index++;
                while (true)
                {
                    int _BlockByteSize = p_Byte[p_Index];  //判断后面的块大小
                    _Lzw.WriteByte(p_Byte[p_Index]);  //不管是什么先写入
                    p_Index++;
                    if (_BlockByteSize > 0)
                    {
                        _Lzw.Write(p_Byte, p_Index, _BlockByteSize);
                        p_Index += _BlockByteSize;
                    }
                    else
                    {
                        break;
                    }
                }
                p_Open = true;
            }
            /// <summary>
            /// 局部颜色列表标志
            /// </summary>
            public bool LocalColorTableFlag
            {
                get
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    return _BitList[7];
                }
                set
                {
                    if (value)
                    {
                        PixelNumb = 256;
                    }
                    else
                    {
                        PixelNumb = 0;
                    }
                    BitArray _BitList = new BitArray(_BitArray);
                    _BitList[7] = value;
                    byte[] _Count = new byte[1];
                    _BitList.CopyTo(_Count, 0);
                    _BitArray[0] = _Count[0];
                }
            }
            /// <summary>
            /// 交织标志
            /// </summary>
            public bool InterlaceFlag
            {
                get
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    return _BitList[6];
                }
                set
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    _BitList[6] = value;
                    _BitList.CopyTo(_BitArray, 0);
                }
            }
            /// <summary>
            /// 表示紧跟着的局部颜色列表分类排列
            /// </summary>
            public bool SortFlag
            {
                get
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    return _BitList[5];
                }
                set
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    _BitList[5] = value;
                    _BitList.CopyTo(_BitArray, 0);
                }
            }
            /// <summary>
            /// 颜色标数量
            /// </summary>
            public int PixelNumb
            {
                get
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    BitArray _Bit = new BitArray(3);
                    _Bit[0] = _BitList[0];
                    _Bit[1] = _BitList[1];
                    _Bit[2] = _BitList[2];
                    byte[] _Count = new byte[1];
                    _Bit.CopyTo(_Count, 0);
                    return (int)Math.Pow(2, _Count[0] + 1);
                }
                set
                {
                    BitArray _BitList = new BitArray(_BitArray);
                    byte[] _Count = new byte[1];
                    switch (value)
                    {
                        case 0:
                            _BitList[0] = false;
                            _BitList[1] = false;
                            _BitList[2] = false;
                            _BitList.CopyTo(_Count, 0);
                            _BitArray[0] = _Count[0];
                            break;
                        default:
                            _BitList[0] = true;
                            _BitList[1] = true;
                            _BitList[2] = true;
                            _BitList.CopyTo(_Count, 0);
                            _BitArray[0] = _Count[0];
                            break;
                    }
                }
            }
            /// <summary>
            /// 开始位置X
            /// </summary>
            public ushort Left
            {
                get { return BitConverter.ToUInt16(_Left, 0); }
                set { _Left = BitConverter.GetBytes(value); }
            }
            /// <summary>
            /// 结束位置Y
            /// </summary>
            public ushort Top
            {
                get { return BitConverter.ToUInt16(_Top, 0); }
                set { _Top = BitConverter.GetBytes(value); }
            }
            /// <summary>
            /// 宽
            /// </summary>
            public short Width
            {
                get { return BitConverter.ToInt16(_Width, 0); }
                set { _Width = BitConverter.GetBytes(value); }
            }
            /// <summary>
            /// 高
            /// </summary>
            public short Height
            {
                get { return BitConverter.ToInt16(_Height, 0); }
                set { _Height = BitConverter.GetBytes(value); }
            }
            /// <summary>
            /// 色彩表
            /// </summary>
            public ColorTable ColorList { get { return _ColorTable; } set { _ColorTable = value; } }
            /// <summary>
            /// 色彩表
            /// </summary>
            public MemoryStream LzwData { get { return _Lzw; } set { _Lzw = value; } }
            public byte[] GetByte()
            {
                int _ColorCount = _ColorTable.ColorTableList.Count * 3;
                byte[] _Temp = new byte[10 + _ColorCount + _Lzw.Length];
//这里2009-05-04修改为10  原来的11 图形在IE和QQ的EDIT里不会动.
                _Temp[0] = _Header[0];
                _Temp[1] = _Left[0];
                _Temp[2] = _Left[1];
                _Temp[3] = _Top[0];
                _Temp[4] = _Top[1];
                _Temp[5] = _Width[0];
                _Temp[6] = _Width[1];
                _Temp[7] = _Height[0];
                _Temp[8] = _Height[1];
                _Temp[9] = _BitArray[0];
                int _Index = 10;
                if (PixelNumb != 0)
                {
                    for (int i = 0; i != _ColorTable.ColorTableList.Count; i++)
                    {
                        _Temp[_Index] = (byte)_ColorTable.ColorTableList[i].R;
                        _Temp[_Index + 1] = (byte)_ColorTable.ColorTableList[i].G;
                        _Temp[_Index + 2] = (byte)_ColorTable.ColorTableList[i].B;
                        _Index += 3;
                    }
                }
                _Lzw.Position = 0;
                for (int i = 0; i != _Lzw.Length - 1; i++)
                {
                    _Temp[_Index] = (byte)_Lzw.ReadByte();
                    _Index++;
                }
                return _Temp;
            }
        }
        #endregion
        #region GIF透明处理
        /// <summary>
        /// 转换为GIF透明  保存 Image.Save(@"c:/1.GIF", ImageFormat.Gif); ALPHA 0为透明
        /// </summary>
        /// <param name="p_MyBitMap">原始图形</param>
        /// <param name="p_Transparent">透明色如果默认为黑色</param>
        /// <returns>GIF格式的IMAGE</returns>
        public static Bitmap ToImageGif(Bitmap p_MyBitMap, Color p_Transparent)
        {
           // int _Width = p_MyBitMap.Width;
           // int _Height = p_MyBitMap.Height;
           // System.IO.MemoryStream _MemoryStream = new MemoryStream();
           // p_MyBitMap.Save(_MemoryStream, ImageFormat.Gif);
           // Bitmap _MyBitMap256 = (Bitmap)Image.FromStream(_MemoryStream);  //保存成GIF 256色
           // _MemoryStream.Dispose();
           // System.Drawing.Imaging.ColorPalette _ColorPalette = _MyBitMap256.Palette; //获取颜色索引
           // Hashtable _ColorHash = new Hashtable();
           // for (int i = 0; i != _ColorPalette.Entries.Length; i++) //获取全部色彩
           // {
           //     string _Key = _ColorPalette.Entries[i].R.ToString("X02") + _ColorPalette.Entries[i].G.ToString("X02") + _ColorPalette.Entries[i].B.ToString("X02");
           //     if (_ColorHash[_Key] == null) _ColorHash.Add(_Key, i);
           // }
           //_ColorPalette.Entries[16] = Color.FromArgb(_ColorPalette.Entries[16].A, p_Transparent.R, p_Transparent.G, p_Transparent.B);
           // ////建立新的图形
           // Bitmap _Newbitmap = new Bitmap(_Width, _Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
           // _Newbitmap.Palette = _ColorPalette;  //设置索引
           // Rectangle _Rect = new Rectangle(0, 0, _Width, _Height);
           // //获取IMAGE数据
           // System.Drawing.Imaging.BitmapData _BitmapData = _Newbitmap.LockBits(_Rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
           // Int32 _BitsInt = 0;
           // if (_BitmapData.Stride > 0)
           // {
           //     _BitsInt = _BitmapData.Scan0.ToInt32();
           // }
           // else
           // {
           //     _BitsInt = _BitmapData.Scan0.ToInt32() + _BitmapData.Stride * (_Height - 1);
           // }
           // int _Stride = Math.Abs(_BitmapData.Stride);
           // byte[] _Bits = new byte[_Width * _Height + _Stride];
           // for (int i = 0; i != _Height; i++)
           // {
           //     for (int y = 0; y != _Width; y++)
           //     {
           //         int _8BppPixel = i * _Stride + y;
           //         Color _TempColor = _MyBitMap256.GetPixel(y, i);
           //         int _Alpha = p_MyBitMap.GetPixel(y, i).A;
           //         if (_Alpha == 0)
           //         {
           //             _Bits[_8BppPixel] = (byte)16;   //如果ALPHA==0 设置为透明色
           //         }
           //         else
           //         {
           //             string _Key = _TempColor.R.ToString("X02") + _TempColor.G.ToString("X02") + _TempColor.B.ToString("X02");
           //             object _ValueIndex = _ColorHash[_Key];
           //             if (_ValueIndex == null) _Bits[_8BppPixel] = (byte)16;       //如果色彩不存在设置为透明色
           //             _Bits[_8BppPixel] = (byte)((int)_ValueIndex);        //设置色彩
           //         }
           //     }
           // }
           // Marshal.Copy(_Bits, 0, new IntPtr(_BitsInt), _Height * _Stride);
           // _Newbitmap.UnlockBits(_BitmapData);
           // return _Newbitmap;   
            // 2009-06-03 修改方法 上面的byte计算可能有错误.
            int _Width = p_MyBitMap.Width;
            int _Height = p_MyBitMap.Height;
            System.IO.MemoryStream _MemoryStream = new MemoryStream();
            p_MyBitMap.Save(_MemoryStream, ImageFormat.Gif);
            Bitmap _SaveImage = (Bitmap)Image.FromStream(_MemoryStream);  //保存成GIF 256色
            _MemoryStream.Dispose();
            //获取颜色索引
            System.Drawing.Imaging.ColorPalette _ColorPalette = _SaveImage.Palette;          
            _ColorPalette.Entries[16] = Color.FromArgb(_ColorPalette.Entries[16].A, p_Transparent.R, p_Transparent.G, p_Transparent.B);
            BitmapData _SaveData = _SaveImage.LockBits(new Rectangle(0, 0, _Width, _Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
            byte[] _SaveBytes = new byte[_SaveData.Stride * _SaveData.Height];  //获取保存后的颜色
            Marshal.Copy(_SaveData.Scan0, _SaveBytes, 0, _SaveData.Stride * _SaveData.Height);
            Bitmap _LoadBitmap = new Bitmap(_Width, _Height, PixelFormat.Format32bppArgb);
            Graphics _Graphcis = Graphics.FromImage(_LoadBitmap);
            _Graphcis.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            _Graphcis.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
            _Graphcis.DrawImage(p_MyBitMap, new Rectangle(0, 0, _Width, _Height));
            _Graphcis.Dispose();
            BitmapData _LoadData = _LoadBitmap.LockBits(new Rectangle(0, 0, _Width, _Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            byte[] _LoadByte = new byte[_LoadData.Stride * _SaveData.Height]; //获取土星的原始颜色
            Marshal.Copy(_LoadData.Scan0, _LoadByte, 0, _LoadData.Stride * _LoadData.Height);
            int _ReadIndex = 0;
            int _WriteIndex = 0;
            int _ReadWidthIndex = 0;
            Color _ReadColor = Color.Transparent;
            int _Transparent = p_Transparent.ToArgb();
            for (int i = 0; i != _Height; i++)
            {
                _ReadIndex = i * _LoadData.Stride;
                _WriteIndex = i * _SaveData.Stride;
                for (int z = 0; z != _Width; z++)
                {
                    _ReadWidthIndex = z * 4;
                    _ReadColor = Color.FromArgb(_LoadByte[_ReadIndex + _ReadWidthIndex + 3], _LoadByte[_ReadIndex + _ReadWidthIndex + 2], _LoadByte[_ReadIndex + _ReadWidthIndex + 1], _LoadByte[_ReadIndex + _ReadWidthIndex]);
                    if (_ReadColor.A == 0 || _ReadColor.ToArgb() == _Transparent)
                    {
                        _SaveBytes[_WriteIndex + z] = (byte)16;
                    }
                }
            }
            _LoadBitmap.UnlockBits(_LoadData);
            _LoadBitmap.Dispose();
            Marshal.Copy(_SaveBytes, 0, _SaveData.Scan0, _SaveBytes.Length);
            _SaveImage.UnlockBits(_SaveData);
            _SaveImage.Palette = _ColorPalette;
            return _SaveImage;
  
        }
        #endregion
    }
}

C#实现透明桌面视频播放源代码 本程序是一个简单的WinForm程序,它实现了透明地播放视频,也就是说你在看视频的同时,不影响你对其它应用程序的任何操作。视频数据源可以来自一个视频文件或在线捕捉设备。 立即下载

相关推荐

C#中如何使用Winform实现炫酷的透明动画界面

讲解了如何使用Winform实现炫酷的透明动画界面,Winform相对于Wpf使用更简单一些,系统要求更低,需要了解的朋友可以参考下

C#中保存Gif文件设置透明无效果

在C#中使用Graphics.Clear(Color.Transparent)生成gif格式透明文件时,设置透明不起作用。使用Bitmap.MakeTransparent 方法同样也不能使gif文件透明。这两个问题也是微软的设计使然,和gif文件格式有关系。 如果要在c#中编程实现透明色,必须另写代码,下面是一位老外的代码 /// &lt;summary&gt; /// Make the i...

weixin_34185512的博客 276

芯片硅前验证技术与驱动工程师实战指南

硅前验证是芯片开发流程中的关键环节,通过pre-silicon simulation等技术手段在流片前验证设计正确性。其核心技术包括软件仿真、FPGA原型验证和硬件仿真器解决方案,涉及Linux驱动开发、芯片架构理解和波形分析等核心技能。作为连接芯片设计与系统集成的桥梁,硅前验证能早期发现设计缺陷,节省数百万美元流片成本。以Cadence Palladium为代表的硬件仿真器,通过专用服务器系统实现比纯软件方案快100-1000倍的仿真速度,为驱动工程师提供完整的外设模型验证环境。掌握硅前验证技术可显著提升

weixin_42522857的博客 332

C# Graphics 透明 gif 进度条

先上效果图 上图可以阻塞整个tabPage界面 上图可以阻塞tabPage组件中一个按钮组件 代码使用 代码有偿获取,需要的请私信我

共同进步 485

Vertx之Grpc消息发送

介绍 Vert.x gRPC 模块,它将 Google gRPC 的编程风格与 Vert.x 风格保持一致. gRPC 是一个高性能、开源、通用的RPC框架,是一种进程间通讯技术,由Google推出,基于HTTP2协议标准设计开发,默认采用Protocol Buffers数据序列化协议,支持多种开发语言。gRPC提供了一种简单的方法来精确的定义服务,并且为客户端和服务端自动生成可靠的功能库。 在gRPC客户端可以直接调用不同服务器上的远程程序,使用姿势看起来就像调用本地程序一样,轻松的实现远端服务的连接、

Lance 1196

LabVIEW安装NI-DAQmx的方法(适合2021版和2022版)

LabVIEW安装NI-DAQmx的方法(适合2021版和2022版)

qq_65101190的博客 2万+

C# Winform实现炫酷的透明动画界面

http://www.cnblogs.com/dskin/p/4606293.html 转载于:https://www.cnblogs.com/zuihaodeziji/p/4632501.html

weixin_34107739的博客 732

数字化转型是什么?核心又是什么呢?

01到底什么是数字化转型? 这时候你或许要问笔者了,既然你说使用新兴技术不是数字化转型,那使用新的商业模式总该是数字化转型了吧?你比如阿里巴巴,作为我们中国人最熟悉的开展电子商务的公司,它可以说是新的商业模式最大的开创者了:阿里巴巴是世界上最大的零售商,却没有任何一家实体店和任何库存;再比如优步(Uber),我们非常熟悉的一个线上打车平台,它可以算是世界上最大的出租车公司了,但是它不拥有一辆出租车。还有近些年来,我们经常使用的的旅游房屋租赁社区爱彼迎(Airbnb),它可以为消费者提供多样的住宿信息..

weixin_58342023的博客 4047

C#制作透明色GIF动画的类(转)

发现一个GIF文件大小错很多.发现主要是3个地方的问题. 一是色彩表的问题.GIF有全局色彩表和图形色彩表. 二是GIF图形的大小并不一定是一样的 桢1可能是100*100的 桢2可能是10*10 比如天空是黑色100*100 星星在是10*10 三是描述区,没什么作用就是描述 图形一样大小不一样的就这两个地方区别大. 用C#来实现下 并没用LZW是...

weixin_33743661的博客 749

✨在Windows11上安装虚拟机(VMware17.6.0)保姆级教程(附安装包)

VMware Workstation Pro 是一款功能强大的虚拟机软件,它允许你在单一的物理机上运行多个操作系统。性能卓越:VMware以其出色的性能和稳定性而闻名,适合运行资源密集型的虚拟机。用户友好:直观的用户界面使得即使是初学者也能快速上手。功能全面:支持多种操作系统,包括Windows、Linux、macOS等,满足不同用户的需求。企业级支持:VMware提供全面的技术支持,适合企业级用户。恭喜你成功安装了VMware Workstation Pro 17.6.0!

Python老吕的博客 1万+

ApacheCordova 13 +Vs 2026 项目搭建教程_开发环境搭建教程

本文介绍基于Cordova 13.0.0、Android 13.0.0及JDK 21.0.8的开发环境搭建流程。通过npm全局安装Cordova CLI创建项目,配置JDK与Android SDK环境变量(Java_Home、ANDROID_SDK_ROOT),并设置Gradle 8.14.7路径。使用VS Code配合CordovaTools插件进行开发调试,验证adb devices连接状态。特别说明VS2026中JDK/Sdk位于H:\dev\vsshared\Android目录,适用于.Net MA

天马3798 78

Rufus USB 启动盘制作指南:选镜像、写引导,3 步搞定

Rufus 是一款开源免安装的 USB 格式化工具:插上 U 盘,选好 Windows 或 Linux 的 ISO,点 `START`,格式化与写入一次完成,产出任意电脑都能开机的启动盘。它适合三类人——装系统的 IT 管理员、给老机器装 Win11 的 DIY 用户、需要 Linux 可写盘的开发者。全程三步:插盘选镜像 → 确认分区与文件系统 → 校验后写盘,十分钟不到。 ## 准备工作:先

gitblog_00930的博客 779

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

[C#编程技术源码]1.C# 多图片(照片,截图,样例图,历史图)制作生成动态图片gif的四种完美方案

本文介绍C# 多图片(照片,截图,样例图,历史图)制作生成动态图片gif的四种完美方案。注意,此方法生成时间比较长,必须先收集完图片然后一起生成,不能边收集图片边生成,否则gif速度会飞快,那是因为单帧加入时间太长,收集图片掉帧严重。以上就是c# 制作gif的四种方法的详细内容,更多关于c# 制作gif的资料请关注脚本之家其它相关文章!gifencode.cs代码。

xzs51job的专栏 744

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C# 生成透明Gif图片

C#中生成透明Gif文件 在C#中使用Graphics.Clear(Color.Transparent)生成gif格式透明文件时,设置透明不起作用。使用Bitmap.MakeTransparent 方法同样也不能使gif文件透明。这两个问题也是微软的设计使然,和gif文件格式有关系。如果要在c#中编程实现透明色,必须另写代码,下面是一位老外的代码-->/// /// Make 

Freewind的专栏 3681

Unity高效加载与播放GIF动画:从解码原理到性能优化实战

在实时内容开发中,动画资源的处理是核心环节之一。GIF作为一种广泛使用的动态图像格式,其内部通过多帧图像序列与时间控制信息实现动画效果。然而,Unity引擎的纹理系统默认将其作为静态资源处理,这源于其渲染管线为序列帧、粒子等可控动画形式而优化。因此,在Unity中实现GIF播放,本质是一个解码与适配的过程,需要将GIF的帧数据转换为引擎可识别的纹理序列。这一过程的技术价值在于,它能极大地丰富应用内容,例如在游戏中展示动态表情或UI元素。针对此需求,开发者通常集成如UniGif这类经过验证的第三方库来快速实现

weixin_33850015的博客 432

怎么利用C#创建透明的GIF图片?(可自定义调色板),收藏一下.

http://www.realworldasp.net/article.asp?article=142using System; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Collections; using System.ComponentModel; using Sys

祝紫山(大可山人)博客[GDI+,WPF, .Net图形图像] 5350

C#保存GIF透明的方法

转换图形为GIF格式,获取GIF的的颜色表。发现只有颜色索引表16的alpha是0 就是透明色。根据图形的ALPHA为0的情况下 来设置颜色索引表。使用方式  如果要保存图形为透明把具体位置的ALPHA的值设置为0     Bitmap _Bitmap24 = (Bitmap)Image.FromFile(@"C:/bfx/24.bmp");            pictur

Zgke 6593

Unity播放GIF插件,不使用第三方库,基于文件协议,纯代码实现,兼容移动端和序列帧

本人通过分析GIF的文件协议,分解GIF的各序列帧,然后封装成Unity可使用的Texture,通过递归播放,实现了在Unity上播放GIF的功能,并发布到了AssetStore上面,欢迎各位朋友交流经验。核心源码:分解GIF //处理每个图块 for (int index = 0; index &lt; gif.GraphicControlExten...

程序员养成计划 2962

怎么利用C#创建透明的GIF图片

Code usingSystem; usingSystem.IO; usingSystem.Drawing; usingSystem.Drawing.Imaging; usingSystem.Collections; usingSystem.ComponentModel; usingSystem.Windows.Forms; usingSystem.D...

weixin_30810583的博客 231
上一篇: C# 在运行中改变控件大小的类 并获取最后控件的大小
下一篇: C# WIN服务实现窗体
zgke
博客等级 码龄25年 560粉丝 95原创
评论 25
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值