根据路径获取图片
更改图片尺寸
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;
png 转 ico
ico 图标是由不同尺寸的png 图片组成。因此可以设置不同尺寸下不同的图。
生成不同尺寸的图
/// <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;
}
ico 头
/// <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); //
}
获取图片数据的长度,及每一个像素,每一个像素又包括 RGBA
写入每张图的位置
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);
}
写入每张图的数据
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);
}
}
}
生成ico
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();
转载请保留原文链接: https://zodream.cn/blog/id/159.html