不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
C#生成高质量缩略图
编辑:dnawo 日期:2008-10-12
复制内容到剪贴板
程序代码

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
/// <summary>
/// ImageHelper 的摘要说明
/// </summary>
public class ImageHelper
{
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="fileName">原图路径</param>
/// <param name="newFile">缩略图路径</param>
/// <param name="maxHeight">最大高度</param>
/// <param name="maxWidth">最大宽度</param>
public static void ThumbnailImage(string fileName, string newFile, int maxHeight, int maxWidth)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(fileName);
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
//缩略图尺寸
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(img.Width);
double sh = Convert.ToDouble(img.Height);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
Size newSize = new Size(Convert.ToInt32(w),Convert.ToInt32(h));
Bitmap outBmp = new Bitmap(newSize.Width,newSize.Height);
Graphics g = Graphics.FromImage(outBmp);
//设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
//以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0;
x < arrayICI.Length;
x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];
//设置JPEG编码
break;
}
}
if (jpegICI != null)
{
outBmp.Save(newFile, jpegICI, encoderParams);
}
else
{
outBmp.Save(newFile,
thisFormat);
}
img.Dispose();
outBmp.Dispose();
}
}
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
/// <summary>
/// ImageHelper 的摘要说明
/// </summary>
public class ImageHelper
{
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="fileName">原图路径</param>
/// <param name="newFile">缩略图路径</param>
/// <param name="maxHeight">最大高度</param>
/// <param name="maxWidth">最大宽度</param>
public static void ThumbnailImage(string fileName, string newFile, int maxHeight, int maxWidth)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(fileName);
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
//缩略图尺寸
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(img.Width);
double sh = Convert.ToDouble(img.Height);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
Size newSize = new Size(Convert.ToInt32(w),Convert.ToInt32(h));
Bitmap outBmp = new Bitmap(newSize.Width,newSize.Height);
Graphics g = Graphics.FromImage(outBmp);
//设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
//以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0;
x < arrayICI.Length;
x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];
//设置JPEG编码
break;
}
}
if (jpegICI != null)
{
outBmp.Save(newFile, jpegICI, encoderParams);
}
else
{
outBmp.Save(newFile,
thisFormat);
}
img.Dispose();
outBmp.Dispose();
}
}
评论: 0 | 引用: 0 | 查看次数: 4783
发表评论
请登录后再发表评论!