using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MakeSmallImg("F:\\myproject\\web\\2006092809470622.jpg", Server.MapPath(@"Photo\a.jpg"), 100, 100, "Cut");
}
/// <summary>
///
/// </summary>
/// <param name="originalImagePath"> ( )</param>
/// <param name="thumbnailPath"> ( )</param>
/// <param name="width"> </param>
/// <param name="height"> </param>
/// <param name="mode"> </param>
public void MakeSmallImg(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW":// ( )
break;
case "W":// ,
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H":// ,
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut":// ( )
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
// bmp
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//
Graphics g = System.Drawing.Graphics.FromImage(i);
//
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
// ,
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
g.Clear(Color.Transparent);
//
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
// jpg
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}