首页 > 编程语言 >c#图片转ico图标

c#图片转ico图标

时间:2023-06-03 09:12:42浏览次数:31  
标签:iconWriter ico c# image Write new byte size 图标

/// <summary>
/// Converts a PNG image to a icon (ico)
/// </summary>
/// <param name="inputPath">The input path</param>
/// <param name="outputPath">The output path</param>
/// <param name="size">The size (16x16 px by default)</param>
/// <param name="preserveAspectRatio">Preserve the aspect ratio</param>
/// <returns>Wether or not the icon was succesfully generated</returns>
public static bool ConvertPNGToIcon(string inputPath, string outputPath, int size = 256, bool preserveAspectRatio = true)
{
    using (FileStream inputStream = new FileStream(inputPath, FileMode.Open))
    using (FileStream outputStream = new FileStream(outputPath, FileMode.OpenOrCreate))
    {
        return ConvertToIcon(inputStream, outputStream, size, preserveAspectRatio);
    }
}
 
/// <summary>
/// Converts a PNG image to a icon (ico)
/// </summary>
/// <param name="input">The input stream</param>
/// <param name="output">The output stream</param>
/// <param name="size">The size (16x16 px by default)</param>
/// <param name="preserveAspectRatio">Preserve the aspect ratio</param>
/// <returns>Wether or not the icon was succesfully generated</returns>
public static bool ConvertPNGToIcon(Stream input, Stream output, int size = 16, bool preserveAspectRatio = false)
{
    Bitmap inputBitmap = (Bitmap)Bitmap.FromStream(input);
    if (inputBitmap != null)
    {
        int width, height;
        if (preserveAspectRatio)
        {
            width = size;
            height = inputBitmap.Height / inputBitmap.Width * size;
        }
        else
        {
            width = height = size;
        }
        Bitmap newBitmap = new Bitmap(inputBitmap, new System.Drawing.Size(width, height));
        if (newBitmap != null)
        {
            // save the resized png into a memory stream for future use
            using (MemoryStream memoryStream = new MemoryStream())
            {
                newBitmap.Save(memoryStream, ImageFormat.Png);
 
                BinaryWriter iconWriter = new BinaryWriter(output);
                if (output != null && iconWriter != null)
                {
                    // 0-1 reserved, 0
                    iconWriter.Write((byte)0);
                    iconWriter.Write((byte)0);
 
                    // 2-3 image type, 1 = icon, 2 = cursor
                    iconWriter.Write((short)1);
 
                    // 4-5 number of images
                    iconWriter.Write((short)1);
 
                    // image entry 1
                    // 0 image width
                    iconWriter.Write((byte)width);
                    // 1 image height
                    iconWriter.Write((byte)height);
 
                    // 2 number of colors
                    iconWriter.Write((byte)0);
 
                    // 3 reserved
                    iconWriter.Write((byte)0);
 
                    // 4-5 color planes
                    iconWriter.Write((short)0);
 
                    // 6-7 bits per pixel
                    iconWriter.Write((short)32);
 
                    // 8-11 size of image data
                    iconWriter.Write((int)memoryStream.Length);
 
                    // 12-15 offset of image data
                    iconWriter.Write((int)(6 + 16));
 
                    // write image data
                    // png data must contain the whole png data file
                    iconWriter.Write(memoryStream.ToArray());
 
                    iconWriter.Flush();
 
                    return true;
                }
            }
        }
        return false;
    }
    return false;
}
 
 
调用:
string sourceurl = @"C:\Users\wh\Desktop\摄像头1-01.png";
            string outUrl = @"C:\Users\wh\Desktop\pt.ico";
            bool bl = FileHelper.ConvertPNGToIcon(sourceurl, outUrl);

  

标签:iconWriter,ico,c#,image,Write,new,byte,size,图标
From: https://www.cnblogs.com/wenghan/p/17453282.html

