C#实现图片背景色透明(亲测有效)

重要代码:

Bitmap bm = new Bitmap(origin);
            bm.MakeTransparent();

string Name_ = "C:\\Users\\xx\\Downloads\\download\\" + DateTime.Now.Ticks.ToString() + ".ico";
                    Size size = new Size(256, 64);
                    ConvertImageToIcon(@"C:\Users\xx\Desktop\orgin.jpg", Name_, size);
                    
 /// 
        /// 图片转换为ico文件
        /// 
        /// 原图片路径
        /// 输出ico文件路径
        /// 输出ico图标尺寸,不可大于255x255
        /// 是否转换成功
        public static bool ConvertImageToIcon(string origin, string destination, Size iconSize)
        {
           Bitmap bm = new Bitmap(origin);
            bm.MakeTransparent();
            Image image = new Bitmap(bm, iconSize); //先读取已有的图片为bitmap,并缩放至设定大小
            //Image image = new Bitmap(new Bitmap(origin), iconSize); //先读取已有的图片为bitmap,并缩放至设定大小
            MemoryStream bitMapStream = new MemoryStream(); //存原图的内存流
            MemoryStream iconStream = new MemoryStream(); //存图标的内存流
            image.Save(bitMapStream, ImageFormat.Png); //将原图读取为png格式并存入原图内存流
            BinaryWriter iconWriter = new BinaryWriter(iconStream); //新建二进制写入器以写入目标图标内存流
            /**
             * 下面是根据原图信息,进行文件头写入
             */
            iconWriter.Write((short)0);
            iconWriter.Write((short)1);
            iconWriter.Write((short)1);
            iconWriter.Write((byte)image.Width);
            iconWriter.Write((byte)image.Height);
            iconWriter.Write((short)0);
            iconWriter.Write((short)0);
            iconWriter.Write((short)32);
            iconWriter.Write((int)bitMapStream.Length);
            iconWriter.Write(22);
            //写入图像体至目标图标内存流
            iconWriter.Write(bitMapStream.ToArray());
            //保存流,并将流指针定位至头部以Icon对象进行读取输出为文件
            iconWriter.Flush();
            iconWriter.Seek(0, SeekOrigin.Begin);
            Stream iconFileStream = new FileStream(destination, FileMode.Create);
            Icon icon = new Icon(iconStream);
            icon.Save(iconFileStream); //储存图像
            /**
             * 下面开始释放资源
             */
            iconFileStream.Close();
            iconWriter.Close();
            iconStream.Close();
            bitMapStream.Close();
            icon.Dispose();
            image.Dispose();
            return File.Exists(destination);
        }

你可能感兴趣的:(winform,c#,image)