zoukankan      html  css  js  c++  java
  • 给图片添加水印

    许久没写代码了,许久没上博客园了,许久没有写博文了。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace MarkPicWithFileName
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                DirectoryInfo dic = new DirectoryInfo(@"C:\Users\kerry\Desktop\handbag");
                FileInfo[] files = dic.GetFiles();
                foreach (FileInfo f in files)
                {
                    Image img = Image.FromFile(f.FullName);
                    string newFileName=@"C:\Users\kerry\Desktop\handbag\new\" + f.Name;
                    Utils.AddImageSignText(img, newFileName, f.Name.Replace(".JPG", string.Empty).Replace(".jpg", string.Empty), 1, 100, "微软雅黑", 24);
                   
                    //Utils .AddImageSignText (
                }
            }

             }

        public class Utils
        {
            /// <summary>
            /// 增加图片文字水印
            /// </summary>
            /// <param name="filename">文件名</param>
            /// <param name="watermarkText">水印文字</param>
            /// <param name="watermarkStatus">图片水印位置</param>
            public static void AddImageSignText(Image img, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
            {
                //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(img);
                //    .FromFile(filename);
                Graphics g = Graphics.FromImage(img);
                Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
                SizeF crSize;
                crSize = g.MeasureString(watermarkText, drawFont);

                float xpos = 0;
                float ypos = 0;

                switch (watermarkStatus)
                {
                    case 1:
                        xpos = (float)img.Width * (float).01;
                        ypos = (float)img.Height * (float).01;
                        break;
                    case 2:
                        xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                        ypos = (float)img.Height * (float).01;
                        break;
                    case 3:
                        xpos = ((float)img.Width * (float).99) - crSize.Width;
                        ypos = (float)img.Height * (float).01;
                        break;
                    case 4:
                        xpos = (float)img.Width * (float).01;
                        ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                        break;
                    case 5:
                        xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                        ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                        break;
                    case 6:
                        xpos = ((float)img.Width * (float).99) - crSize.Width;
                        ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                        break;
                    case 7:
                        xpos = (float)img.Width * (float).01;
                        ypos = ((float)img.Height * (float).99) - crSize.Height;
                        break;
                    case 8:
                        xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                        ypos = ((float)img.Height * (float).99) - crSize.Height;
                        break;
                    case 9:
                        xpos = ((float)img.Width * (float).99) - crSize.Width;
                        ypos = ((float)img.Height * (float).99) - crSize.Height;
                        break;
                }

                //            System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
                //            StrFormat.Alignment = System.Drawing.StringAlignment.Center;
                //
                //            g.DrawString(watermarkText, drawFont, new System.Drawing.SolidBrush(System.Drawing.Color.White), xpos + 1, ypos + 1, StrFormat);
                //            g.DrawString(watermarkText, drawFont, new System.Drawing.SolidBrush(System.Drawing.Color.Black), xpos, ypos, StrFormat);
                g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
                g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);

                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;

                if (ici != null)
                {
                    img.Save(filename, ici, encoderParams);
                }
                else
                {
                    img.Save(filename);
                }
                g.Dispose();
                //bmp.Dispose();
                img.Dispose();
            }
       
        }
    }

    作者:KKcat
        
    个人博客:http://jinzhao.me/
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    【工具】模板引擎 Velocity
    【Android】Android 彩信发送的两种方式+源代码
    【Win7】 建立无线局域网实现共享上网
    【Android】java.net.SocketException: Permission denied解决
    Android中使用Post请求(网友版)
    Android通过post请求发送一个xml,解析返回xml数据
    学用MVC4做网站六后台管理:6.1管理员
    学用MVC4做网站六:后台管理
    html5的新增的标签和废除的标签
    HTML5新增的属性和废除的属性
  • 原文地址:https://www.cnblogs.com/jinzhao/p/1932118.html
Copyright © 2011-2022 走看看