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,這里對添加水印函數參數進行了修改,返回值也做了下數據類型轉換


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM