用C#实现缩放gif文件,请高手们指点,分马上给!! 用C#实现缩放gif文件,请高手们指点,分马上给!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //原始图片名称 string originalFilename = "c:\\222.jpg"; //生成的高质量图片名称 string strGoodFile = "c:\\222-small-good.jpg"; //生成的低质量图片名称 string strBadFile = "c:\\222-small-bad.jpg"; //缩小的倍数 int iScale = 3; //从文件取得图片对象 System.Drawing.Image image = System.Drawing.Image.FromFile(originalFilename); //取得图片大小 System.Drawing.Size size = new Size(image.Width / iScale , image.Height / iScale); //新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,size.Height); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空一下画布 g.Clear(Color.Blue); //在指定位置画图 g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), new System.Drawing.Rectangle(0, 0, image.Width,image.Height), System.Drawing.GraphicsUnit.Pixel); //保存高清晰度的缩略图 bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg); //取得原图像的普通缩略图 System.Drawing.Image img = image.GetThumbnailImage(image.Width / iScale, image.Height / iScale, null, IntPtr.Zero); //保存普通缩略图 img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Jpeg); g.Dispose(); MessageBox.Show("生成完毕");jpg改成gif 行吗? --#.. /// <summary> /// 图像尺寸调节 /// </summary> /// <param name="b">原始图像</param> /// <param name="dstWidth">目标宽度</param> /// <param name="dstHeight">目标高度</param> /// <returns></returns> public Bitmap ResizeBitmap(Bitmap b, int dstWidth, int dstHeight) { Bitmap dstImage = new Bitmap(dstWidth, dstHeight); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dstImage); // 设置插值模式 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear; // 设置平滑模式 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.DrawImage(b, new System.Drawing.Rectangle(0, 0, dstImage.Width, dstImage.Height), new System.Drawing.Rectangle(0, 0, b.Width, b.Height), System.Drawing.GraphicsUnit.Pixel); g.Save(); g.Dispose(); return dstImage; } // end of Resize private void reSizeProcess() { string pathStrSelText; int n = 0; this.label1.Text=""; //string pathStrResize=""; //MessageBox.Show(NewWidth.ToString()); foreach (ListViewItem SelItems in this.listView1.Items) { pathStrSelText = SelItems.Text; Bitmap zc = new Bitmap(SelItems.Text); zc = ResizeBitmap(zc, NewHeight, NewWidth); EncoderParameters ep = new EncoderParameters(); ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); //find the encoder with the image/jpeg mime-type ImageCodecInfo ici = null; foreach (ImageCodecInfo codec in codecs) { if (codec.MimeType == "image/jpeg") ici = codec; } //We'll save images with 25%, 50%, 75% and 100% quality as compared with the original //for (int x = 25; x < 101; x += 25) //{ //Create an encoder parameter for quality with an appropriate level setting ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)50); //} // zc.Save(@"d:\1.jpg"); Directory.CreateDirectory(Path.GetDirectoryName(pathStrSelText) + @"\" + "bakPhoto" + @"\" + "reSize"); zc.Save((Path.GetDirectoryName(pathStrSelText) + @"\" + "bakPhoto" + @"\" + "reSize" + @"\" + Path.GetFileNameWithoutExtension(pathStrSelText) + Path.GetExtension(pathStrSelText)), ici, ep); //Directory.CreateDirectory(Application.StartupPath + @"\bakPhoto"); //zc.Save(Application.StartupPath + @"\bakPhoto\" + Path.GetFileName(pathStrSelText), ici, ep); if (n < 20) { this.label1.Text += "\n"; this.label1.Text += Path.GetFileName(SelItems.Text); n++; } else { n = 0; this.label1.Text = ""; } } this.label1.Text += "\n"; this.label1.Text += "工作完成.. 累死我了..."; } 首先非常感谢“zhaochong12(超级大笨鸟)”大哥,不过你的方法里似乎只能收缩静态图片,gif还是不行,因为用这种方法收缩后的gif图片 就变成静态图片了,我想要解决的就是这一点。找了很多资料都没找到解决方案,郁闷………… zhaochong12(超级大笨鸟) ,可以把gif一贞一贞读出来然后每贞缩小后在组成一个新的gif,帮忙写个例子哦~~,先给你加分~~ C# 自定义打印纸张大小 关于线程与线程之间访问问题 菜鸟问题,如何将数据库里面的数据显示到Textbox? DataGridView小问题? 急.. 菜鸟学习Ajax遇到的问题相当诡异 DataGridView的奇怪问题:输入数字时第一个数重复 如何在C#获得mpeg视频媒体的播放时间?急!!!!!! picturebox半透明的问题 email的正则表达式求解??? 两个comBoBox要邦定相同内容的时候遇到的问题 急 如何把数据库里面的数据调出来 做为坐标用g.DrawLine()画成曲线 问个惊世骇俗的问题
string originalFilename = "c:\\222.jpg";
//生成的高质量图片名称
string strGoodFile = "c:\\222-small-good.jpg";
//生成的低质量图片名称
string strBadFile = "c:\\222-small-bad.jpg";
//缩小的倍数
int iScale = 3;
//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromFile(originalFilename);
//取得图片大小
System.Drawing.Size size = new Size(image.Width / iScale , image.Height / iScale);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width,image.Height),
System.Drawing.GraphicsUnit.Pixel);
//保存高清晰度的缩略图
bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
//取得原图像的普通缩略图
System.Drawing.Image img = image.GetThumbnailImage(image.Width / iScale, image.Height / iScale, null, IntPtr.Zero);
//保存普通缩略图
img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
MessageBox.Show("生成完毕");
jpg改成gif 行吗? --#..
/// 图像尺寸调节
/// </summary>
/// <param name="b">原始图像</param>
/// <param name="dstWidth">目标宽度</param>
/// <param name="dstHeight">目标高度</param>
/// <returns></returns>
public Bitmap ResizeBitmap(Bitmap b, int dstWidth, int dstHeight)
{
Bitmap dstImage = new Bitmap(dstWidth, dstHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dstImage); // 设置插值模式
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear; // 设置平滑模式
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.DrawImage(b,
new System.Drawing.Rectangle(0, 0, dstImage.Width, dstImage.Height),
new System.Drawing.Rectangle(0, 0, b.Width, b.Height),
System.Drawing.GraphicsUnit.Pixel);
g.Save();
g.Dispose(); return dstImage;
} // end of Resize
private void reSizeProcess()
{
string pathStrSelText;
int n = 0;
this.label1.Text="";
//string pathStrResize="";
//MessageBox.Show(NewWidth.ToString());
foreach (ListViewItem SelItems in this.listView1.Items)
{
pathStrSelText = SelItems.Text;
Bitmap zc = new Bitmap(SelItems.Text); zc = ResizeBitmap(zc, NewHeight, NewWidth);
EncoderParameters ep = new EncoderParameters();
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); //find the encoder with the image/jpeg mime-type
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
ici = codec;
} //We'll save images with 25%, 50%, 75% and 100% quality as compared with the original
//for (int x = 25; x < 101; x += 25)
//{
//Create an encoder parameter for quality with an appropriate level setting
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)50);
//}
// zc.Save(@"d:\1.jpg");
Directory.CreateDirectory(Path.GetDirectoryName(pathStrSelText) + @"\" + "bakPhoto" + @"\" + "reSize");
zc.Save((Path.GetDirectoryName(pathStrSelText) + @"\" + "bakPhoto" + @"\" + "reSize" + @"\" + Path.GetFileNameWithoutExtension(pathStrSelText) + Path.GetExtension(pathStrSelText)), ici, ep);
//Directory.CreateDirectory(Application.StartupPath + @"\bakPhoto");
//zc.Save(Application.StartupPath + @"\bakPhoto\" + Path.GetFileName(pathStrSelText), ici, ep);
if (n < 20)
{
this.label1.Text += "\n";
this.label1.Text += Path.GetFileName(SelItems.Text);
n++;
}
else
{
n = 0;
this.label1.Text = "";
}
}
this.label1.Text += "\n";
this.label1.Text += "工作完成.. 累死我了...";
}