原文地址:http://www.qqroom.com/Member/Blog/ItemView.aspx?BlogItemID=397&UserName=Jerry
今天突然看到这篇帖子,代码如下
我在页面上放了一个Input(file),一个按钮Button1,以及一个Image
后台代码:
今天突然看到这篇帖子,代码如下
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace LabWeb
{
public class XuDrawImage
{
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = 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图片
Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置双三次的高质量插值法
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = 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();
}
}
}
}
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace LabWeb
{
public class XuDrawImage
{
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = 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图片
Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置双三次的高质量插值法
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = 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();
}
}
}
}
我在页面上放了一个Input(file),一个按钮Button1,以及一个Image
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="XuDrawImageTest.aspx.cs" Inherits="LabWeb.XuDrawImageTest" EnableViewState="true" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="UpFile" runat="server" style="width: 400px" type="file" />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>WH</asp:ListItem>
<asp:ListItem>W</asp:ListItem>
<asp:ListItem>H</asp:ListItem>
<asp:ListItem>Cut</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传图片!" />
<asp:Image ID="Image1" runat="server" /></div>
</form>
</body>
</html>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="UpFile" runat="server" style="width: 400px" type="file" />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>WH</asp:ListItem>
<asp:ListItem>W</asp:ListItem>
<asp:ListItem>H</asp:ListItem>
<asp:ListItem>Cut</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传图片!" />
<asp:Image ID="Image1" runat="server" /></div>
</form>
</body>
</html>
后台代码:
protected void Button1_Click(object sender, EventArgs e)
{
if (this.UpFile.PostedFile.FileName.Trim() != "")
{
//取得上传文件的总路径
string imagePath = this.UpFile.PostedFile.FileName;
//取出图片的类型
string imageType = Path.GetExtension(imagePath).ToUpper();
//取得图片名称
string imageName = Path.GetFileName(imagePath).ToString();
imageName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + imageType;
Response.Write(imagePath+"<br>"+imageType+"<br>"+imageName);
//判断上传图片类型
if (imageType == ".JPG" || imageType == ".BMP" || imageType == ".JPEG" || imageType == ".GIF")
{
//保存原图
string imageSavePath = Server.MapPath("/ProductPicFile" + "\\" + imageName);
this.UpFile.PostedFile.SaveAs(imageSavePath);
//生成第一幅缩略图
string drawMode = this.DropDownList1.SelectedValue.ToString();
string drawImageName= "S_" + DateTime.Now.ToString("yyyyMMddhhmmss")+".JPG";
string drawImageSavePath = Server.MapPath("/ProductPicFile"+"\\" +drawImageName);
LabWeb.XuDrawImage.MakeThumbnail(imageSavePath, drawImageSavePath, 100, 50, drawMode);
//定义image类的对象
System.Drawing.Image image, shuiyinImg;
//为上传的图片建立引用
image = System.Drawing.Image.FromFile(drawImageSavePath);
/*************加水印开始*********************************************/
shuiyinImg = System.Drawing.Image.FromFile(Server.MapPath("/Images/FlagImg/") + "Flag200_100.png");
Graphics g = Graphics.FromImage(image);
////设置双三次的高质量插值法
//g.InterpolationMode = InterpolationMode.HighQualityBicubic;
////设置高质量,低速度呈现平滑程度
//g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(shuiyinImg, new Rectangle(image.Width - shuiyinImg.Width, image.Height - shuiyinImg.Height,
shuiyinImg.Width, shuiyinImg.Height), 0, 0, shuiyinImg.Width, shuiyinImg.Height, GraphicsUnit.Pixel);
g.Dispose();
string drawFlagPath = Server.MapPath("/ProductPicFile" +"\\ff_"+ drawImageName);
image.Save(drawFlagPath);
image.Dispose();
/************加水印的过程结束*********************************************/
//删除原图
if (File.Exists(imageSavePath))
{
File.Delete(imageSavePath);
}
this.Image1.ImageUrl = drawFlagPath;
}
}
{
if (this.UpFile.PostedFile.FileName.Trim() != "")
{
//取得上传文件的总路径
string imagePath = this.UpFile.PostedFile.FileName;
//取出图片的类型
string imageType = Path.GetExtension(imagePath).ToUpper();
//取得图片名称
string imageName = Path.GetFileName(imagePath).ToString();
imageName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + imageType;
Response.Write(imagePath+"<br>"+imageType+"<br>"+imageName);
//判断上传图片类型
if (imageType == ".JPG" || imageType == ".BMP" || imageType == ".JPEG" || imageType == ".GIF")
{
//保存原图
string imageSavePath = Server.MapPath("/ProductPicFile" + "\\" + imageName);
this.UpFile.PostedFile.SaveAs(imageSavePath);
//生成第一幅缩略图
string drawMode = this.DropDownList1.SelectedValue.ToString();
string drawImageName= "S_" + DateTime.Now.ToString("yyyyMMddhhmmss")+".JPG";
string drawImageSavePath = Server.MapPath("/ProductPicFile"+"\\" +drawImageName);
LabWeb.XuDrawImage.MakeThumbnail(imageSavePath, drawImageSavePath, 100, 50, drawMode);
//定义image类的对象
System.Drawing.Image image, shuiyinImg;
//为上传的图片建立引用
image = System.Drawing.Image.FromFile(drawImageSavePath);
/*************加水印开始*********************************************/
shuiyinImg = System.Drawing.Image.FromFile(Server.MapPath("/Images/FlagImg/") + "Flag200_100.png");
Graphics g = Graphics.FromImage(image);
////设置双三次的高质量插值法
//g.InterpolationMode = InterpolationMode.HighQualityBicubic;
////设置高质量,低速度呈现平滑程度
//g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(shuiyinImg, new Rectangle(image.Width - shuiyinImg.Width, image.Height - shuiyinImg.Height,
shuiyinImg.Width, shuiyinImg.Height), 0, 0, shuiyinImg.Width, shuiyinImg.Height, GraphicsUnit.Pixel);
g.Dispose();
string drawFlagPath = Server.MapPath("/ProductPicFile" +"\\ff_"+ drawImageName);
image.Save(drawFlagPath);
image.Dispose();
/************加水印的过程结束*********************************************/
//删除原图
if (File.Exists(imageSavePath))
{
File.Delete(imageSavePath);
}
this.Image1.ImageUrl = drawFlagPath;
}
}
一个人的孤独,一个人的封闭,不如一起努力!