前台页面 tupian.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="tupian.aspx.cs" Inherits="tupian" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type ="text/css" >
.style1{font-family :幼圆;font-size :13px;color:#336699;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="上传" Width="65px" />
<span class ="style1"> 设置图片宽度</span><asp:TextBox ID="TextBox1"
runat="server" Width="90px"></asp:TextBox>
<span class ="style1"> 设置图片高度</span><asp:TextBox ID="TextBox2"
runat="server" Width="90px"></asp:TextBox>
<asp:Button ID="Button2" runat="server" οnclick="Button2_Click"
style="height: 21px" Text="保存图片" />
<br />
<asp:Image ID="Image1" runat="server" />
</div>
</form>
</body>
</html>
后台tupian.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
public partial class tupian : System.Web.UI.Page
{
protected static string path="";
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 缩小裁剪图片
/// </summary>
/// <param name="int_Width">要缩小裁剪图片宽度</param>
/// <param name="int_Height">要缩小裁剪图片长度</param>
/// <param name="input_ImgUrl">要处理图片路径</param>
/// <param name="out_ImgUrl">处理完毕图片路径</param>
///
public void ImgReduceCutOut(int int_Width, int int_Height, string input_ImgUrl, string out_ImgUrl)
{
// ===上传标准图大小===
int int_Standard_Width = 160;
int int_Standard_Height = 160;
int Reduce_Width = 0; // 缩小的宽度
int Reduce_Height = 0; // 缩小的高度
int CutOut_Width = 0; // 裁剪的宽度
int CutOut_Height = 0; // 裁剪的高度
int level = 100; //缩略图的质量 1-100的范围
// ===获得缩小,裁剪大小===
if (int_Standard_Height * int_Width / int_Standard_Width > int_Height)
{
Reduce_Width = int_Width;
Reduce_Height = int_Standard_Height * int_Width / int_Standard_Width;
CutOut_Width = int_Width;
CutOut_Height = int_Height;
}
else if (int_Standard_Height * int_Width / int_Standard_Width < int_Height)
{
Reduce_Width = int_Standard_Width * int_Height / int_Standard_Height;
Reduce_Height = int_Height;
CutOut_Width = int_Width;
CutOut_Height = int_Height;
}
else
{
Reduce_Width = int_Width;
Reduce_Height = int_Height;
CutOut_Width = int_Width;
CutOut_Height = int_Height;
}
// ===通过连接创建Image对象===
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(input_ImgUrl));
// ===缩小图片===
System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(Reduce_Width, Reduce_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Bitmap bm = new Bitmap(thumbnailImage);
// ===处理JPG质量的函数===
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
ici = codec;
}
EncoderParameters ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)level);
//bm.Save(Server.MapPath("2.jpg"), ici, ep);
// ===裁剪图片===
Rectangle cloneRect = new Rectangle(0, 0, CutOut_Width, CutOut_Height);
PixelFormat format = bm.PixelFormat;
Bitmap cloneBitmap = bm.Clone(cloneRect, format);
oldimage.Dispose();
// ===保存图片===
//Debug.WriteLine(string.Format("the image height is {0} width is {1}", cloneBitmap.Height, cloneBitmap.Width));
cloneBitmap.Save(Server.MapPath(out_ImgUrl), ici, ep);
}
public bool ThumbnailCallback()
{
return true;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1 .HasFile == false)
{
Response.Write("<script lanage='javascript'> alert('请上传图片!')</script>");
}
else
{
string fType = FileUpload1.PostedFile.ContentType;//获取图像的类型
if (fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/jpeg" || fType == "image/x-png")
{
//获取文件信息
//System .IO.FileInfo file = new System .IO.FileInfo(FileUpload1.PostedFile.FileName);
Guid guid = Guid.NewGuid();
string stamp = guid.ToString("N");
path = "~/UploadFiles/" + stamp + ".jpg";
FileUpload1.SaveAs(Server.MapPath(path));
Image1.ImageUrl = path;
//System.Drawing.Image imgtemp = System.Drawing.Image.FromFile(path);
TextBox1.Text = Image1.Width.ToString();
TextBox2.Text = Image1.Height.ToString();
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(TextBox1.Text.Trim());
int j = Convert.ToInt32(TextBox2.Text.Trim());
ImgReduceCutOut(i,j, path, path);
Image1.ImageUrl = path;
}
}