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,这里对添加水印函数参数进行了修改,返回值也做了下数据类型转换

posted @ 2017-03-28 11:25  那一轮弯月~  阅读(2409)  评论(0编辑  收藏  举报