谁能给一个demo,上传图片时给图片加水印,最好还可以生成缩略图

ccb8888 2009-03-18 01:16:25
如题
...全文
553 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cyanapple_wen 2010-02-23
  • 打赏
  • 举报
回复
楼上几位讲的都还不错。路过顶一下。
darkzane 2010-01-16
  • 打赏
  • 举报
回复
顶一下
深海之蓝 2009-03-18
  • 打赏
  • 举报
回复

//计算水印图片的比率
//取背景的1/4宽度来比较
if ((_width > watermark.Width * 4) && (_height > watermark.Height * 4))
{
bl = 1;
}
else if ((_width > watermark.Width * 4) && (_height<watermark.Height * 4))
{
bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);

}
else if ((_width < watermark.Width * 4) && (_height > watermark.Height * 4))
{
bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
}
else
{
if ((_width * watermark.Height) > (_height * watermark.Width))
{
bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);

}
else
{
bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);

}

}

WatermarkWidth = Convert.ToInt32(watermark.Width * bl);
WatermarkHeight = Convert.ToInt32(watermark.Height * bl);

switch (_watermarkPosition)
{
case "WM_TOP_LEFT":
xpos = 10;
ypos = 10;
break;
case "WM_TOP_RIGHT":
xpos = _width - WatermarkWidth - 10;
ypos = 10;
break;
case "WM_BOTTOM_RIGHT":
xpos = _width - WatermarkWidth - 10;
ypos = _height -WatermarkHeight - 10;
break;
case "WM_BOTTOM_LEFT":
xpos = 10;
ypos = _height - WatermarkHeight - 10;
break;
}

picture.DrawImage(
watermark,
new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight),
0,
0,
watermark.Width,
watermark.Height,
GraphicsUnit.Pixel,
imageAttributes);

watermark.Dispose();
imageAttributes.Dispose();
}

/// <summary>
/// 加水印图片
/// </summary>
/// <param name="picture">imge 对象</param>
/// <param name="WaterMarkPicPath">水印图片的地址</param>
/// <param name="_watermarkPosition">水印位置</param>
/// <param name="_width">被加水印图片的宽</param>
/// <param name="_height">被加水印图片的高</param>
private void addWatermarkImage( Graphics picture,string WaterMarkPicPath,
string _watermarkPosition,int _width,int _height)
{
Image watermark = new Bitmap(WaterMarkPicPath);

this.addWatermarkImage(picture, watermark, _watermarkPosition, _width,
_height);
}
#endregion

#region 生成缩略图
/// <summary>
/// 保存图片
/// </summary>
/// <param name="image">Image 对象</param>
/// <param name="savePath">保存路径</param>
/// <param name="ici">指定格式的编解码参数</param>
private void SaveImage(Image image, string savePath, ImageCodecInfo ici)
{
//设置 原图片 对象的 EncoderParameters 对象
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, ((long) 90));
image.Save(savePath, ici, parameters);
parameters.Dispose();
}

/// <summary>
/// 获取图像编码解码器的所有相关信息
/// </summary>
/// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
/// <returns>返回图像编码解码器的所有相关信息</returns>
private ImageCodecInfo GetCodecInfo(string mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach(ImageCodecInfo ici in CodecInfo)
{
if(ici.MimeType == mimeType)
return ici;
}
return null;
}

/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="sourceImagePath">原图片路径(相对路径)</param>
/// <param name="thumbnailImagePath">生成的缩略图路径,如果为空则保存为原图片路径(相对路径)</param>
/// <param name="thumbnailImageWidth">缩略图的宽度(高度与按源图片比例自动生成)</param>
public void ToThumbnailImages(
string SourceImagePath,
string ThumbnailImagePath,
int ThumbnailImageWidth)
{
Hashtable htmimes = new Hashtable();
htmimes[".jpeg"] = "image/jpeg";
htmimes[".jpg"] = "image/jpeg";
htmimes[".png"] = "image/png";
htmimes[".tif"] = "image/tiff";
htmimes[".tiff"] = "image/tiff";
htmimes[".bmp"] = "image/bmp";
htmimes[".gif"] = "image/gif";

// 取得原图片的后缀
string sExt = SourceImagePath.Substring(
SourceImagePath.LastIndexOf(".")).ToLower();

//从 原图片 创建 Image 对象
Image image = Image.FromFile(SourceImagePath);
int num = ((ThumbnailImageWidth / 4) * 3);
int width = image.Width;
int height = image.Height;

//计算图片的比例
if ((((double) width) / ((double) height)) >= 1.3333333333333333f)
{
num = ((height * ThumbnailImageWidth) / width);
}
else
{
ThumbnailImageWidth = ((width * num) / height);
}
if ((ThumbnailImageWidth < 1) || (num < 1))
{
return;
}

//用指定的大小和格式初始化 Bitmap 类的新实例
Bitmap bitmap = new Bitmap(ThumbnailImageWidth, num,
PixelFormat.Format32bppArgb);

//从指定的 Image 对象创建新 Graphics 对象
Graphics graphics = Graphics.FromImage(bitmap);

//清除整个绘图面并以透明背景色填充
graphics.Clear(Color.Transparent);

graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.High;

//在指定位置并且按指定大小绘制 原图片 对象
graphics.DrawImage(image, new Rectangle(0, 0, ThumbnailImageWidth, num));
image.Dispose();

try
{
//将此 原图片 以指定格式并用指定的编解码参数保存到指定文件
SaveImage(bitmap, ThumbnailImagePath,
GetCodecInfo((string)htmimes[sExt]));
}
catch(System.Exception e)
{
throw e;
}
}
#endregion
}
}

