獨占網(wǎng)絡(luò)上傳圖片加上文字水印的方法。
2015/2/6 11:05:56 獨占網(wǎng)絡(luò) 網(wǎng)站建設(shè)知識
我們上傳圖片的經(jīng)常會加上文字水印,如獨占網(wǎng)絡(luò)上傳圖片的時候打入水印為 http://www.ykfic.cn,下面就是我們使用的代碼
/// <summary>
/// 文字水印
/// </summary>
/// <param name="imgPath">服務(wù)器圖片相對路徑</param>
/// <param name="filename">保存文件名</param>
/// <param name="watermarkText">水印文字</param>
/// <param name="watermarkStatus">圖片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
/// <param name="quality">附加水印圖片質(zhì)量,0-100</param>
/// <param name="fontname">字體</param>
/// <param name="fontsize">字體大小</param>
public static void AddImageSignText(string imgPath, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
{
//得到原圖片的信息
byte[] _ImageBytes = File.ReadAllBytes(HttpContext.Current.Server.MapPath(imgPath));
Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
filename = HttpContext.Current.Server.MapPath(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;
}
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;
//水印的質(zhì)量
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);
//關(guān)閉資源
g.Dispose();
img.Dispose();
}