最近挺嫌的荒,不知道写点什么好,就在网上找了个类似的例子,自己重新写了一遍,稍微改了一下。希望对大家有用
实现了生成缩略图、为图片添加文字水印、图片水印等功能
源代码打包下载:http://files.cnblogs.com/ywqu/ThumPicture.rar
前台代码:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<table>
<tr>
<td style="width: 100px">
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
<td style="width: 100px">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></td>
</tr>
<tr>
<td style="height: 20px"></td>
<td style="height: 20px"></td>
</tr>
<tr>
<td style="height: 20px;" colspan="2">
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text="Label"></asp:Label></td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 100px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台代码
Code
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileType = FileUpload1.PostedFile.ContentType;
if (fileType == "image/bmp" || fileType == "image/gif" || fileType == "image/jpg")
{
string name = FileUpload1.PostedFile.FileName; //文件路径
FileInfo fileInfo = new FileInfo(name);
string fileName = fileInfo.Name; //文件名称
string sFileName = "x_" + fileName; //缩略图文件名
string syFileNameText = "text_" + fileName; //水印图文件名(文字)
string syFileNamePic = "pic_" + fileName; //水印图文件名(图片)
string filePath = Server.MapPath("ImgUpLoad/" + fileName); //服务器端文件路径
string sFilePath = Server.MapPath("ImgUpLoad/" + sFileName);//缩略图文件路径
string syFileTextPath = Server.MapPath("ImgUpLoad/" + syFileNameText);//带水印的的图片路径(文字)
string syFilePicPath = Server.MapPath("ImgUpLoad/" + syFileNamePic);
string syPic = Server.MapPath("hello36.png");
if (!File.Exists(filePath))
{
try
{
FileUpload1.SaveAs(filePath);
ThumNail.AddWaterWord(filePath, syFileTextPath);
ThumNail.AddWaterPic(filePath, syPic, syFilePicPath);
ThumNail.MakeThumNail(filePath, sFilePath, 130, 130, "Cut");
Label1.Text = "图片操做:生成缩略图、添加水印成功!";
}
catch (Exception ex)
{
Label1.Text = "文件操作失败,失败原因:" + ex.Message;
}
}
else
{
Label1.Text = "文件已经存在,请重新命名后上传!";
}
}
else
{
Label1.Text = "文件格式不符,允许的格式:.gif .jpg .bmp";
}
}
Label1.Visible = true;
}
生成缩略图、为图片添加文字水印、图片水印等功能单独放到一个类(ThumNail.cs)里面了
Code
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;
/// <summary>
/// 生成缩略图、为图片添加文字水印、图片水印的类
/// </summary>
public class ThumNail
{
public ThumNail()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="orginalImagePat">原图片地址</param>
/// <param name="thumNailPath">缩略图地址</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="model">生成缩略的模式</param>
public static void MakeThumNail(string originalImagePath,string thumNailPath,int width,int height,string model)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int thumWidth = width; //缩略图的宽度
int thumHeight = height; //缩略图的高度
int x = 0;
int y = 0;
int originalWidth = originalImage.Width; //原始图片的宽度
int originalHeight = originalImage.Height; //原始图片的高度
switch (model)
{
case "HW": //指定高宽缩放,可能变形
break;
case "W": //指定宽度,高度按照比例缩放
thumHeight = originalImage.Height * width / originalImage.Width;
break;
case "H": //指定高度,宽度按照等比例缩放
thumWidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut":
if ((double)originalImage.Width / (double)originalImage.Height > (double)thumWidth / (double)thumHeight)
{
originalHeight = originalImage.Height;
originalWidth = originalImage.Height * thumWidth / thumHeight;
y = 0;
x = (originalImage.Width - originalWidth) / 2;
}
else
{
originalWidth = originalImage.Width;
originalHeight = originalWidth * height / thumWidth;
x = 0;
y = (originalImage.Height - originalHeight) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(thumWidth,thumHeight);
//新建一个画板
System.Drawing.Graphics graphic=System.Drawing.Graphics.FromImage(bitmap);
//设置高质量查值法
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
graphic.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
graphic.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, thumWidth, thumHeight), new System.Drawing.Rectangle(x, y, originalWidth, originalHeight), System.Drawing.GraphicsUnit.Pixel);
try
{
bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
graphic.Dispose();
}
}
/// <summary>
/// 在图片上添加文字水印
/// </summary>
/// <param name="path">要添加水印的图片路径</param>
/// <param name="syPath">生成的水印图片存放的位置</param>
public static void AddWaterWord(string path,string syPath)
{
string syWord = "http://www.hello36.cn";
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
//新建一个画板
System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(image);
graphic.DrawImage(image,0,0,image.Width,image.Height);
//设置字体
System.Drawing.Font f = new System.Drawing.Font("Verdana",60);
//设置字体颜色
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
graphic.DrawString(syWord, f, b, 35, 35);
graphic.Dispose();
//保存文字水印图片
image.Save(syPath);
image.Dispose();
}
/// <summary>
/// 在图片上添加图片水印
/// </summary>
/// <param name="path">原服务器上的图片路径</param>
/// <param name="syPicPath">水印图片的路径</param>
/// <param name="waterPicPath">生成的水印图片存放路径</param>
public static void AddWaterPic(string path, string syPicPath, string waterPicPath)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
System.Drawing.Image waterImage = System.Drawing.Image.FromFile(syPicPath);
System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(image);
graphic.DrawImage(waterImage,new System.Drawing.Rectangle(image.Width-waterImage.Width,image.Height-waterImage.Height,waterImage.Width,waterImage.Height),0,0,waterImage.Width,waterImage.Height,System.Drawing.GraphicsUnit.Pixel);
graphic.Dispose();
image.Save(waterPicPath);
image.Dispose();
}
}