asp.net 生成缩略图、为图片添加文字水印、图片水印等功能

简介: 最近挺嫌的荒,不知道写点什么好,就在网上找了个类似的例子,自己重新写了一遍,稍微改了一下。希望对大家有用 实现了生成缩略图、为图片添加文字水印、图片水印等功能 源代码打包下载:http://files.

最近挺嫌的荒,不知道写点什么好,就在网上找了个类似的例子,自己重新写了一遍,稍微改了一下。希望对大家有用

实现了生成缩略图、为图片添加文字水印、图片水印等功能

源代码打包下载: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">
&nbsp;<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();
}

}

  

 

 

版权

作者:灵动生活 郝宪玮

出处:http://www.cnblogs.com/ywqu

如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,

img_2c313bac282354945ea179a807d7e70d.jpg

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

相关文章
|
10天前
|
存储 文字识别 C#
.NET开源免费、功能强大的 Windows 截图录屏神器
今天大姚给大家分享一款.NET开源免费(基于GPL3.0开源协议)、功能强大、简洁灵活的 Windows 截图、录屏、Gif动图制作神器:ShareX。
|
3月前
|
SQL 存储 开发框架
分享107个ASP贺卡图片源码,总有一款适合您
分享107个ASP贺卡图片源码,总有一款适合您
28 1
|
3月前
|
存储 SQL 开发框架
分享88个ASP贺卡图片源码,总有一款适合您
分享88个ASP贺卡图片源码,总有一款适合您
21 2
|
3月前
|
SQL 开发框架 JavaScript
分享66个ASP贺卡图片源码,总有一款适合您
分享66个ASP贺卡图片源码,总有一款适合您
29 0
|
3月前
|
数据安全/隐私保护
.net给图片增加水印和生成图片缩略图
.net给图片增加水印和生成图片缩略图
31 0
|
关系型数据库 MySQL
【Mysql】服务没有响应控制功能。 请键入 NET HELPMSG 2186 以获得更多的帮助。
解决方法: 1. 下载dll文件 https://www.aliyundrive.com/s/oV6GgghtPkN 2.将文件放置在mysql bin文件夹下 3. 重新启动Mysql,发现启动成功了!🚀
725 0
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
|
2月前
|
C# Windows
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
|
7月前
|
Apache
基于commons-net实现ftp创建文件夹、上传、下载功能.
基于commons-net实现ftp创建文件夹、上传、下载功能.
106 0
|
9月前
|
移动开发 监控 网络协议
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)