深海之蓝 2009-03-18
  • 打赏
  • 举报
回复

using System;
using System.IO;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace Imag_writer
{
/// <summary>
/// 水印的类型
/// </summary>
public enum WaterMarkType
{
/// <summary>
/// 文字水印
/// </summary>
TextMark,
/// <summary>
/// 图片水印
/// </summary>
//ImageMark // 暂时只能添加文字水印
};

/// <summary>
/// 水印的位置
/// </summary>
public enum WaterMarkPosition
{
/// <summary>
/// 左上角
/// </summary>
WMP_Left_Top,
/// <summary>
/// 左下角
/// </summary>
WMP_Left_Bottom,
/// <summary>
/// 右上角
/// </summary>
WMP_Right_Top,
/// <summary>
/// 右下角
/// </summary>
WMP_Right_Bottom
};

/// <summary>
/// 处理图片的类(包括加水印,生成缩略图)
/// </summary>
public class ImageWaterMark
{
public ImageWaterMark()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

#region 给图片加水印
/// <summary>
/// 添加水印(分图片水印与文字水印两种)
/// </summary>
/// <param name="oldpath">原图片绝对地址</param>
/// <param name="newpath">新图片放置的绝对地址</param>
/// <param name="wmtType">要添加的水印的类型</param>
/// <param name="sWaterMarkContent">水印内容,若添加文字水印,此即为要添加的文字;
/// 若要添加图片水印,此为图片的路径</param>
public void addWaterMark(string oldpath, string newpath,
WaterMarkType wmtType, string sWaterMarkContent)
{
try
{
Image image = Image.FromFile(oldpath);

Bitmap b = new Bitmap(image.Width, image.Height,
PixelFormat.Format24bppRgb);

Graphics g = Graphics.FromImage(b);
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.High;

g.DrawImage(image, 0, 0, image.Width, image.Height);

switch (wmtType)
{
/*case WaterMarkType.ImageMark:
//图片水印
this.addWatermarkImage(g,
Page.Server.MapPath(Watermarkimgpath),
WatermarkPosition,image.Width,image.Height);
break;*/
case WaterMarkType.TextMark:
//文字水印
this.addWatermarkText(g, sWaterMarkContent, "WM_BOTTOM_RIGHT",
image.Width, image.Height);
break;
}

b.Save(newpath);
b.Dispose();
image.Dispose();
}
catch
{
if(File.Exists(oldpath))
{
File.Delete(oldpath);
}
}
finally
{
if(File.Exists(oldpath))
{
File.Delete(oldpath);
}
}
}

/// <summary>
/// 加水印文字
/// </summary>
/// <param name="picture">imge 对象</param>
/// <param name="_watermarkText">水印文字内容</param>
/// <param name="_watermarkPosition">水印位置</param>
/// <param name="_width">被加水印图片的宽</param>
/// <param name="_height">被加水印图片的高</param>
private void addWatermarkText(Graphics picture, string _watermarkText,
string _watermarkPosition, int _width, int _height)
{
// 确定水印文字的字体大小
int[] sizes = new int[]{32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4};
Font crFont = null;
SizeF crSize = new SizeF();
for (int i = 0;i < sizes.Length; i++)
{
crFont = new Font("Arial Black", sizes[i], FontStyle.Bold);
crSize = picture.MeasureString(_watermarkText, crFont);

if((ushort)crSize.Width < (ushort)_width)
{
break;
}
}

// 生成水印图片(将文字写到图片中)
Bitmap floatBmp = new Bitmap((int)crSize.Width + 3,
(int)crSize.Height + 3, PixelFormat.Format32bppArgb);
Graphics fg=Graphics.FromImage(floatBmp);
PointF pt=new PointF(0,0);

// 画阴影文字
Brush TransparentBrush0 = new SolidBrush(Color.FromArgb(255, Color.Black));
Brush TransparentBrush1 = new SolidBrush(Color.FromArgb(255, Color.Black));
fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X, pt.Y + 1);
fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X + 1, pt.Y);

fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X + 1, pt.Y + 1);
fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X, pt.Y + 2);
fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X + 2, pt.Y);

TransparentBrush0.Dispose();
TransparentBrush1.Dispose();

// 画文字
fg.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
fg.DrawString(_watermarkText,
crFont, new SolidBrush(Color.White),
pt.X, pt.Y, StringFormat.GenericDefault);

// 保存刚才的操作
fg.Save();
fg.Dispose();
// floatBmp.Save("d:\\WebSite\\DIGITALKM\\ttt.jpg");

// 将水印图片加到原图中
this.addWatermarkImage(
picture,
new Bitmap(floatBmp),
"WM_BOTTOM_RIGHT",
_width,
_height);
}

/// <summary>
/// 加水印图片
/// </summary>
/// <param name="picture">imge 对象</param>
/// <param name="iTheImage">Image对象(以此图片为水印)</param>
/// <param name="_watermarkPosition">水印位置</param>
/// <param name="_width">被加水印图片的宽</param>
/// <param name="_height">被加水印图片的高</param>
private void addWatermarkImage( Graphics picture,Image iTheImage,
string _watermarkPosition,int _width,int _height)
{
Image watermark = new Bitmap(iTheImage);

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, 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 colorMatrix = new ColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

int xpos = 0;
int ypos = 0;
int WatermarkWidth = 0;
int WatermarkHeight = 0;
double bl = 1d;



Fly_yuanrunfei 2009-03-18
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upfile.aspx.cs" Inherits="upfile_upfile" %>

<!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>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" /><br />
<asp:Label ID="Label1" runat="server"></asp:Label></div>
</form>
</body>
</html>
upfile.aspx.cs文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.IO;

public partial class upfile_upfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
{
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径

FileInfo file = new FileInfo(name);
string fileName = file.Name; // 文件名称
string fileName_s = "s_" + file.Name; // 缩略图文件名称
string fileName_sy = "sy_" + file.Name; // 水印图文件名称(文字)
string fileName_syp = "syp_" + file.Name; // 水印图文件名称(图片)
string webFilePath = Server.MapPath("file/" + fileName); // 服务器端文件路径
string webFilePath_s = Server.MapPath("file/" + fileName_s);  // 服务器端缩略图路径
string webFilePath_sy = Server.MapPath("file/" + fileName_sy); // 服务器端带水印图路径(文字)
string webFilePath_syp = Server.MapPath("file/" + fileName_syp); // 服务器端带水印图路径(图片)
string webFilePath_sypf = Server.MapPath("file/shuiyin.jpg"); // 服务器端水印图路径(图片)

if (!File.Exists(webFilePath))
{
try
{
FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
AddShuiYinWord(webFilePath, webFilePath_sy);
AddShuiYinPic(webFilePath, webFilePath_syp, webFilePath_sypf);
MakeThumbnail(webFilePath, webFilePath_s, 130, 130, "Cut"); // 生成缩略图方法
Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
}
catch (Exception ex)
{
Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
}
}
else
{
Label1.Text = "提示:文件已经存在,请重命名后上传";
}
}
else
{
Label1.Text = "提示:文件类型不符";
}
}
}
/**//// <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)
{
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();
}
}

/**//// <summary>
/// 在图片上增加文字水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_sy">生成的带文字水印的图片路径</param>
protected void AddShuiYinWord(string Path, string Path_sy)
{
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();
}

/**//// <summary>
/// 在图片上生成图片水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_syp">生成的带图片水印的图片路径</param>
/// <param name="Path_sypf">水印图片路径</param>
protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
{
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();
}
}
cppfaq 2009-03-18
  • 打赏
  • 举报
