WinformGDIPaintPlane/Helper/ICON.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace Helper
{
    public class ICON
    {
        /// <summary>
        /// 保存为ICO文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns>true:成功 false:失败</returns>
        public static bool SaveAsICON(string filePath, int width = 0, int height = 0)
        {
			try
			{
				string fileDirectory = Path.GetDirectoryName(filePath);
				string fileSavePath = Path.Combine(fileDirectory, Path.GetFileNameWithoutExtension(filePath)) + ".ico";
				using (Bitmap bm = new Bitmap(filePath))
				{
					if (width == 0 || height == 0)
					{
						width = bm.Width;
						height = bm.Height;
					}
					bool isSuccess = ConvertImageToIcon(filePath, fileSavePath, new Size(width, height));
					Process.Start("explorer.exe", fileDirectory);
					return isSuccess;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("转化失败。错误信息:" + ex.Message);
				return false;
			}
        }

		/// <summary>
		/// 图片转换为ico文件
		/// </summary>
		/// <param name="origin">原图片路径</param>
		/// <param name="destination">输出ico文件路径</param>
		/// <param name="iconSize">输出ico图标尺寸,不可大于255x255</param>
		/// <returns>是否转换成功</returns>
		private static bool ConvertImageToIcon(string origin, string destination, Size iconSize)
		{
			if (iconSize.Width > 255 || iconSize.Height > 255)
			{
				return false;
			}
			Image image = new Bitmap(new Bitmap(origin), iconSize); //先读取已有的图片为bitmap,并缩放至设定大小
			MemoryStream bitMapStream = new MemoryStream(); //存原图的内存流
			MemoryStream iconStream = new MemoryStream(); //存图标的内存流
			image.Save(bitMapStream, ImageFormat.Png); //将原图读取为png格式并存入原图内存流
后面还有 36 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。