导读部分
-------------------------------------------------------------------------------------------------------------
C#基类|C#自定义类|C#帮助类--系列导航文章
http://www.sufeinet.com/thread-655-1-1.html
直接看代码吧,
大家可以直接复制使用
[C#] 纯文本查看 复制代码 /// <summary>
/// 类说明:SmallImage类,
/// 编码日期:2012-08-20
/// 编 码 人: 苏飞
/// 联系方式:361983679 Email:[url=mailto:sufei.1013@163.com]sufei.1013@163.com[/url] Blogs:[url=http://www.sufeinet.com]http://www.sufeinet.com[/url]
/// 修改日期:2012-08-20
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SystemImage
{
public class SmallImage
{
//产生缩略图,高是100像素,宽按比例缩小
public static void GetSmallImage(string originalImagePath, string thumbnailPath, int width, int height)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
if (originalImage.Width < originalImage.Height)
{
width = originalImage.Width * width / originalImage.Height;//按比例指定宽度,高度固定不变,是100个像素,整个图像按比例缩小
}
else
{
height = originalImage.Height * height / originalImage.Width;
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap(width,height);
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;//.HighQualityBicubic;//设置高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;//.HighQuality;//设置高质量,低速度呈现平滑程度
g.Clear(Color.Transparent); //清空画布并以透明背景色填充
g.DrawImage(originalImage, new Rectangle(0, 0, width, height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
//检查大图像,如果宽超过四百,或高超过六百,就按比例缩放
public static bool CheckBigAreaImg(string url,string newUrl)
{
bool result = false;
using (System.Drawing.Image img = Bitmap.FromFile(url))
{
int height = img.Size.Height;//获得用户上传图片的宽度和高度
int width = img.Size.Width;
if (height > 800 || width > 530)
{
result = true;
GetSmallImage(url, newUrl, 500, 500);
}
}
if (result)
{
File.Delete(url);
}
return result;
}
}
}
|