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