我的入参是byte[](字节数组)类型,所以下面将byte[]转为了image类型,如果小伙伴们入参是image类型的话,改一下方法的参数类型,再将下面的转换去掉即可。
/// <summary>
/// 加水印图片
/// </summary>
/// <param name="picture_Input">imge 对象(在此图片上加水印)</param>
/// <param name="iTheImage">Image对象(以此图片为水印)</param>
/// <param name="_watermarkPosition">水印位置</param>
/// <param name="_width">被加水印图片的宽</param>
/// <param name="_height">被加水印图片的高</param>
public static byte[] GetMaterMarkImg(byte[] picture_Input, Image iTheImage,
WatermarkPosition _watermarkPosition, int _width = 0, int _height = 0)
{
Image watermark = new Bitmap(iTheImage);
#region 将byte[]类型的图片数据转为img类型
MemoryStream ms = new MemoryStream(picture_Input);
Image image = System.Drawing.Image.FromStream(ms);
#endregion
Graphics picture = Graphics.FromImage(image);
if (_width == 0)
{
_width = image.Width;
}
if (_height == 0)
{
_height = image.Height;
}
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;
//计算水印图片的比率
//取背景的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 WatermarkPosition.左上:
xpos = 10;
ypos = 10;
break;
case WatermarkPosition.右上:
xpos = _width - WatermarkWidth - 10;
ypos = 10;
break;
case WatermarkPosition.中间:
xpos = (_width / 2) - (WatermarkWidth / 2);
ypos = (_height / 2) - (WatermarkHeight / 2);
break;
case WatermarkPosition.右下:
xpos = _width - WatermarkWidth - 10;
ypos = _height - WatermarkHeight - 10;
break;
case WatermarkPosition.左下:
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();
MemoryStream ms_OutPut = new MemoryStream();
image.Save(ms_OutPut, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] buffer = new byte[ms_OutPut.Length];
ms_OutPut.Seek(0, SeekOrigin.Begin);
ms_OutPut.Read(buffer, 0, buffer.Length);
return buffer;
}
public enum WatermarkPosition
{
无 = 1000,
左上 = 1001,
右上 = 1002,
中间 = 1003,
右下 = 1004,
左下 = 1005
}