回复
再来一个:)
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Tutorial
{
class WaterMark
{

[STAThread]
static void Main(string[] args)
{
//设置工作目录
string WorkingDirectory = @"C:Documents and Settingsadministrator.JAZZMINEMy DocumentsProjectsTutorialsWaterMark";

//定义版权声明的字符串信息或文本(看来原作者是老外)
string Copyright = "Copyright @2002 - AP Photo/David Zalubowski";

//创建一个需要添加水印的图像对象
Image imgPhoto = Image.FromFile(WorkingDirectory + "\watermark_photo.jpg");
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;

//创建一个原始图像大小的Bitmap
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

//把Bitmap装载进Graphics对象
Graphics grPhoto = Graphics.FromImage(bmPhoto);

//创建一个包含水印的image对象
Image imgWatermark = new Bitmap(WorkingDirectory + "\watermark.bmp");
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;

//------------------------------------------------------------
//第一步 -插入版权信息
//------------------------------------------------------------

//设置这个要转换Graphics对象的图像质量
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

//绘制图像对象到graphics对象(保留原始的宽高)
grPhoto.DrawImage(
imgPhoto, // Photo Image object
new Rectangle(0, 0, phWidth, phHeight), // 构造矩形
0, // 要绘制的源图像的x坐标
0, // 要绘制的源图像的y坐标
phWidth, // 要绘制的源图像的宽度
phHeight, // 要绘制的源图像的高度
GraphicsUnit.Pixel); // 图像所使用的单位

//-------------------------------------------------------
//为了最大化版权信息的字体,我们通过测试多个字体大小,定一个了一个数组来包含不同的字体大小
//-------------------------------------------------------
int[] sizes = new int[]{16,14,12,10,8,6,4};

Font crFont = null;
SizeF crSize = new SizeF();

//通过循环这个数组,来选用不同的字体大小
//如果它的大小小于图像的宽度,就选用这个大小的字体
for (int i=0 ;i<7; i++)
{
//设置字体,这里是用arial,黑体
crFont = new Font("arial", sizes[i], FontStyle.Bold);
//Measure the Copyright string in this Font
crSize = grPhoto.MeasureString(Copyright, crFont);

if((ushort)crSize.Width < (ushort)phWidth)
break;
}

//因为图片的高度可能不尽相同, 所以定义了
//从图片底部算起预留了5%的空间
int yPixlesFromBottom = (int)(phHeight *.05);

//现在使用版权信息字符串的高度来确定要绘制的图像的字符串的y坐标

float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));

//计算x坐标
float xCenterOfImg = (phWidth/2);

//把文本布局设置为居中
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;

//通过Brush来设置黑色半透明
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));

//绘制版权字符串
grPhoto.DrawString(Copyright, //版权字符串文本
crFont, //字体
semiTransBrush2, //Brush
new PointF(xCenterOfImg+1,yPosFromBottom+1), //位置
StrFormat);

//设置成白色半透明
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));

//第二次绘制版权字符串来创建阴影效果
//记住移动文本的位置1像素
grPhoto.DrawString(Copyright, //版权文本
crFont, //字体
semiTransBrush, //Brush
new PointF(xCenterOfImg,yPosFromBottom), //位置
StrFormat); //文本对齐



//------------------------------------------------------------
//第二步:插入水印图像
//------------------------------------------------------------

//通过之前创建的Bitmap创建一个 Bitmap
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

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();

//第一步是用水印来替代背景颜色
//(Alpha=0, R=0, G=0, B=0)
//要使用 Colormap 来定义一个RemapTable
ColorMap colorMap = new ColorMap();

//这个水印的背景定义成100%绿色,并用来替代透明
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

ColorMap[] remapTable = {colorMap};

imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

//第二个颜色是用来控制水印图片的不透明度的
//是使用一个5*5的RGBA矩阵来实现的
// 设置第4行、第4列的值为0.3来不透明度的级别
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);

//本例的水印放置在右上方
// 偏移量是10


int xPosOfWm = ((phWidth - wmWidth)-10);
int yPosOfWm = 10;

grWatermark.DrawImage(imgWatermark,
new Rectangle(xPosOfWm,yPosOfWm,wmWidth,wmHeight), //Set the detination Position
0, // x坐标
0, // y坐标
wmWidth, // 水印宽度
wmHeight, // 水印高度
GraphicsUnit.Pixel, // 单位
imageAttributes); //ImageAttributes对象

//使用新生成的加了水印图片替代原始图片
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();

//保存的路径
imgPhoto.Save(WorkingDirectory + @"\watermark_final.jpg", ImageFormat.Jpeg);
imgPhoto.Dispose();
imgWatermark.Dispose();

}
}
}



http://blog.csdn.net/patriot074/archive/2008/05/21/2465217.aspx
cppfaq 2009-03-18
  • 打赏
  • 举报
回复
给图片加上水印效果
http://www.cnblogs.com/index/archive/2004/10/20/54498.aspx

