欢迎光临

木子屋

标题: C#高质量(清晰)缩略图生成类

作者: dnawo

日期: 2012-06-13 05:08 PM

分类: Win编程

内容: 复制内容到剪贴板 程序代码using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ConsoleApplication1
{
public class ImageHelper
{
/// <summary>
/// 生成缩略图(缩小原图到指定范围)
/// </summary>
/// <param name="srcFile">原图路径</param>
/// <param name="destFile">缩略图路径</param>
/// <param name="maxWidth">缩略图最大宽度</param>
/// <param name="maxHeight">缩略图最大高度</param>
/// <returns></returns>
public static bool GetFixedRatioImage(string srcFile, string destFile, int maxWidth, int maxHeight)
{
if (!File.Exists(srcFile))
return false;
int destWidth = 0, destHeight = 0;
using (Image image = Image.FromFile(srcFile))
{
if (image.Width > maxWidth && image.Height > maxHeight)
{
destWidth = maxWidth;
destHeight = maxWidth * image.Height / image.Width;
if (destHeight > maxHeight)
{
destWidth = maxHeight * image.Width / image.Height;
destHeight = maxHeight;
}
}
else
{
destWidth = image.Width;
destHeight = image.Height;
}
using (Image bitmap = new Bitmap(destWidth, destHeight))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.InterpolationMode = InterpolationMode.High;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
bitmap.Save(destFile, ImageFormat.Jpeg);
}
}
}
return true;
}
/// <summary>
/// 生成缩略图(裁切原图并缩小到指定尺寸)
/// </summary>
/// <param name="srcFile">原图路径</param>
/// <param name="destFile">缩略图路径</param>
/// <param name="destWidth">缩略图宽度</param>
/// <param name="destHeight">缩略图高度</param>
/// <returns></returns>
public static bool GetFixedSizeImage(string srcFile, string destFile, int destWidth, int destHeight)
{
if (!File.Exists(srcFile))
return false;
int srcWidth = 0, srcHeight = 0;
using (Image image = Image.FromFile(srcFile))
{
if (image.Width > destWidth && image.Height > destHeight)
{
if ((float)image.Width / (float)destWidth > (float)image.Height / (float)destHeight)
{
srcWidth = image.Height * destWidth / destHeight;
srcHeight = image.Height;
}
else
{
srcWidth = image.Width;
srcHeight = image.Width * destHeight / destWidth;
}
}
else
{
srcWidth = image.Width;
srcHeight = image.Height;
destWidth = image.Width;
destHeight = image.Height;
}
using (Image bitmap = new Bitmap(destWidth, destHeight))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.InterpolationMode = InterpolationMode.High;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
bitmap.Save(destFile, ImageFormat.Jpeg);
}
}
}
return true;
}
}
}

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.691 ms

返回

标题: C#高质量(清晰)缩略图生成类

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.691 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.707 ms

返回