using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Hidistro.UI.Common.Controls
{
public class WatermarkHelper
{
/// </summary>
/// <param name="img">原图Img对象</param>
/// <param name="watermarkFilename">水印文件名</param>
/// <param name="watermarkStatus">位置 1~ 9 上中下分别三个点</param>
/// <param name="quality">图片质量1~100</param>
/// <param name="watermarkTransparency">图片水印透明度 取值范围1--10 (10为不透明)</param>
public static MemoryStream SetWatermark(System.Drawing.Image img, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
{
Graphics g = null;
System.Drawing.Image watermark = null;
ImageAttributes imageAttributes = null;
MemoryStream mStream = null;
try
{
g = Graphics.FromImage(img);
//设置高质量插值法
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
if (File.Exists(watermarkFilename))
{
watermark = new Bitmap(watermarkFilename);
}
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 transparency = 0.5F;
if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
{
transparency = (watermarkTransparency / 10.0F);
}
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, transparency, 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;
if (watermark == null)
watermarkStatus = 1;
switch (watermarkStatus)
{
case 1:
//xpos = (int)(img.Width * (float).01);
//ypos = (int)(img.Height * (float).01);
xpos = 0;
ypos = 0;
break;
case 2:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)(img.Height * (float).01);
break;
case 3:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)(img.Height * (float).01);
break;
case 4:
xpos = (int)(img.Width * (float).01);
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 5:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 6:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 7:
xpos = (int)(img.Width * (float).01);
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
case 8:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
case 9:
//右下角
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
}
if (watermark != null)
g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
//g.DrawImage(watermark, new System.Drawing.Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, System.Drawing.GraphicsUnit.Pixel);
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;
mStream = new MemoryStream();
if (ici != null)
{
img.Save(mStream, ici, encoderParams);
}
return mStream;
}
catch
{ mStream = null; return mStream; }
finally
{
if (g != null) g.Dispose();
if (img != null) img.Dispose();
if (watermark != null) watermark.Dispose();
if (imageAttributes != null) imageAttributes.Dispose();
}
}
public static MemoryStream SetWatermark(string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
{
Bitmap img = null;
Graphics g = null;
System.Drawing.Image watermark = null;
ImageAttributes imageAttributes = null;
MemoryStream mStream = null;
try
{
img = new Bitmap(filename);
g = Graphics.FromImage(img);
//设置高质量插值法
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
if (File.Exists(watermarkFilename))
{
watermark = new Bitmap(watermarkFilename);
}
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 transparency = 0.5F;
if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
{
transparency = (watermarkTransparency / 10.0F);
}
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, transparency, 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;
if (watermark == null)
watermarkStatus = 1;
switch (watermarkStatus)
{
case 1:
//xpos = (int)(img.Width * (float).01);
//ypos = (int)(img.Height * (float).01);
xpos = 0;
ypos = 0;
break;
case 2:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)(img.Height * (float).01);
break;
case 3:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)(img.Height * (float).01);
break;
case 4:
xpos = (int)(img.Width * (float).01);
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 5:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 6:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 7:
xpos = (int)(img.Width * (float).01);
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
case 8:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
case 9:
//右下角
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
}
if (watermark != null)
g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
//g.DrawImage(watermark, new System.Drawing.Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, System.Drawing.GraphicsUnit.Pixel);
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;
mStream = new MemoryStream();
if (ici != null)
{
img.Save(mStream, ici, encoderParams);
}
return mStream;
}
catch
{ mStream = null; return mStream; }
finally
{
if (g != null) g.Dispose();
if (img != null) img.Dispose();
if (watermark != null) watermark.Dispose();
if (imageAttributes != null) imageAttributes.Dispose();
}
}
}
}
函数返回值 返回流,使用Bitmap保存到本地
var bitmap = new Bitmap(ms);
//先删除原来的图片 再保存新的水印图片
//File.Delete(imageurl);
bitmap.Save(imageurl);
bitmap.Dispose();