C# 缩放图片

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

public partial class Media_ImageCut : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\0.gif";
string SavePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"/temp/1.gif";
int width = 100;
int height = 100;
ImageCut(FilePath,SavePath,width,height);
}
protected void ImageCut(string Path,string SavePath,int width,int height)
{
string imgPath = Path;//原图路径
System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath);//获取原图
if (img.Width > width && img.Height > height)//不够width*height的不进行缩放
{
System.Drawing.Image img1 = new Bitmap(width, height);//获取新图的第一帧
Image img1_1 = new Bitmap(width, height);//新图的其他帧
Graphics GDI_1 = Graphics.FromImage(img1);//新图的第一帧GDI
Graphics GDI1_1 = Graphics.FromImage(img1_1);//新图的其他帧GDI
//绘制新图的第一帧
GDI_1.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;//呈现颜色时,覆盖其背景色
GDI_1.InterpolationMode = InterpolationMode.HighQualityBicubic;//质量最高的转换图像
GDI_1.PixelOffsetMode = PixelOffsetMode.HighQuality;//高质量、低速度呈现
GDI_1.SmoothingMode = SmoothingMode.HighQuality;//抗锯齿
GDI_1.Clear(Color.FromKnownColor(KnownColor.Transparent));//系统默认背景填充
//绘制新图其他帧的图像
GDI_1.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;//呈现颜色时,覆盖其背景色
GDI_1.InterpolationMode = InterpolationMode.HighQualityBicubic;//质量最高的转换图像
GDI_1.PixelOffsetMode = PixelOffsetMode.HighQuality;//高质量、低速度呈现
GDI_1.SmoothingMode = SmoothingMode.HighQuality;//抗锯齿
GDI_1.Clear(Color.FromKnownColor(KnownColor.Transparent));//系统默认背景填充
foreach (Guid guid in img.FrameDimensionsList)//遍历维数
{
FrameDimension f = FrameDimension.Time;//时间维度
int count = img.GetFrameCount(f);//根据时间维度获取总帧数
Encoder encode = Encoder.SaveFlag;//保存标志参数类别
EncoderParameters ep = null;//参数
ImageCodecInfo ici = null;//编码解码器
ImageCodecInfo[] icis = ImageCodecInfo.GetImageDecoders();//解码器集合
//获取gif的解码器
foreach (ImageCodecInfo ici1 in icis)
{
if (ici1.FormatID == ImageFormat.Gif.Guid)
{
ici = ici1;
break;
}
}
//遍历每一帧
for (int z = 0; z < count; z++)
{
img.SelectActiveFrame(f, z);
//第一帧
if (z == 0)
{
//将原图的第一帧画给新图的第一帧
GDI_1.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
for (int i = 0; i < img.PropertyItems.Length; i++)//获取原图的所有属性赋给新图
{
img1.SetPropertyItem(img.PropertyItems[i]);
}
ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(encode, (long)EncoderValue.MultiFrame);//指定对象的帧数多于1帧
img1.Save(SavePath, ici, ep);//将第一帧保存到新图
}
else
{
GDI1_1.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
for (int i = 0; i < img.PropertyItems.Length; i++)
{
img1_1.SetPropertyItem(img.PropertyItems[i]);
}
ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(encode, (long)EncoderValue.FrameDimensionTime);
img1.SaveAdd(img1_1, ep);
}

}
ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(encode, (long)EncoderValue.Flush);//关闭多帧文件流
img1.SaveAdd(ep);
img.Dispose();
img1.Dispose();
img1_1.Dispose();
GDI_1.Dispose();
GDI1_1.Dispose();
}
}
}

}

你可能感兴趣的:(C#)