如何在 C# 中创建包含多种尺寸/图像的图标文件

2023-11-25

如何创建包含多种尺寸的图标文件?

我知道我使用位图创建了一个图标Icon.FromHandle()但如何向该图标添加另一个图像/尺寸?

Edit:我需要在我的应用程序中执行此操作,因此我无法执行外部应用程序来进行组合。


我正在寻找一种将 .png 文件(没什么花哨的)组合成图标的方法。在无法找到简单的东西并且这个问题是顶部搜索结果后,我创建了下面的代码。


下面的代码可以创建一个具有多种尺寸的图标,如果对于每个图像,图像.RawFormat is 图像格式.Png, the 图像.像素格式 is PixelFormat.Format32bppArgb并且尺寸小于或等于256x256:

/// <summary>
/// Provides methods for creating icons.
/// </summary>
public class IconFactory
{

    #region constants

    /// <summary>
    /// Represents the max allowed width of an icon.
    /// </summary>
    public const int MaxIconWidth = 256;

    /// <summary>
    /// Represents the max allowed height of an icon.
    /// </summary>
    public const int MaxIconHeight = 256;

    private const ushort HeaderReserved = 0;
    private const ushort HeaderIconType = 1;
    private const byte HeaderLength = 6;

    private const byte EntryReserved = 0;
    private const byte EntryLength = 16;

    private const byte PngColorsInPalette = 0;
    private const ushort PngColorPlanes = 1;

    #endregion

    #region methods

    /// <summary>
    /// Saves the specified <see cref="Bitmap"/> objects as a single 
    /// icon into the output stream.
    /// </summary>
    /// <param name="images">The bitmaps to save as an icon.</param>
    /// <param name="stream">The output stream.</param>
    /// <remarks>
    /// The expected input for the <paramref name="images"/> parameter are 
    /// portable network graphic files that have a <see cref="Image.PixelFormat"/> 
    /// of <see cref="PixelFormat.Format32bppArgb"/> and where the
    /// width is less than or equal to <see cref="IconFactory.MaxIconWidth"/> and the 
    /// height is less than or equal to <see cref="MaxIconHeight"/>.
    /// </remarks>
    /// <exception cref="InvalidOperationException">
    /// Occurs if any of the input images do 
    /// not follow the required image format. See remarks for details.
    /// </exception>
    /// <exception cref="ArgumentNullException">
    /// Occurs if any of the arguments are null.
    /// </exception>
    public static void SavePngsAsIcon(IEnumerable<Bitmap> images, Stream stream)
    {
        if (images == null)
            throw new ArgumentNullException("images");
        if (stream == null)
            throw new ArgumentNullException("stream");

        // validates the pngs
        IconFactory.ThrowForInvalidPngs(images);

        Bitmap[] orderedImages = images.OrderBy(i => i.Width)
                                       .ThenBy(i => i.Height)
                                       .ToArray();

        using (var writer = new BinaryWriter(stream))
        {

            // write the header
            writer.Write(IconFactory.HeaderReserved);
            writer.Write(IconFactory.HeaderIconType);
            writer.Write((ushort)orderedImages.Length);

            // save the image buffers and offsets
            Dictionary<uint, byte[]> buffers = new Dictionary<uint, byte[]>();

            // tracks the length of the buffers as the iterations occur
            // and adds that to the offset of the entries
            uint lengthSum = 0;
            uint baseOffset = (uint)(IconFactory.HeaderLength +
                                     IconFactory.EntryLength * orderedImages.Length);

            for (int i = 0; i < orderedImages.Length; i++)
            {
                Bitmap image = orderedImages[i];

                // creates a byte array from an image
                byte[] buffer = IconFactory.CreateImageBuffer(image);

                // calculates what the offset of this image will be
                // in the stream
                uint offset = (baseOffset + lengthSum);

                // writes the image entry
                writer.Write(IconFactory.GetIconWidth(image));
                writer.Write(IconFactory.GetIconHeight(image));
                writer.Write(IconFactory.PngColorsInPalette);
                writer.Write(IconFactory.EntryReserved);
                writer.Write(IconFactory.PngColorPlanes);
                writer.Write((ushort)Image.GetPixelFormatSize(image.PixelFormat));
                writer.Write((uint)buffer.Length);
                writer.Write(offset);

                lengthSum += (uint)buffer.Length;

                // adds the buffer to be written at the offset
                buffers.Add(offset, buffer);
            }

            // writes the buffers for each image
            foreach (var kvp in buffers)
            {

                // seeks to the specified offset required for the image buffer
                writer.BaseStream.Seek(kvp.Key, SeekOrigin.Begin);

                // writes the buffer
                writer.Write(kvp.Value);
            }
        }

    }

