CODE
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
/// <summary>
/// Helper 的摘要说明
/// </summary>
public class Helper
{
public class UpLoadHelper
{
#region 添加文字水印
/// <summary>
/// 添加文字水印
/// </summary>
/// <param name="text">水印文字</param>
/// <param name="file">图片文件</param>
public static void AttachText(string text, string file)
{
if (string.IsNullOrEmpty(text))
{
return;
}
if (!File.Exists(file))
{
return;
}
FileInfo oFile = new FileInfo(file);
string strTempFile = Path.Combine(oFile.DirectoryName, Guid.NewGuid().ToString() + oFile.Extension);
oFile.CopyTo(strTempFile);
Image img = Image.FromFile(strTempFile);
ImageFormat thisFormat = img.RawFormat;
int nHeight = img.Height;
int nWidth = img.Width;
Bitmap outBmp = new Bitmap(nWidth, nHeight);
Graphics g = Graphics.FromImage(outBmp);
g.Clear(Color.White);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, nWidth, nHeight), 0, 0, nWidth, nHeight, 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 = g.MeasureString(text, crFont);
if ((ushort)crSize.Width < (ushort)nWidth)
{ break; }
}
//因为图片的高度可能不尽相同, 所以定义了
//从图片底部算起预留了5%的空间
int yPixlesFromBottom = (int)(nHeight * .08);
//现在使用版权信息字符串的高度来确定要绘制的图像的字符串的y坐标
float yPosFromBottom = ((nHeight - yPixlesFromBottom) - (crSize.Height / 2));
//计算x坐标
float xCenterOfImg = (nWidth / 2);
//把文本布局设置为居中
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
//通过Brush来设置黑色半透明
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
//绘制版权字符串
g.DrawString(text, //版权字符串文本
crFont, //字体
semiTransBrush2, //Brush
new PointF(xCenterOfImg + 1, yPosFromBottom + 1), //位置
StrFormat); //设置成白色半透明
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
//第二次绘制版权字符串来创建阴影效果
//记住移动文本的位置1像素
g.DrawString(text, //版权文本
crFont, //字体
semiTransBrush, //Brush
new PointF(xCenterOfImg, yPosFromBottom), //位置
StrFormat);
g.Dispose(); // 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam; //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//设置JPEG编码
break;
}
} if (jpegICI != null)
{ outBmp.Save(file, jpegICI, encoderParams); }
else { outBmp.Save(file, thisFormat); }
img.Dispose();
outBmp.Dispose();
File.Delete(strTempFile);
}
#endregion
}
}
页面调用:Helper.UpLoadHelper.AttachText("水印文字", FileUpload1.PostedFile.FileName);
2,
code 2
/// <summary>
/// 增加图片文字水印
/// </summary>
/// <param name="filename">文件名</param>
/// <param name="watermarkText">水印文字</param>
/// <param name="watermarkStatus">图片水印位置</param>
/// <param name="quality">附加图片质量,1是 0不是</param>
public static void AddImageSignText(Image img, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
{
//System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(img);
// .FromFile(filename);
Graphics g = Graphics.FromImage(img);
Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF crSize;
crSize = g.MeasureString(watermarkText, drawFont);
float xpos = 0;
float ypos = 0;
switch (watermarkStatus)
{
case 1:
xpos = (float)img.Width * (float).01;
ypos = (float)img.Height * (float).01;
break;
case 2:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = (float)img.Height * (float).01;
break;
case 3:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = (float)img.Height * (float).01;
break;
case 4:
xpos = (float)img.Width * (float).01;
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 5:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 6:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 7:
xpos = (float)img.Width * (float).01;
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
case 8:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
case 9:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
}
// System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
// StrFormat.Alignment = System.Drawing.StringAlignment.Center;
//
// g.DrawString(watermarkText, drawFont, new System.Drawing.SolidBrush(System.Drawing.Color.White), xpos + 1, ypos + 1, StrFormat);
// g.DrawString(watermarkText, drawFont, new System.Drawing.SolidBrush(System.Drawing.Color.Black), xpos, ypos, StrFormat);
g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType.IndexOf("jpeg") > -1)
{
ici = codec;
}
}
EncoderParameters encoderParams = new EncoderParameters();
long[] qualityParam = new long[1];
if (quality < 0 || quality > 100)
{
quality = 80;
}
qualityParam[0] = quality;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
encoderParams.Param[0] = encoderParam;
if (ici != null)
{
img.Save(filename, ici, encoderParams);
}
else
{
img.Save(filename);
}
g.Dispose();
//bmp.Dispose();
img.Dispose();
}
页面调用:、
Image img = Image.FromStream(this.FileUpload1.PostedFile.InputStream);
Helper.UpLoadHelper.AddImageSignText(img, FileUpload1.PostedFile.FileName, "水印文字", 1, 100, "宋体", 20);
3,加上LOGO图片水印
code3
/// <summary>
/// 加上logo图片水印
/// </summary>
/// <param name="FilePath">源图片地址</param>
/// <param name="SavePath">图片保存路径</param>
public static void AddWaterMark(string FilePath, string SavePath,string logoPath)
{
Bitmap bitmap = new Bitmap(FilePath);
Graphics g = Graphics.FromImage(bitmap);
System.Drawing.Image logo = System.Drawing.Image.FromFile(logoPath);//加载logo图片
if (bitmap.Width < logo.Width || bitmap.Height <= logo.Height)
return;
//下面定义一个矩形区域,以后在这个矩形里画上透明背景和白色字体
float rectWidth = (float)logo.Width;
float rectHeight = (float)logo.Height;
float rectX = bitmap.Width - rectWidth;
float rectY = bitmap.Height - rectHeight;
//声明矩形域
RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);
g.DrawImage(logo, textArea);
MemoryStream ms = new MemoryStream();
//保存为Jpg类型
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
bitmap.Dispose();
logo.Dispose();
FileStream fs = new FileStream(SavePath, FileMode.OpenOrCreate);
fs.Write(ms.ToArray(), 0, ms.ToArray().Length);
fs.Close();
}
页面调用:
Helper.UpLoadHelper.AddWaterMark(FileUpload1.PostedFile.FileName, Server.MapPath("~/images/aa.jpg"), FileUpload2.PostedFile.FileName);