组合 System.Drawing.Bitmap[] -> 图标

2024-02-15

将图标拆分为Bitmap零件很简单:

Bitmap icon16 = new Icon(combinedIcon, new Size(16, 16)).ToBitmap()

But 你如何合并多种的Bitmap物合而为一Icon?

Bitmap icon16, icon32, icon64;
Icon combinedIcon = [...]


我不是that明确关于Icon一般对象。它确实是一组多个图像。加载时,可以将其单独放入其Bitmap部分。但我没有看到任何创建多图标的方法。无法迭代、添加或删除似乎也很奇怪Bitmap以一种明显的方式组成部分,就像拥有一组位图一样。


The Icon.Net 中的 class 非常初级,甚至还没有接近提供对实际图标格式的所有功能的访问。最好将图标构建为字节流,然后将其加载为图标。

我不久前研究过该格式,它实际上接受 png 数据作为内部图像。请注意,所述图像的宽度或高度不能超过 256 像素,并且文件中的图像数量以两个字节保存,因此不能超过Int16.MaxValue、或 0xFFFF、或 65535。

代码应该如下所示:

public static Icon ConvertImagesToIco(Image[] images)
{
    if (images == null)
        throw new ArgumentNullException("images");
    Int32 imgCount = images.Length;
    if (imgCount == 0)
        throw new ArgumentException("No images given!", "images");
    if (imgCount > 0xFFFF)
        throw new ArgumentException("Too many images!", "images");
    using (MemoryStream ms = new MemoryStream())
    using (BinaryWriter iconWriter = new BinaryWriter(ms))
    {
        Byte[][] frameBytes = new Byte[imgCount][];
        // 0-1 reserved, 0
        iconWriter.Write((Int16)0);
        // 2-3 image type, 1 = icon, 2 = cursor
        iconWriter.Write((Int16)1);
        // 4-5 number of images
        iconWriter.Write((Int16)imgCount);
        // Calculate header size for first image data offset.
        Int32 offset = 6 + (16 * imgCount);
        for (Int32 i = 0; i < imgCount; ++i)
        {
            // Get image data
            Image curFrame = images[i];
            if (curFrame.Width > 256 || curFrame.Height > 256)
                throw new ArgumentException("Image at index " + i + " is too large!", "images");
            // for these three, 0 is interpreted as 256,
            // so the cast reducing 256 to 0 is no problem.
            Byte width = (Byte)curFrame.Width;
            Byte height = (Byte)curFrame.Height;
            Byte colors = (Byte)curFrame.Palette.Entries.Length;
            Int32 bpp;
            Byte[] frameData;
            using (MemoryStream pngMs = new MemoryStream())
            {
                curFrame.Save(pngMs, ImageFormat.Png);
                frameData = pngMs.ToArray();
            }
            // Get the colour depth to save in the icon info. This needs to be
            // fetched explicitly, since png does not support certain types
            // like 16bpp, so it will convert to the nearest valid on save.
            Byte colDepth = frameData[24];
            Byte colType = frameData[25];
            // I think .Net saving only supports colour types 2, 3 and 6 anyway.
            switch (colType)
            {
                case 2: bpp = 3 * colDepth; break; // RGB
                case 6: bpp = 4 * colDepth; break; // ARGB
                default: bpp = colDepth; break; // Indexed & greyscale
            }
            frameBytes[i] = frameData;
            Int32 imageLen = frameData.Length;
            // Write image entry
            // 0 image width. 
            iconWriter.Write(width);
            // 1 image height.
            iconWriter.Write(height);
            // 2 number of colors.
            iconWriter.Write(colors);
            // 3 reserved
            iconWriter.Write((Byte)0);
            // 4-5 color planes
            iconWriter.Write((Int16)0);
            // 6-7 bits per pixel
            iconWriter.Write((Int16)bpp);
            // 8-11 size of image data
            iconWriter.Write(imageLen);
            // 12-15 offset of image data
            iconWriter.Write(offset);
            offset += imageLen;
        }
        for (Int32 i = 0; i < imgCount; i++)
        {
            // Write image data
            // png data must contain the whole png data file
            iconWriter.Write(frameBytes[i]);
        }
        iconWriter.Flush();
        ms.Position = 0;
        return new Icon(ms);
    }
}

请注意,与其他System.Drawing图像格式,Icon类确实not要求流保持打开状态;它只是从流中读取字节并保留在那里。

可以找到png颜色深度信息here http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html and here https://stackoverflow.com/questions/55978230/how-to-query-the-bit-depth-of-a-png, btw.

如果您想要将图标文件作为字节数组,或者想要将其写入光盘,您当然可以修改此代码以返回字节数组,或者甚至只是让它将内容写入到Stream作为参数给出而不是创建MemoryStream内部。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

组合 System.Drawing.Bitmap[] -> 图标 的相关文章

随机推荐