生成高清晰缩略图
来源:互联网 发布:卡通配音软件 编辑:程序博客网 时间:2024/04/30 11:49
private static Bitmap CreateThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect)
{
// return the source image if it's smaller than the designated thumbnail
if (source.Width < thumbWi && source.Height < thumbHi) return source;
System.Drawing.Bitmap ret = null;
try
{
int wi, hi;
wi = thumbWi;
hi = thumbHi;
if (maintainAspect)
{
// maintain the aspect ratio despite the thumbnail size parameters
if (source.Width > source.Height)
{
wi = thumbWi;
hi = (int)(source.Height * ((decimal)thumbWi / source.Width));
}
else
{
hi = thumbHi;
wi = (int)(source.Width * ((decimal)thumbHi / source.Height));
}
}
// original code that creates lousy thumbnails
// System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
ret = new Bitmap(wi, hi);
using (Graphics g = Graphics.FromImage(ret))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, wi, hi);
g.DrawImage(source, 0, 0, wi, hi);
g.Dispose();
}
}
catch
{
ret = null;
}
return ret;
}
public static void CreateCompressThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect, string SavePathThumb, string fileName)
{
using (System.Drawing.Image myThumbnail = CreateThumbnail(source, thumbWi, thumbHi, maintainAspect))
{
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] quality = new long[1];
quality[0] = 75;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];
break;
}
}
Bitmap bmp = new Bitmap(myThumbnail);
bmp.Save(SavePathThumb+fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
//myThumbnail.Save(Path.Combine(SavePathThumb, fileName + ".jpg"), jpegICI, encoderParams);
bmp.Dispose();
myThumbnail.Dispose();
{
// return the source image if it's smaller than the designated thumbnail
if (source.Width < thumbWi && source.Height < thumbHi) return source;
System.Drawing.Bitmap ret = null;
try
{
int wi, hi;
wi = thumbWi;
hi = thumbHi;
if (maintainAspect)
{
// maintain the aspect ratio despite the thumbnail size parameters
if (source.Width > source.Height)
{
wi = thumbWi;
hi = (int)(source.Height * ((decimal)thumbWi / source.Width));
}
else
{
hi = thumbHi;
wi = (int)(source.Width * ((decimal)thumbHi / source.Height));
}
}
// original code that creates lousy thumbnails
// System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
ret = new Bitmap(wi, hi);
using (Graphics g = Graphics.FromImage(ret))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, wi, hi);
g.DrawImage(source, 0, 0, wi, hi);
g.Dispose();
}
}
catch
{
ret = null;
}
return ret;
}
public static void CreateCompressThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect, string SavePathThumb, string fileName)
{
using (System.Drawing.Image myThumbnail = CreateThumbnail(source, thumbWi, thumbHi, maintainAspect))
{
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] quality = new long[1];
quality[0] = 75;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];
break;
}
}
Bitmap bmp = new Bitmap(myThumbnail);
bmp.Save(SavePathThumb+fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
//myThumbnail.Save(Path.Combine(SavePathThumb, fileName + ".jpg"), jpegICI, encoderParams);
bmp.Dispose();
myThumbnail.Dispose();
}
调用方法:
Bitmap bitmap = new Bitmap("f://1.jpg");
CreateCompressThumbnail(bitmap, 600, 600, true, "f://", "2.jpg");
- 生成高清晰缩略图
- 生成高清晰缩略图
- 生成高清晰缩略图
- 生成高清晰缩略图
- asp.net生成高清晰缩略图
- asp.net生成高清晰缩略图
- C#(.NET)生成高质量(清晰)缩略图
- Asp.Net 上传图片并生成高清晰缩略图
- .Net 上传图片并生成高清晰缩略图
- ASP.Net 上传图片并生成高清晰缩略图
- ASP.Net 上传图片并生成高清晰缩略图
- ASP.Net 上传图片并生成高清晰缩略图
- ASP.Net 上传图片并生成高清晰缩略图
- Asp.Net 上传图片并生成高清晰缩略图
- .Net 上传图片并生成高清晰缩略图
- Asp.Net 上传图片并生成高清晰缩略图
- Asp.Net 上传图片并生成高清晰缩略图
- Asp.Net 上传图片并生成高清晰缩略图
- 成功的12条黄金法则
- ARM Instruction Formats and Timings
- 在ASP.NET中用三个DropDownList控件方便的选择年月日
- Oracle的rownum原理和使用(转)
- DB2 字段和表
- 生成高清晰缩略图
- 设置水晶报表隔行显示不同颜色
- [总结]提高程序性能的一些方法
- 服务器网页缓存的深入分析
- JDBC与数据库的连接字符串
- 安装SQL Server2000时程序挂起的问题
- 安装Apache
- Linux系统下硬盘的基本知识
- 快速备份一个表以及里面的数据