返回
软件
分类

有时候我们需要在内存中转换Image格式到Icon

日期: 2020-03-13 18:55 浏览次数 : 85

有时候我们需要在内存中转换Image格式到Icon
根据经验,通常我们应该可以这样做
Image image = xxxx;///假设这里已经有一个Image对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

System.IO.MemoryStream mStream = new System.IO.MemoryStream();///创建内存流
image.Save(mStream, System.Drawing.Imaging.ImageFormat.Icon);
Icon icon = Icon.FromHandle(new Bitmap(mStream).GetHicon());
mStream.Close();

namespace Bright.HIS.Data
必赢备用网址 ,{
    public class Icon
    {
        #region Win32 API

但这里在image.Save的时候会报告错误,空对象引用。

        [System.Runtime.InteropServices.DllImport("shell32.dll", EntryPoint = "ExtractAssociatedIcon")]
        private static extern IntPtr ExtractAssociatedIconA(IntPtr hInst,[System.Runtime.InteropServices.MarshalAs(
   System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpIconPath,ref int lpiIcon);

于是我改变保存的格式为Bitmap、Jpeg等,这样就没有错误了(说明不能在内存中直接保存为Icon格式)。
但是这样的话,取得的Icon将会去掉原图片中的透明信息。

        [System.Runtime.InteropServices.DllImport("shell32.dll", EntryPoint = "ExtractIcon")]
        private static extern IntPtr ExtractIconA(IntPtr hInst,[System.Runtime.InteropServices.MarshalAs(
                 System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpszExeFileName,int nIconIndex);

一个个格式的试验我们可以发现保存为Gif格式的时候,还可以保留透明信息,哈哈,终于找到,
但这里,还有值得注意的就是,在成功取得内存流后,不能直接用Icon构造,一定要用Bitmap转,否则也会报错。
Icon icon = new Icon(mStream);//这种方式将报错

        private static IntPtr hInst;

        #endregion

        #region ICON

        public static System.Drawing.Icon ExtractIcon(string fileName, int index)
        {
            if (System.IO.File.Exists(fileName) || System.IO.Directory.Exists(fileName))
            {
                System.IntPtr hIcon;

                hIcon = ExtractIconA(hInst, fileName, -1);

                if (hIcon.Equals(IntPtr.Zero))
                {
                    return ExtractAssociatedIcon(fileName);
                }
                else
                {
                    int numOfIcons = hIcon.ToInt32();

                    if (0 <= index && index < numOfIcons)
                    {
                        hIcon = ExtractIconA(hInst, fileName, index);

                        if (!hIcon.Equals(IntPtr.Zero))
                        {
                            return System.Drawing.Icon.FromHandle(hIcon);
                        }
                        else
                        {
                            return null;
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            else
            {
                return null;
            }
        }

        public static System.Drawing.Icon ExtractAssociatedIcon(string fileName)
        {
            if (System.IO.File.Exists(fileName) || System.IO.Directory.Exists(fileName))
            {
                int i = 0;

                IntPtr hIcon = ExtractAssociatedIconA(hInst, fileName, ref i);

                if (!hIcon.Equals(IntPtr.Zero))
                {
                    return System.Drawing.Icon.FromHandle(hIcon);
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static System.Drawing.Image GetIconFromFile(string aFilePath)
        {
            System.Drawing.Icon img = UICommon.Icon.ExtractIcon(aFilePath.ToString(), 0);
            return img.ToBitmap();
        }

        public static System.Drawing.Image GetIconByMemoryStream(string aFilePath)
        {
            System.Drawing.Image img = new System.Drawing.Bitmap(aFilePath.ToString());
            System.IO.MemoryStream mStream = new System.IO.MemoryStream();
            img.Save(mStream, System.Drawing.Imaging.ImageFormat.Png);
            System.Drawing.Image IconShow = System.Drawing.Image.FromStream(mStream);
            mStream.Close();
            img.Dispose();
            return IconShow;
        }

        public static System.Drawing.Image GetImgFromPic(string aFilePath)
        {
            System.Drawing.Image img = new System.Drawing.Bitmap(aFilePath.ToString());
            return img;
        }

        public static System.Drawing.Icon GetIconFromPic(string aFilePath)
        {
            System.Drawing.Image img = new System.Drawing.Bitmap(aFilePath.ToString());
            System.IO.MemoryStream mStream = new System.IO.MemoryStream();
            img.Save(mStream, System.Drawing.Imaging.ImageFormat.Gif);
            System.Drawing.Icon IconShow = System.Drawing.Icon.FromHandle(new System.Drawing.Bitmap(mStream).GetHicon());
            mStream.Close();
            img.Dispose();
            return IconShow;
        }
        #endregion
    }
}