给图片添加水印效果图的函数(可以在图片上添加自己的版权和LOGO图片的水印) 【转载】
protected void Button1_ServerClick(object sender, System.EventArgs e)
{
if(File1.PostedFile!=null)
{
string fileName = File1.PostedFile.FileName ;
//取得上传文件的扩展名
string fileExtName =fileName.Substring(fileName.LastIndexOf("."));
//创建GUID,作为上传文件的唯一标识
System.Guid myGuid = System.Guid.NewGuid();
string savePath = Server.MapPath("~").ToString() + "//uploadfile//" + myGuid.ToString() + "." + fileExtName;
string newSavePath = Server.MapPath("~").ToString() + "//uploadfile1//" + myGuid.ToString() + "." + fileExtName;
string waterMarkPath = Server.MapPath("~").ToString() + "//uploadfile//watermark.bmp";
File1.PostedFile.SaveAs(savePath);
watermark myWatermark = new watermark();
myWatermark.MakeWatermark("北京朝阳",waterMarkPath,savePath,newSavePath);
// //文件的相关属性:文件名,文件类型,文件大小
// myFile.PostedFile.FileName;
// myFile.PostedFile.ContentType ;
// myFile.PostedFile.ContentLength.ToString();
}
}
{
if(File1.PostedFile!=null)
{
string fileName = File1.PostedFile.FileName ;
//取得上传文件的扩展名
string fileExtName =fileName.Substring(fileName.LastIndexOf("."));
//创建GUID,作为上传文件的唯一标识
System.Guid myGuid = System.Guid.NewGuid();
string savePath = Server.MapPath("~").ToString() + "//uploadfile//" + myGuid.ToString() + "." + fileExtName;
string newSavePath = Server.MapPath("~").ToString() + "//uploadfile1//" + myGuid.ToString() + "." + fileExtName;
string waterMarkPath = Server.MapPath("~").ToString() + "//uploadfile//watermark.bmp";
File1.PostedFile.SaveAs(savePath);
watermark myWatermark = new watermark();
myWatermark.MakeWatermark("北京朝阳",waterMarkPath,savePath,newSavePath);
// //文件的相关属性:文件名,文件类型,文件大小
// myFile.PostedFile.FileName;
// myFile.PostedFile.ContentType ;
// myFile.PostedFile.ContentLength.ToString();
}
}
看看 类的处理
/*
* 给图片设置水印效果图的函数
* 可以在图片上添加自己的版权和LOGO图片的水印
*
* 原作者:Joel Neubeck
*
* 本人在原作者的基础上进行了部分修改,并改为一个单独的函数
* 大家可以根据需要修改此代码
* 但请保留原作者信息.
*
* 星宿.net(zhuhee)
* 修改于2006-6-14
*
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace WebWaterMark.Components
{
/// <summary>
/// watermark 的摘要说明。
/// </summary>
public class watermark
{
public watermark()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 用于处理图片
/// 给图片加入水印
/// </summary>
/// <param name="Copyright">需要写入的版权信息</param>
/// <param name="MarkBmpPath">需要假如的logo图片</param>
/// <param name="photoPath">需要处理的图片的路径</param>
/// <param name="savePhotoPath">保存的图片路径</param>
public void MakeWatermark(string Copyright,string MarkBmpPath,string photoPath,string savePhotoPath)
{
//创建一个image对象,即要被处理的图片
Image imgPhoto = Image.FromFile(photoPath);
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//创建原始图片大小的Bitmap
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//将位图bmPhoto加载到Graphics对象
Graphics grPhoto = Graphics.FromImage(bmPhoto);
//创建一个需要填充水银的Image对象
Image imgWatermark = new Bitmap(MarkBmpPath);
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;
//------------------------------------------------------------
//第一步,插入版权信息
//------------------------------------------------------------
//设置图象的成相质量
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
//将原始图象绘制到grPhoto上
grPhoto.DrawImage(
imgPhoto, // 要绘制的Image对象
new Rectangle(0, 0, phWidth, phHeight), // 绘制图象的位置和大小
0, // 要绘制的原图象部分的左上角的X坐标
0, // 要绘制的原图象部分的左上角的Y坐标
phWidth, // 要绘制的原图象的高度
phHeight, // 要绘制的原图象的宽度
GraphicsUnit.Pixel); // 源矩形的度量单位
//-------------------------------------------------------
//字体大小放在一个数组中,最大字体为32.
//感觉用处不大,可以自己再修改这里
//-------------------------------------------------------
int[] sizes = new int[]{32,14,12,10,8,6,4};
Font crFont = null;
SizeF crSize = new SizeF();
//循环测试数组中所定义的字体大小是否适合版权信息,如果合适就使用此种字体大小
for (int i=0 ;i<6; i++)
{
//设置字体类型,可以单独提出,作为参数
crFont = new Font("迷你繁篆书", sizes[0], FontStyle.Bold);
//测量此种字体大小
crSize = grPhoto.MeasureString(Copyright, crFont);
if((ushort)crSize.Width < (ushort)phWidth)
break;
}
//给底部保留%3的空间
int yPixlesFromBottom = (int)(phHeight *.03);
//设置字体在图片中的位置
float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));
//float xCenterOfImg = (phWidth/2);
float xCenterOfImg = (phWidth-(crSize.Width)/2);
//设置字体居中
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
//设置绘制文本的颜色和纹理 (Alpha=153)
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
//将版权信息绘制到图象上
grPhoto.DrawString(Copyright, //版权信息
crFont, //字体
semiTransBrush2, //绘制文本的颜色及纹理
new PointF(xCenterOfImg+1,yPosFromBottom+1), //绘制文本的位置
StrFormat); //格式
//设置绘制文本的颜色和纹理 (Alpha=153)
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
//重新绘制版权信息,以让其具有阴影效果
//将文本向又下移动一个象素
grPhoto.DrawString(Copyright, //版权信息
crFont, //字体
semiTransBrush, //绘制文本的颜色及纹理
new PointF(xCenterOfImg,yPosFromBottom), //绘制文本的位置
StrFormat); //格式
//------------------------------------------------------------
//第二步,插入图片水印
//------------------------------------------------------------
//在原来修改过的bmPhoto上创建一个水银位图
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//将位图bmWatermark加载到Graphics对象
Graphics grWatermark = Graphics.FromImage(bmWatermark);
//To achieve a transulcent watermark we will apply (2) color
//manipulations by defineing a ImageAttributes object and
//seting (2) of its properties.
ImageAttributes imageAttributes = new ImageAttributes();
//The first step in manipulating the watermark image is to replace
//the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
//to do this we will use a Colormap and use this to define a RemapTable
ColorMap colorMap = new ColorMap();
//My watermark was defined with a background of 100% Green this will
//be the color we search for and replace with transparency
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
//The second color manipulation is used to change the opacity of the
//watermark. This is done by applying a 5x5 matrix that contains the
//coordinates for the RGBA space. By setting the 3rd row and 3rd column
//to 0.3f we achive a level of opacity
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, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.3f, 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);
//For this example we will place the watermark in the upper right
//hand corner of the photograph. offset down 10 pixels and to the
//left 10 pixles
int xPosOfWm = ((phWidth - wmWidth) - 10);
int yPosOfWm = 10;
grWatermark.DrawImage(imgWatermark,
new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), //Set the detination Position
0, // x-coordinate of the portion of the source image to draw.
0, // y-coordinate of the portion of the source image to draw.
wmWidth, // Watermark Width
wmHeight, // Watermark Height
GraphicsUnit.Pixel, // Unit of measurment
imageAttributes); //ImageAttributes Object
//Replace the original photgraphs bitmap with the new Bitmap
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
//save new image to file system.
imgPhoto.Save(savePhotoPath, ImageFormat.Jpeg);
imgPhoto.Dispose();
imgWatermark.Dispose();
}
}
}
* 给图片设置水印效果图的函数
* 可以在图片上添加自己的版权和LOGO图片的水印
*
* 原作者:Joel Neubeck
*
* 本人在原作者的基础上进行了部分修改,并改为一个单独的函数
* 大家可以根据需要修改此代码
* 但请保留原作者信息.
*
* 星宿.net(zhuhee)
* 修改于2006-6-14
*
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace WebWaterMark.Components
{
/// <summary>
/// watermark 的摘要说明。
/// </summary>
public class watermark
{
public watermark()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 用于处理图片
/// 给图片加入水印
/// </summary>
/// <param name="Copyright">需要写入的版权信息</param>
/// <param name="MarkBmpPath">需要假如的logo图片</param>
/// <param name="photoPath">需要处理的图片的路径</param>
/// <param name="savePhotoPath">保存的图片路径</param>
public void MakeWatermark(string Copyright,string MarkBmpPath,string photoPath,string savePhotoPath)
{
//创建一个image对象,即要被处理的图片
Image imgPhoto = Image.FromFile(photoPath);
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//创建原始图片大小的Bitmap
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//将位图bmPhoto加载到Graphics对象
Graphics grPhoto = Graphics.FromImage(bmPhoto);
//创建一个需要填充水银的Image对象
Image imgWatermark = new Bitmap(MarkBmpPath);
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;
//------------------------------------------------------------
//第一步,插入版权信息
//------------------------------------------------------------
//设置图象的成相质量
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
//将原始图象绘制到grPhoto上
grPhoto.DrawImage(
imgPhoto, // 要绘制的Image对象
new Rectangle(0, 0, phWidth, phHeight), // 绘制图象的位置和大小
0, // 要绘制的原图象部分的左上角的X坐标
0, // 要绘制的原图象部分的左上角的Y坐标
phWidth, // 要绘制的原图象的高度
phHeight, // 要绘制的原图象的宽度
GraphicsUnit.Pixel); // 源矩形的度量单位
//-------------------------------------------------------
//字体大小放在一个数组中,最大字体为32.
//感觉用处不大,可以自己再修改这里
//-------------------------------------------------------
int[] sizes = new int[]{32,14,12,10,8,6,4};
Font crFont = null;
SizeF crSize = new SizeF();
//循环测试数组中所定义的字体大小是否适合版权信息,如果合适就使用此种字体大小
for (int i=0 ;i<6; i++)
{
//设置字体类型,可以单独提出,作为参数
crFont = new Font("迷你繁篆书", sizes[0], FontStyle.Bold);
//测量此种字体大小
crSize = grPhoto.MeasureString(Copyright, crFont);
if((ushort)crSize.Width < (ushort)phWidth)
break;
}
//给底部保留%3的空间
int yPixlesFromBottom = (int)(phHeight *.03);
//设置字体在图片中的位置
float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));
//float xCenterOfImg = (phWidth/2);
float xCenterOfImg = (phWidth-(crSize.Width)/2);
//设置字体居中
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
//设置绘制文本的颜色和纹理 (Alpha=153)
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
//将版权信息绘制到图象上
grPhoto.DrawString(Copyright, //版权信息
crFont, //字体
semiTransBrush2, //绘制文本的颜色及纹理
new PointF(xCenterOfImg+1,yPosFromBottom+1), //绘制文本的位置
StrFormat); //格式
//设置绘制文本的颜色和纹理 (Alpha=153)
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
//重新绘制版权信息,以让其具有阴影效果
//将文本向又下移动一个象素
grPhoto.DrawString(Copyright, //版权信息
crFont, //字体
semiTransBrush, //绘制文本的颜色及纹理
new PointF(xCenterOfImg,yPosFromBottom), //绘制文本的位置
StrFormat); //格式
//------------------------------------------------------------
//第二步,插入图片水印
//------------------------------------------------------------
//在原来修改过的bmPhoto上创建一个水银位图
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//将位图bmWatermark加载到Graphics对象
Graphics grWatermark = Graphics.FromImage(bmWatermark);
//To achieve a transulcent watermark we will apply (2) color
//manipulations by defineing a ImageAttributes object and
//seting (2) of its properties.
ImageAttributes imageAttributes = new ImageAttributes();
//The first step in manipulating the watermark image is to replace
//the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
//to do this we will use a Colormap and use this to define a RemapTable
ColorMap colorMap = new ColorMap();
//My watermark was defined with a background of 100% Green this will
//be the color we search for and replace with transparency
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
//The second color manipulation is used to change the opacity of the
//watermark. This is done by applying a 5x5 matrix that contains the
//coordinates for the RGBA space. By setting the 3rd row and 3rd column
//to 0.3f we achive a level of opacity
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, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.3f, 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);
//For this example we will place the watermark in the upper right
//hand corner of the photograph. offset down 10 pixels and to the
//left 10 pixles
int xPosOfWm = ((phWidth - wmWidth) - 10);
int yPosOfWm = 10;
grWatermark.DrawImage(imgWatermark,
new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), //Set the detination Position
0, // x-coordinate of the portion of the source image to draw.
0, // y-coordinate of the portion of the source image to draw.
wmWidth, // Watermark Width
wmHeight, // Watermark Height
GraphicsUnit.Pixel, // Unit of measurment
imageAttributes); //ImageAttributes Object
//Replace the original photgraphs bitmap with the new Bitmap
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
//save new image to file system.
imgPhoto.Save(savePhotoPath, ImageFormat.Jpeg);
imgPhoto.Dispose();
imgWatermark.Dispose();
}
}
}