相关文章

  • Vue——属性指令、style和class、条件渲染、列表渲染、事件处理、数据双向绑定、过滤
    vm对象<body><divid="app"><h1>{{name}}</h1><button@click="handleClick">点我</button></div></body><script>//1写在data或method中的属性或方法,从vm中直接可以点出来//2method的函数中,如......
  • 如何使用JavaScript格式化日期 – JS中的日期格式化
    日期是许多JavaScript应用程序的基本组成部分,无论是在网页上显示当前日期还是处理用户输入以安排事件。但以清晰一致的格式显示日期对于积极的用户体验至关重要。在本文中,我们将探讨在JavaScript中格式化日期的各种技术,使您能够以您的应用程序所需的格式显示日期。(前端教程......
  • Docker入门与实践
    Docker容器化技术对比虚拟机技术虚拟机技术虚拟出一套硬件资源,在此上安装操作系统进而运行一些软件而容器虚拟化出的容器都是直接使用宿主机硬件资源,基于宿主机的内核上进行运行,不用虚拟出来一套硬件资源,从而更快,更节约存储空间虚拟机与容器化1.两者是相辅相成的,可以共同使......
  • CF321E - Ciel and Gondolas
    考虑\(dp_{i,j}\)表示用\(i\)条船载走前\(j\)个人的最小贡献,\(w_{i,j}\)表示区间\([i,j]\)里的人同乘一条船的代价。则\(dp_{i,j}=\min_{1\lek\ltj}(dp_{i-1,k}+w_{k+1,j})\)。我们发现,\(w_{i,j}\)可以通过\(w_{i,j-1}+s_{j,j}-s_{j,i-1}\)递推计算。其中\(s_{i,......
  • 5、cloudsim基于CloudSimExample6建立自己云计算仿真平台
    1、项目结构如下: 2、构建MyCloudSim6.java1packagecloudsim;2importjava.text.DecimalFormat;3importjava.util.ArrayList;4importjava.util.Calendar;5importjava.util.LinkedList;6importjava.util.List;78importorg.cloudbus.clou......
  • 读书笔记: Psychological Power between knowledge and practice; Inverted Totalitar
    JohnDeweyonceremarkedthatequalitybecomesdangerouswhenitiswidelypraisedbutemptyinpractice. Howtogeneratesuchkindofpsychologicalandsocialpower?Thepropagandaorganizationsadminidtratemassivemedia. Perhapsthemostcrucialel......
  • R语言GARCH族模型:正态分布、t、GED分布EGARCH、TGARCH的VaR分析股票指数|附代码数据
    全文链接:http://tecdat.cn/?p=31023最近我们被客户要求撰写关于GARCH族模型的研究报告,包括一些图形和统计输出。如何构建合适的模型以恰当的方法对风险进行测量是当前金融研究领域的一个热门话题 ( 点击文末“阅读原文”获取完整代码数据******** )。VaR方法作为当前业内比较......
  • MATLAB偏最小二乘回归(PLSR)和主成分回归(PCR)分析光谱数据|附代码数据
    全文链接:http://tecdat.cn/?p=2655最近我们被客户要求撰写关于偏最小二乘回归的研究报告,包括一些图形和统计输出。此示例显示如何在matlab中应用偏最小二乘回归(PLSR)和主成分回归(PCR),并讨论这两种方法的有效性 ( 点击文末“阅读原文”获取完整代码数据******** ) 。当存在大量......
  • Python金融时间序列模型ARIMA 和GARCH 在股票市场预测应用|附代码数据
    原文链接:http://tecdat.cn/?p=24407最近我们被客户要求撰写关于金融时间序列模型的研究报告,包括一些图形和统计输出。这篇文章讨论了自回归综合移动平均模型(ARIMA)和自回归条件异方差模型(GARCH)及其在股票市场预测中的应用 ( 点击文末“阅读原文”获取完整代码数据******......
  • Ubuntu安装CUDA工具包、cuDNN、TensorRT
    简介一下这三个东西:CUDA:是一种针对支持CUDA功能的GPU(图形处理器)的C语言开发环境。cuDNN:是GPU加速的用于深度神经网络的原语库。cuDNN为标准例程提供了高度优化的实现,例如向前和向后卷积,池化,规范化和激活层。TensorRT:是nvidia家的一款高性能深度学习推理SDK。此SDK包含深度......