RT
解决方案 »
- 删除图片时报:正由另一进程使用,因此该进程无法访问
- asp.net歌手按字母分类排列
- 求助 用户控件的问题
- 在前端脚本块中如何捕捉页面回发和加载事件
- Repeater中绑定的CheckBox,如果选取状态改变了触发的是什么事件?
- 为了学习FormsAuthentication 类, 在MSDN中找了一个例子, 但是不会用, 请问这个东西是怎样用的? 要输入什么用户名与密码才能通过? 它们
- 一个简单的问题-------------在线等(急...............)
- 自定义控件不放在APP_code文件夹里,会不会被编译成.dll文件?
- 高分请教高手模板列单击后数据处理的问题
- ASP.NET如何将中文字符提交输入到SQL
- 还有一个问题要解决
- 同一部主机的两个不同域名的网站(都是asp.net写的),可不可以共享Session?
只是猜,我还没试过,呵呵
http://www.imagecomponent.net/Products/Figures/overlappedImage_50withoutborder.jpg
/// 添加图片水印
/// </summary>
private void ImageAddImage(ref Image mg)
{
if (_addimage == null)
throw new SysException("请设置图片路径", true);
//检查图片文件存在与否
if (File.Exists(_addimage))
{
Image imgWater = new Bitmap(this._addimage);
int wmWidth = imgWater.Width;
int wmHeight = imgWater.Height; ImageAttributes imageAttributes =
new ImageAttributes();
ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable,
ColorAdjustType.Bitmap);
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.2f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.5f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
}; ColorMatrix wmColorMatrix = new
ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
int xPosOfWm = 10;
int yPosOfWm = 10; //画图区
Graphics grWater = Graphics.FromImage(mg);
grWater.DrawImage(imgWater, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes);
grWater.Dispose();
imgWater.Dispose(); }
}
我也关注,我觉得最好不要用组件,原来做
/// 生成缩略图
/// </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 )
{
#region 生成缩略图
System.Drawing.Image originalImage = System.Drawing.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图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.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();
}
#endregion
} /**//// <summary>
/// 在图片上增加文字水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_sy">生成的带文字水印的图片路径</param>
public static void AddShuiYinWord( string Path, string Path_sy )
{
#region 在图片上增加文字水印
string addText = "测试水印";
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue); g.DrawString(addText, f, b, 15, 15);
g.Dispose(); image.Save(Path_sy);
image.Dispose();
#endregion
} /**//// <summary>
/// 在图片上生成图片水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_syp">生成的带图片水印的图片路径</param>
/// <param name="Path_sypf">水印图片路径</param>
public static void AddShuiYinPic( string Path, string Path_syp, string Path_sypf )
{
#region 在图片上生成图片水印
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width,copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
g.Dispose(); image.Save(Path_syp);
image.Dispose();
#endregion
} /// <summary>
/// 处理上传图片功能
/// </summary>
/// <param name="File1"></param>
/// <param name="savePath"></param>
/// <param name="isThumbnail"></param>
/// <param name="delPath"></param>
/// <returns></returns>
public static string uploadFile( System.Web.UI.HtmlControls.HtmlInputFile File1,string savePath,bool isThumbnail,string delPath )
{
#region 处理上传图片功能
try
{
if ( File1.PostedFile.FileName.Trim() != "" )
{
string fileContentType = File1.PostedFile.ContentType.ToLower();
if ( fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg" )
{
System.IO.FileInfo file = new System.IO.FileInfo( File1.PostedFile.FileName );
string fileName = System.DateTime.Now.ToString( "yyyyMMddhhmmss" ) + file.Extension; // 文件名称
string fileName_s = "s_" + fileName;
string webFilePath = System.Web.HttpContext.Current.Server.MapPath( savePath + fileName ); // 服务器端文件路径
string webFilePath_s = System.Web.HttpContext.Current.Server.MapPath( savePath + fileName_s); // 服务器端缩略图路径 if ( !System.IO.File.Exists( webFilePath ) )
{
#region
File1.PostedFile.SaveAs( webFilePath ); if ( delPath != "" && System.IO.File.Exists( System.Web.HttpContext.Current.Server.MapPath(savePath + delPath) ) )//如果存在该文件,则删除
{
System.IO.File.Delete( System.Web.HttpContext.Current.Server.MapPath(savePath + delPath) );
} if ( isThumbnail )//如果是需要处理缩略图,则执行这里
{
MakeThumbnail( webFilePath, webFilePath_s, 130, 100, "Cut" ); // 生成缩略图方法 if ( delPath != "" && System.IO.File.Exists( System.Web.HttpContext.Current.Server.MapPath(savePath + "s_" + delPath) ) )//如果存在该文件,则删除
{
System.IO.File.Delete( System.Web.HttpContext.Current.Server.MapPath(savePath + "s_" + delPath) );
}
}
#endregion
return fileName;
}
else
{
return "1";//表示存在相同文件名
}
}
else
{
return "2";//表示文件类型限制
}
}
else
{
return "3";//表示没有选择上传文件
}
}
catch
{
return "0";//表示系统错误
}
#endregion
}
/// 处理上传图片功能
/// </summary>
/// <param name="File1">这个你看了就知道</param>
/// <param name="savePath">保存路径,比如“../Upfiles/”,直到路径,不到文件名称</param>
/// <param name="isThumbnail">是否需要生成缩略图</param>
/// <param name="delPath">删除文件的文件名称,如果不需要删除,则留空 ""</param>
/// <returns></returns>
public static string uploadFile( System.Web.UI.HtmlControls.HtmlInputFile File1,string savePath,bool isThumbnail,string delPath )
前台页面:
<%@ Page language="c#" Codebehind="MikeCat_WaterMark.aspx.cs" AutoEventWireup="false" Inherits="MikeCat.MikeCat_WaterMark" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>MikeCat_WaterMark</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT id="File1" style="Z-INDEX: 101; LEFT: 144px; WIDTH: 400px; POSITION: absolute; TOP: 88px; HEIGHT: 22px"
type="file" size="47" name="File1" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 176px; POSITION: absolute; TOP: 136px" runat="server"
Text="上传并加文字水印"></asp:Button>
<asp:Button id="Button2" style="Z-INDEX: 103; LEFT: 352px; POSITION: absolute; TOP: 136px" runat="server"
Text="上传并加图片水印"></asp:Button>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 104; LEFT: 552px; POSITION: absolute; TOP: 88px"
runat="server" ErrorMessage="*" ControlToValidate="File1"></asp:RequiredFieldValidator>
<DIV style="Z-INDEX: 105; LEFT: 16px; WIDTH: 100%; POSITION: absolute; TOP: 168px; HEIGHT: 100px"
ms_positioning="GridLayout">
<asp:Image id="Image1" style="Z-INDEX: 107; LEFT: 56px; POSITION: absolute; TOP: 16px" runat="server"
ImageAlign="Middle"></asp:Image></DIV>
<asp:Label id="Label1" style="Z-INDEX: 107; LEFT: 176px; POSITION: absolute; TOP: 40px" runat="server">欢迎光临老猫的理想--上传图片并加入水印</asp:Label>
</form>
</body>
</HTML>
后台页面:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;namespace MikeCat
{
/// <summary>
/// MikeCat_WaterMark 的摘要说明。
/// *******************************
/// 作者:迈克老猫
/// 功能:上传图片加入水印
/// EMAIL:mikecat#mikecat.net
/// *******************************
/// </summary>
public class MikeCat_WaterMark : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Image Image1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
Image1.ImageUrl="mikepp.gif";
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
if(File1.PostedFile.FileName.Trim()!="")
{
//上传文件
string extension = Path.GetExtension(File1.PostedFile.FileName).ToLower();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
string path = Server.MapPath(".") + "/upload/" + fileName + extension;
File1.PostedFile.SaveAs(path); //加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font("Verdana",16);
Brush b = new SolidBrush(Color.Blue);
string addText = "老猫的理想http://www.mikecat.net";
g.DrawString(addText, f, b, 10, 10);
g.Dispose(); //保存加水印过后的图片,删除原始图片
string newPath = Server.MapPath(".") + "/upload/" + fileName + "_new" + extension;
image.Save(newPath);
image.Dispose();
if(File.Exists(path))
{
File.Delete(path);
}
Image1.ImageUrl=newPath;
// Response.Redirect(newPath);
} } private void Button2_Click(object sender, System.EventArgs e)
{
//上传文件
string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
string path = Server.MapPath(".") + "/upload/" + fileName + extension;
File1.PostedFile.SaveAs(path);
//加图片水印
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/mikepp.gif");
Graphics g = Graphics.FromImage(image);
g.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
g.Dispose(); //保存加水印过后的图片,删除原始图片
string newPath = Server.MapPath(".") + "/upload/" + fileName + "_new" + extension;
image.Save(newPath);
image.Dispose();
if(File.Exists(path))
{
File.Delete(path);
} Image1.ImageUrl=newPath;
}
}
}