c# 图片格式转换

 

ImageConvert(@"E:\素材\xx.png", @"E:\素材\xx.ico");
 /// <summary>
 /// 图片格式转换
 /// </summary>
 /// <param name="format">目标格式</param>
 /// <param name="imagePath">源图片地址</param>
 /// <param name="outPath">转换后图片地址</param>
 public static void ImageConvert(ImageFormat format, string imagePath, string outPath)
 {
     Image img = Image.FromFile(imagePath);
     var width = 32;
     var height = 32;
     Size size;
     if (width == 1 && height == 1)
     {
         size = new Size(img.Width, img.Height);
     }
     else
     {
         size = new Size(width, height);
     }
     Bitmap bitmap = new Bitmap(img, size);

     //ImageFormat format = ImageFormat.Icon;

     using (FileStream fs = new FileStream(outPath, FileMode.Create))
     {
         if (format == ImageFormat.Icon)
         {
             nint hwd = bitmap.GetHicon();
             Icon icon = Icon.FromHandle(hwd);
             icon.Save(fs);
         }
         else
         {
             bitmap.Save(fs, format);
         }
     }
 }

以上方式在将png转ico时,转出的图片变形

=》

 

换以下换种方式将png转ICO格式 参考

/// <summary>
/// PNG转ICO
/// </summary>
/// <param name="imagePath">源图片流</param>
/// <param name="outPath">转出图片流</param>
/// <param name="size">图片转换目标尺寸</param>
/// <param name="preserveAspectRatio">是否保持原来的纵横比</param>
/// <returns></returns>
public static bool ConvertPNGToIcon(string imagePath, string outPath, int size = 16, bool preserveAspectRatio = false)
{
    Image img = Image.FromFile(imagePath);
    int width, height;
    if (preserveAspectRatio)
    {
        width = size;
        height = img.Height / img.Width * size;
    }
    else
    {
        width = height = size;
    }
    Bitmap bitmap = new Bitmap(img, new Size(width, height));
    if (bitmap != null)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            bitmap.Save(memoryStream, ImageFormat.Png);
            using (FileStream output = new FileStream(outPath, FileMode.OpenOrCreate))
            {
                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;
}

 

转换效果

=》

 注:此处的代码在有尺寸变化是装换后的图片会有失真的情况,修正版本参考

posted @ 2024-05-10 17:29  流年sugar  阅读(88)  评论(0编辑  收藏  举报