    private static void ThrowForInvalidPngs(IEnumerable<Bitmap> images)
    {
        foreach (var image in images)
        {
            if (image.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new InvalidOperationException
                    (string.Format("Required pixel format is PixelFormat.{0}.",
                                   PixelFormat.Format32bppArgb.ToString()));
            }

            if (image.RawFormat.Guid != ImageFormat.Png.Guid)
            {
                throw new InvalidOperationException
                    ("Required image format is a portable network graphic (png).");
            }

            if (image.Width > IconFactory.MaxIconWidth ||
                image.Height > IconFactory.MaxIconHeight)
            {
                throw new InvalidOperationException
                    (string.Format("Dimensions must be less than or equal to {0}x{1}",
                                   IconFactory.MaxIconWidth, 
                                   IconFactory.MaxIconHeight));
            }
        }
    }

    private static byte GetIconHeight(Bitmap image)
    {
        if (image.Height == IconFactory.MaxIconHeight)
            return 0;

        return (byte)image.Height;
    }

    private static byte GetIconWidth(Bitmap image)
    {
        if (image.Width == IconFactory.MaxIconWidth)
            return 0;

        return (byte)image.Width;
    }

    private static byte[] CreateImageBuffer(Bitmap image)
    {
        using (var stream = new MemoryStream())
        {
            image.Save(stream, image.RawFormat);

            return stream.ToArray();
        }
    }

    #endregion

}

Usage:

using (var png16 = (Bitmap)Bitmap.FromFile(@"C:\Test\3dGlasses16.png"))
using (var png32 = (Bitmap)Bitmap.FromFile(@"C:\Test\3dGlasses32.png"))
using (var stream = new FileStream(@"C:\Test\Combined.ico", FileMode.Create))
{
    IconFactory.SavePngsAsIcon(new[] { png16, png32 }, stream);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 C# 中创建包含多种尺寸/图像的图标文件 的相关文章

  • 有没有办法为向量采用内存资源?

    我已经开始在我的项目中使用 pmr allocators 并且我已经看到使用它们带来了很多性能提升和优势 我使用的分配器与我在下面的简单示例中展示的非常相似 include
  • ASP .NET MVC 5 - 客户地址一对一关系

    我在这里查看了论坛 实际上发现了一些类似的问题 但不是相同的问题 类似的解决方案没有给我正确的答案 我正在使用实体框架和代码优先方法来处理 ASP NET MVC 5 我想建立客户 gt 地址一对一关系的模型 我建模的是 客户等级 publ
  • 实体框架 5 不清除导航属性

    我在 Entity Framework 5 中遇到了这个奇怪的问题 我在其中一个实体中有一个导航属性 我想将其设置为null 但由于某种原因 该属性只有在我第二次调用该属性时才会被清除 using var db new Entities v
  • 模拟 EF core dbcontext 和 dbset

    我正在使用 ASP NET Core 2 2 EF Core 和 MOQ 当我运行测试时 我收到此错误 消息 System NotSupportedException 非虚拟 可在 VB 中重写 成员上的设置无效 x gt x Movies
  • 如何在Qt无框窗口中实现QSizeGrip?

    如何使用 Qt 无框窗口实现 QSizeGrip 代码会是什么样的 您只需在布局内窗口的一角添加 QSizeGrip 即可使其保持在该角落 QDialog dialog new QDialog 0 Qt FramelessWindowHin
  • 输入缓冲区刷新

    考虑下面的代码 include
  • 如何从c++调用python

    我是Python新手 我尝试像这样从 C 调用 python 脚本 在 Raspberry Pi 中 std string pythonCommand python Callee py a b int res system pythonCo
  • 接口中的私有成员

    是否可以在 NET 接口中创建私有成员 我听说现在可以了 但我的 IDE 拒绝了 public interface IAnimal void SetDefaultName string name ChangeName name privat
  • 在 C# 中使用 as 关键字与泛型类型发生冲突的编译时行为

    当尝试对无法转换为的非泛型类型使用 C as 关键字时 编译器会给出无法转换类型的错误 但是 当对泛型类型使用 as 关键字时 编译器不会给出错误 public class Foo public class Bar
  • 将 libpng 链接到 android 原生项目

    我在尝试在本机 Android 项目中加载 libpng 时遇到问题 编译器似乎无法识别 libpng 函数 但可以识别类型 如 png byte 它可以正常编译类型 但如果我添加函数 则会抛出错误 这是编译输出 Windows 7 cmd
  • 为什么 C# 编译的正则表达式比等效的字符串方法更快?

    每次我必须对字符串执行简单的包含或替换操作 其中我正在搜索的术语是固定值 时 我发现如果我获取示例输入并对其进行一些分析 则使用编译的正则表达式是几乎 总是比使用 String 类中的等效方法更快 我尝试过比较多种方法 hs是要搜索的 干草
  • 如何定义 Swagger UI 参数的默认值?

    我已将 Swagger Swashbuckle 集成到 NET Core 2 2 API 项目中 一切都很好 我的要求纯粹是为了方便 考虑以下 API 方法 public Model SomeEstimate SomeRequest req
  • 是否可以从.NET Core中间件检索控制器的操作结果?

    public class UsersController APIControllerBase public UsersController public Client Get return new Client ClientID 1 Las
  • 第一个随机数始终小于其余随机数

    我碰巧注意到 在 C 中 使用 std rand 方法调用的第一个随机数大多数时候都明显小于第二个随机数 关于 Qt 实现 第一个几乎总是小几个数量级 qsrand QTime currentTime msec qDebug lt lt q
  • TypeScript 中 C# 类虚拟成员的等效项

    因此 在 C 中 当我创建模型类和延迟加载内容时 我会执行以下操作 public int User ID get set public int Dept ID get set 然后在我的班级稍远一点的地方 我像这样弹出我的虚拟 public
  • 可选参数代码在 .NET 3.5 中编译。为什么?

    这段代码在 VS 2010 的框架 3 5 项目中编译正常 我三次检查过 public LoggingClient string uri net msmq localhost logging 为什么 我在 C 4 规范中没有看到任何内容 文
  • 任何浮点密集型代码是否会在任何基于 x86 的架构中产生位精确的结果?

    我想知道使用浮点运算的 C 或 C 代码是否会在任何基于 x86 的体系结构中产生位精确的结果 无论代码的复杂性如何 据我所知 自 Intel 8087 以来的任何 x86 架构都使用准备处理 IEEE 754 浮点数的 FPU 单元 并且
  • MsBuild 在 Visual Studio Online 上找不到恢复的 NuGet 包

    我尝试构建一个存储在 Visual Studio Online 上的外部 GIT 存储库中的解决方案 它有以下步骤 1 Git 恢复 有效 2 NuGet 恢复 有效 3 构建 不起作用 查看日志时我的第一个猜测是 MsBuild 没有查找
  • 从哪里开始阅读 SQLite 源代码? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想了解sqlite是如何实现的 并且 想阅读源代码 我已经下载了源代码 我应该开始查看代码的哪一部分 SQLite文档页 http
  • GetActiveObject() 与 GetObject() -- MK_E_UNAVAILABLE 错误

    All 我在将一些 VBA 代码转换为 C 时遇到一些问题 我们有一个充当本地 COM 服务器的第 3 方应用程序 在我们使用的VBA代码中获取对象 获取对现有对象的引用 e g Set appHandle GetObject ProgId

随机推荐

  • 在 .NET 中获取当前文化日名称

    是否有可能获得CurrentCulture的工作日从DateTimeFormatInfo 但返回Monday作为一周的第一天而不是Sunday 并且 如果当前区域性不是英语 即 ISO 代码不是 en 则将其保留为默认值 默认情况下Cult
  • EntityContainer名称在不同程序集中必须是唯一的?

    我有两个项目 新闻 数据 标签 数据 两者都定义Entities 当我尝试执行时 using var db new News Data Entities name Entities results1 db News ToList using
  • 如何将 SSRS 时间格式转换为 HH:MM AM/PM 格式?

    我的任务是增强以下代码以显示不带秒的时间以及 AM PM IIF Fields New Date Value lt gt NO CHANGE FormatDateTime IIF Fields New Date Value NO CHANG
  • Bash 补全:compgen 一个单词列表,就好像它们是路径一样 - 仅建议直到下一个斜杠

    我正在为一个 bash 完成脚本工作点文件管理实用程序 该工具有一个命令dots diff filename 这将显示安装的点文件和源点文件之间的差异 它还有一个命令dots files其中列出了所有托管点文件的路径 相对于源目录 我想用
  • sendmail 错误 452 这一小时收到的收件人太多

    我们正在向我们的 BREW 设备 在 sprint 网络中 发送大量电子邮件 一段时间后 我们的邮件服务器将消息排队并卡住了 当我们尝试刷新它们时 我们会从服务器获得关注 这个问题有解决办法吗 这个错误 452 是来自我们的邮件服务器还是
  • 查找DBUpdateException的原因

    打电话时DbContext SaveChanges 我得到一个 DbUpdateException 未处理的类型异常 System Data Entity Infrastruct DbUpdateException 发生在 实体框架 dll
  • 如何在 Python 中使用 Selenium 创建新的(持久的)Firefox 配置文件?

    尝试使用 Selenium 添加新的 持久的 Firefox 配置文件 AFAIK 执行时FirefoxProfile 使用临时文件生成新的配置文件 理想情况下 该配置文件应该能够对后续进程保持可用 即使创建者关闭后也是如此 Problem
  • 新的 iOS 7.1 mini-ui 视口设置是否存在错误?

    iOS 7 1 中新的 最小 ui 设置非常适合横向网站 我的网络应用程序使用全屏 绝对定位的 div 来显示其内容 以赋予其类似应用程序的感觉 但 Safari 似乎只是在底部添加了 URL 栏的高度 我在不同的 iPhone 上都试过了
  • Slim 3 自动装载机

    我是 slim 框架的新手 不知道如何使用自动加载器来自动加载我的类 我创建了一个app models myclass php但当然 当我尝试使用它时 我发现找不到课程 我不确定哪种是自动加载类的正确方法 或者我应该使用的命名约定 我应该通
  • Spring Boot 2.2.2.RELEASE 中集成测试隔离失败(每次 SpringBootTest 后删除表时出错)

    我们的应用程序正在 2 0 4 版本中运行 升级到 2 2 2 RELEASE 后 我们看到集成测试失败 我怀疑存在一些配置错误 并且每个集成测试本身都不会清理 或者存在以前不存在的额外初始化 我真的不知道如何正确修复它 再具体一点 每个测
  • 如何通过 REST 查询 SQL Server 来获取 XML

    我们一直在使用 Web 应用程序框架来构建需要能够查询 SQL Server 数据库并获取 XML 形式的结果的应用程序 过去 框架提供了这种能力 但该功能现已被弃用 所以我们在想 该框架允许我们轻松地通过 HTTP 查询 REST 服务
  • 禁用 xampp 将 http 重定向到 https

    我在 localhost 中的 xampp 上为 wordpress 创建了 VirtualHost 并在 httpd vhosts conf 中添加了以下代码
  • 在 Linux 上用 python 监听全局组合键

    我刚刚写了一个小程序 每隔几分钟从 flickr 下载一张新壁纸 现在我想添加 喜欢 壁纸的功能 因此它会比不喜欢或不喜欢的壁纸更频繁地出现 我想为此函数分配一个全局键盘快捷键 例如 如果我按 ctrl 7 它将在 Python 中执行某种
  • Tensorflow尝试使用未初始化的值AUC/AUC/auc/false_positives

    我正在训练用于图像分类的 CNN 由于我的数据集大小有限 我正在使用迁移学习 基本上 我使用的是谷歌在其重新训练示例中证明的预训练网络 https www tensorflow org tutorials image retraining
  • Git重置=致命:无法读取树

    有人在错误的分支上提交了所以我尝试将提交移动到另一个分支 当我尝试回滚提交时 出现以下错误 user server gitrepo git reset hard 9c2de3c0 fatal unable to read tree 9c2d
  • 如何在使用 Android API Level 15 的同时保持向后兼容性?

    冰淇淋三明治引入了很多新的UI设计元素 但市场渗透率仍然只有4 左右 如果您想使其应用程序面向未来 并利用 Honeycomb Ice Cream Sandwich 引入的一些设计元素 例如操作栏 配色方案等 那么确保您保持一定程度的落后的
  • Highgui 和 ruby

    我需要编写一个简单的项目 我正在使用 opencv ruby 和 mac 我通过brew安装了opencv 通过gem install安装了rb webcam coding utf 8 require opencv require rb w
  • .htaccess 用于 SEO 机器人在没有 hashbang 的情况下抓取单页应用程序

    Using a pushState启用页面 通常您使用重定向 SEO 机器人escaped fragment习俗 您可以阅读更多相关内容here 该约定假设您将使用 hashbang 前缀位于单页应用程序上所有 URI 之前 SEO 机器人
  • 按 R 中的因素过滤数据框

    我有以下数据框 sp lt combn c sp1 sp2 sp3 sp4 2 d lt data frame t sp freq sample 0 100 6 和两个因素 x1 lt as factor c sp1 sp2 x2 lt a
  • 如何在 C# 中创建包含多种尺寸/图像的图标文件

    如何创建包含多种尺寸的图标文件 我知道我使用位图创建了一个图标Icon FromHandle 但如何向该图标添加另一个图像 尺寸 Edit 我需要在我的应用程序中执行此操作 因此我无法执行外部应用程序来进行组合 我正在寻找一种将 png 文