• 给图片添加水印


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

    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/
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    CentOS7 FTP安装与配置
    linux CentOS 安装 nginx
    linux CentOS YUM 安装 nginx+tomcat+java+mysql运行环境
    Node.js 开发
    Nginx 负载均衡
    BtxCMS.Net 项目
    不得不看!史上最全的三十多张架构师图谱!
    高危群体:开发者的自白,躲坑,迷茫,和下一步
    p2p-如何拯救k8s镜像分发的阿喀琉斯之踵
    Tower与DevCloud对比分析报告
  • 原文地址:https://www.cnblogs.com/jinzhao/p/1932118.html
Copyright © 2020-2023  润新知