下面的代码中,加文字水印和加图片水印的代码不能共存
我是为了方便显示才写在一块的
 private void Btn_Upload_Click(object sender, System.EventArgs e)
{
if(UploadFile.PostedFile.FileName.Trim()!="")
{
//上传文件
string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
string path = Server.MapPath(".") + "/UploadFile/" + fileName + extension;
UploadFile.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", 32);
Brush b = new SolidBrush(Color.White);
string addText = AddText.Value.Trim();
g.DrawString(addText, f, b, 10, 10);
g.Dispose();

//加图片水印
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/Alex.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(".") + "/UploadFile/" + fileName + "_new" + extension;
image.Save(newPath);
image.Dispose();
if(File.Exists(path))
{
File.Delete(path);
}

Response.Redirect(newPath);
}
}
Winson.Framework 1.0简介:

Winson.Framework 1.0可以说是一个基于ASP.NET2.0技术小型应用框架,是本人从以

往的开发经验和网上的一些好的代码收集整合而成的。

这里需要说明一下的就是,本框架的数据库底层代码是改自于台湾一位朋友的数据库

底层代码,我是在其基础上添了一些功能,如可同可支持MSSQL和ACCESS数据库,可显

示自定义异常等。当然,如果需也,也可以继续扩展支持其他的数据库,呵呵。

========================================

功能介绍:

一、数据库类:

1、本框架是基于ASP.NET2.0技术,同可支持MSSQL和ACCESS数据库,只需在配置文件里修改
一下即可马上使用不同的数据库,不需改其他任何代码。
2、可设置是否对连接字符串进行密,并可设置解密KEY。
3、可直接使用XML文件为实体
4、可设置是否需要缓存,同可设置缓存文件依赖存放的位置
5、可支持多语言的数据库自定义错误信息,现在支持简体和繁体2种
6、可设置是否启用错误日志记录
7、可设置自动保存应用程序参数的间间隔
8、可设置是否显示系统异常信息,否则显示自定义异常信息
9、通用的数据库底层操作,同支持存储过程调用
10、方便地操作读取自定义配置文件的信息

二、功能控件类:

1、漂亮的自定义警告框,可为警告框设置相应的事件
2、漂亮的自定义提示框,即类似MSN消息框的效果,可设置多种效果,也可以使用事件钩子调用
3、强大的自定义分页控件,支持多种数据绑定控件,支持自定义分页按钮样式,支持AJAX无刷新
分页,可灵活设置各种参数效果
4、方便的Tab控件(取自Discuz!NT),即DNT论坛后台的那种选项卡的效果
5、类帮助器,可以动态对类,类成员进行控制(添,删除),目前只支持属性控制。
6、可动态创建ul或者dl构成的列表
7、通用的MessageBox类,可根据需要显示各种提示框
8、支持URL重写,可自定义重写规则,同还可以限制IP访问,需修改XML配置文件
9、Xml文件操作类库,基本上已可满足常用的XML操作需要

三、经常需要使用的一些验证逻辑与方法类:

1、返回上一个页面的地址
2、获取当前请求的原始 URL(URL 中域信息之后的部分,包括查询字符串(如果存在))
3、获得当前完整Url地址
4、过滤危险字符串
5、替换html中的特殊字符
6、恢复html中的特殊字符
7、检查一个字符串是否可以转化为日期,一般用于验证用户输入日期的合法性
8、检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。
9、检查一个字符串是否是纯字母和数字构成的,一般用于查询字符串参数的有效性验证。
10、判断是否是数字,包括小数和整数。
11、快速验证一个字符串是否符合指定的正则表达式。
12、上传文件,不限制大小和类型并上传到默认UploadFile目录
13、上传文件,不限制大小和类型并上传到自定义路径
14、上传文件,可设置大小、类型和存放路径
15、生成缩略图
16、在图片上增文字水印
17、在图片生成图片水印

四、实体操作类:

1、利用泛型和集合,结合数据库实体进行操作以提高效率和封装性
2、动态生成所需的SQL增、删、改、查等语句对实体进行操作
3、上层操作可以不需要写SQL语句
4、实体赋值方便,只需设置需要更新的某个字段即可,不需全部字段去赋值,同读取实体
系统也会自动为每个字段赋值,不需再手工编写赋值代码
5、由于本框架的实体类和实体操作类需特别处理,因此已提供了实体类和实体操作类的代码
生成模板文件,使用Codesmith生成
6、另此DEMO一个无限级分类的管理操作及相关的数据库

五、其他说明:

本程序目录里已有多个试例DEMO,基本上主要功能均有演示了,另有一个非常通用方便的JS

校验文件,在js/check.js目录,至于使用方法,请自己看看里面的注释吧,呵呵

欢迎大家一起来完善这个框架,有什么好的东西记得告诉我哦:)

110,590

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