c# 图片处理及ico 生成

Fork Me On Github
zodream 编程技术 C# 2020年06月

根据路径获取图片

c#
 
var bmp = new Bitmap(fileName);
1

更改图片尺寸

c#
                                   
Image img;

var bmp = new Bitmap(width, height);
bmp.SetResolution(source.HorizontalResolution, source.VerticalResolution);
using (var g = Graphics.FromImage(bmp))
{
    g.CompositingMode = CompositingMode.SourceCopy;
    switch (quality)
    {
        case SmoothingMode.AntiAlias:
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            break;
        case SmoothingMode.HighQuality:
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            break;
        case SmoothingMode.HighSpeed:
            g.CompositingQuality = CompositingQuality.HighSpeed;
            g.InterpolationMode = InterpolationMode.NearestNeighbor;
            g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
            g.SmoothingMode = SmoothingMode.HighSpeed;
            break;
    }
    using (var ia = new ImageAttributes())
    {
        ia.SetWrapMode(WrapMode.TileFlipXY);
        g.DrawImage(source, new System.Drawing.Rectangle(0, 0, width, height), 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia);
    }
}
return bmp;
1234567891011121314151617181920212223242526272829303132333435

png 转 ico

ico 图标是由不同尺寸的png 图片组成。因此可以设置不同尺寸下不同的图。

生成不同尺寸的图

c#
                                           
/// <summary>
/// 修改图片尺寸
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="source"></param>
/// <returns></returns>
public static Bitmap ResizeImage(int width, int height, Image source, SmoothingMode quality)
{
    var bmp = new Bitmap(width, height);
    bmp.SetResolution(source.HorizontalResolution, source.VerticalResolution);
    using (var g = Graphics.FromImage(bmp))
    {
        g.CompositingMode = CompositingMode.SourceCopy;
        switch (quality)
        {
            case SmoothingMode.AntiAlias:
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.SmoothingMode = SmoothingMode.AntiAlias;
                break;
            case SmoothingMode.HighQuality:
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                break;
            case SmoothingMode.HighSpeed:
                g.CompositingQuality = CompositingQuality.HighSpeed;
                g.InterpolationMode = InterpolationMode.NearestNeighbor;
                g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
                g.SmoothingMode = SmoothingMode.HighSpeed;
                break;
        }
        using (var ia = new ImageAttributes())
        {
            ia.SetWrapMode(WrapMode.TileFlipXY);
            g.DrawImage(source, new System.Drawing.Rectangle(0, 0, width, height), 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia);
        }
    }
    return bmp;
}
12345678910111213141516171819202122232425262728293031323334353637383940414243

ico 头

c#
           
/// <summary>
/// 写头
/// </summary>
/// <param name="count"></param>
/// <param name="writer"></param>
private static void CreateHeader(int count, BinaryWriter writer)
{
    writer.Write((ushort)0);
    writer.Write((ushort)1);
    writer.Write((ushort)count); // 
}
1234567891011

获取图片数据的长度,及每一个像素,每一个像素又包括 RGBA

c#
    
private static int GetImageSize(Image image)
{
    return image.Height * image.Width * 4;
}
1234

写入每张图的位置

c#
                
 private static void CreateDirectory(int offset, Image image, int imageLength, BinaryWriter writer)
{
    var size = imageLength + 40;
    var width = image.Width >= 256 ? 0 : image.Width;
    var height = image.Height >= 256 ? 0 : image.Height;
    var bpp = Image.GetPixelFormatSize(image.PixelFormat);

    writer.Write((byte)width);
    writer.Write((byte)height);
    writer.Write((byte)0);
    writer.Write((byte)0);
    writer.Write((ushort)1);
    writer.Write((ushort)bpp);
    writer.Write((uint)size);
    writer.Write((uint)offset);
}
12345678910111213141516

写入每张图的数据

c#
                             
private static void CreateBitmap(Image image, int compression, int imageLength, BinaryWriter writer)
{
    writer.Write((uint)40);
    writer.Write((uint)image.Width);
    writer.Write((uint)image.Height * 2);
    writer.Write((ushort)1);
    writer.Write((ushort)Image.GetPixelFormatSize(image.PixelFormat));
    writer.Write((uint)compression);
    writer.Write((uint)imageLength);
    writer.Write(0);
    writer.Write(0);
    writer.Write((uint)0);
    writer.Write((uint)0);
}

private static void CreateDib(Image image, BinaryWriter writer)
{
    for (int i = 0; i < image.Height; i++)
    {
        for (int j = 0; j < image.Width; j++)
        {
            var color = (image as Bitmap).GetPixel(j, i);
            writer.Write(color.B);
            writer.Write(color.G);
            writer.Write(color.R);
            writer.Write(color.A);
        }
    }
}
1234567891011121314151617181920212223242526272829

生成ico

c#
              
IEnumerable<Image> images;
Stream stream;
var bw = new BinaryWriter(stream);
CreateHeader(images.Count(), bw);
var offset = 6 + (16 * images.Count());
foreach (var item in images) {
    var length = GetImageSize(item);
    CreateDirectory(offset, item, length, bw);
}
foreach (var item in images) {
    CreateBitmap(item, colorMode, GetImageSize(item), bw);
    CreateDib(item, bw);
}
bw.Dispose();
1234567891011121314
点击查看全文
0 508 0