protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
int x1 = Convert.ToInt32(Request["x1"]);
int y1 = Convert.ToInt32(Request["y1"]);
int x2 = Convert.ToInt32(Request["x2"]);
int y2 = Convert.ToInt32(Request["y2"]);
HttpFileCollection files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
file.SaveAs(Server.MapPath("Upload/" + file.FileName));
//设置缩略图
int Thumbnailwidth = 400;
int Thumbnailheight = 300;
//新建一个bmp图片
Bitmap bitmap = new Bitmap(Thumbnailwidth, Thumbnailheight);
//新建一个画板
Graphics graphic = Graphics.FromImage(bitmap);
//设置高质量插值法
graphic.InterpolationMode = InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
graphic.SmoothingMode = SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
graphic.Clear(System.Drawing.Color.Transparent);
//原图片
Bitmap originalImage = new Bitmap(file.InputStream);
//在指定位置并且按指定大小绘制原图片的指定部分
graphic.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, Thumbnailwidth, Thumbnailheight),
new System.Drawing.Rectangle(0, 0, originalImage.Width, originalImage.Height),
System.Drawing.GraphicsUnit.Pixel);
//得到缩略图
System.Drawing.Image ThumbnailImage = System.Drawing.Image.FromHbitmap(bitmap.GetHbitmap());
//创建选择图片
Bitmap selectbitmap = new Bitmap(x2-x1, y2-y1);
//新建一个画板
Graphics selectgraphic = Graphics.FromImage(selectbitmap);
//裁切
selectgraphic.DrawImage(ThumbnailImage, 0, 0, new Rectangle(x1, y1, x2 - x1, y2 - y1), GraphicsUnit.Pixel);
//保存
selectbitmap.Save(Server.MapPath("Upload/"+Guid.NewGuid() + file.FileName), ImageFormat.Jpeg);
//todo:将上述资源释放
}
}
}
|