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/
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    C/C++中extern关键字详解
    C/C++中static关键字详解
    MFC创建工程图解
    C++中L和_T()之区别
    C/C++中const关键字详解
    winfrom中,父窗体中只允许显示一个子窗体的代码怎么写?
    sql 批量插入
    visual studio .net ide : checking into source control now says checkIn Now (recursive)
    1.4 Turbo C V2.0的基本操作
    关于vs2003不能调试的解决方法
  • 原文地址:https://www.cnblogs.com/jinzhao/p/1932118.html
Copyright © 2011-2022 走看看