C#给图片添加水印

public enum WaterPositionMode
{
            LeftTop,
            LeftBottom,
            RightTop,
            RightBottom,
            Center
}

/// <summary>添加图片水印
/// </summary>
/// <param name="oldImage"></param>
/// <param name="watertext"></param>
/// <param name="position"></param>
/// <param name="color"></param>
/// <param name="alpha"></param>
/// <returns></returns>

public static byte[] AddWaterText(byte[] oldImage, string watertext, WaterPositionMode position, string color, int alpha)
{
            MemoryStream ms = new MemoryStream(oldImage);
            Image image = Image.FromStream(ms);
            Bitmap bitmap = new Bitmap(image.Width, image.Height);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
            Font font = new Font("arial", 20);//水印字体
            SizeF ziSizeF = new SizeF();
            ziSizeF = graphics.MeasureString(watertext, font);
            float x = 0f;
            float y = 0f;
            switch (position)
            {
                case WaterPositionMode.LeftTop:
                    x = ziSizeF.Width / 2f;
                    y = 8f;
                    break;
                case WaterPositionMode.LeftBottom:
                    x = ziSizeF.Width / 2f;
                    y = image.Height - ziSizeF.Height;
                    break;
                case WaterPositionMode.RightTop:
                    x = image.Width * 1f - ziSizeF.Width / 2f;
                    y = 8f;
                    break;
                case WaterPositionMode.RightBottom:
                    x = image.Width - ziSizeF.Width;
                    y = image.Height - ziSizeF.Height;
                    break;
                case WaterPositionMode.Center:
                    x = image.Width / 2;
                    y = image.Height / 2 - ziSizeF.Height / 2;
                    break;
            }
            try
            {
                StringFormat stringFormat = new StringFormat { Alignment = StringAlignment.Center };
                SolidBrush solidBrush = new SolidBrush(Color.FromArgb(alpha, 0, 0, 0));
                graphics.DrawString(watertext, font, solidBrush, x + 1f, y + 1f, stringFormat);
                SolidBrush brush = new SolidBrush(Color.FromArgb(alpha, ColorTranslator.FromHtml(color)));
                graphics.DrawString(watertext, font, brush, x, y, stringFormat);
                solidBrush.Dispose();
                brush.Dispose();
                using (MemoryStream stream = new MemoryStream())
                {
                    bitmap.Save(stream, ImageFormat.Jpeg);
                    byte[] newImage = new byte[stream.Length];
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.Read(newImage, 0, Convert.ToInt32(stream.Length));
                    return newImage;
                }
            }
            catch (Exception e)
            {

            }
            finally
            {
                bitmap.Dispose();
                image.Dispose();
            }
            return null;

}

这里再添加两个图片和byte[]互相转换的函数
public byte[] ConvertBinary(string filePath)//图片转byte数组
{
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);//以文件流形式读取图片
            BinaryReader br = new BinaryReader(fs);//转换成二进制流
            byte[] imageBytes = br.ReadBytes((int)fs.Length);//保存到字节数组中

            return imageBytes;
}
public Image ReturnPhoto(byte[] streamByte)//byte数组转图片
{
            MemoryStream ms = new MemoryStream(streamByte);//以内存流的形式存储byte数组
            Image img = Image.FromStream(ms);//将相应的内存流转换成图片
            return img;
}

本文来自 http://www.cnblogs.com/xsyblogs/p/3533622.html,这里对添加水印函数参数进行了修改,返回值也做了下数据类型转换

转载于:https://www.cnblogs.com/Bright-King/p/6633076.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值