当前位置: 首页 > 编程日记 > 正文

批量修改图片以及加水印

好久木有来写点什么了.. 前段时间挺忙的.  

今天上来分享一个,这几天我给商城图片加水印的代码吧.因为产品编辑那边是先把图片都上传完成了,所以只能做批量修改图片来完成给所有图片加水印的效果. 类似DX、京东效果.

现在正在开发的B2C项目商城: www.oxozoom.com  也希望更多能关注下.

效果如下:

public void plAddSY(object sender, EventArgs e)
        {
            //C#遍历指定文件夹中的所有文件
            DirectoryInfo TheFolder = new DirectoryInfo(Request.PhysicalApplicationPath + "images\\Product");
            //DirectoryInfo TheFolder = new DirectoryInfo("D:\\WebFiles\\Newb2c\\images\\Product");
            //遍历文件夹
            foreach (DirectoryInfo NextFolderfirst in TheFolder.GetDirectories())
            {
                foreach (DirectoryInfo NextFolder in NextFolderfirst.GetDirectories())
                {
                    if (NextFolder.Name == "-2" || NextFolder.Name == "-1" || NextFolder.Name == "0" || NextFolder.Name == "1" || NextFolder.Name == "2" || NextFolder.Name == "3" || NextFolder.Name == "IsBoolImg")
                    {
                        //遍历文件
                        foreach (FileInfo NextFile in NextFolder.GetFiles())
                        {
                            if (NextFile.Name.Contains("SY800SY"))
                            {
                                File.Delete(NextFile.FullName);
                            }
                            else if (!NextFile.Name.Contains("SY.") && !NextFile.Name.Contains("SY800."))
                            {
                                if (!NextFile.Name.Contains("47-47_")&&!NextFile.Name.Contains("50-50_")&&!NextFile.Name.Contains("80-80_")&&!NextFile.Name.Contains("100-100_")&&!NextFile.Name.Contains("120-120_")&&!NextFile.Name.Contains("140-140_")&&!NextFile.Name.Contains("160-160_")&&!NextFile.Name.Contains("380-380_"))
                                {
                                    MarkWater(NextFile.FullName, Request.PhysicalApplicationPath + "images\\" + "logo-back.png");
                                    //MarkWater(NextFile.FullName, "D:\\WebFiles\\Newb2c\\images\\" + "logo-back.png");
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>   
        /// 给图片上水印   
        /// </summary>   
        /// <param name="filePath">原图片地址</param>   
        /// <param name="waterFile">水印图片地址</param>   
        public void MarkWater(string filePath, string waterFile)
        {
            int i = filePath.LastIndexOf(".");
            string ex = filePath.Substring(i, filePath.Length - i);
            if (string.Compare(ex, ".gif", true) == 0)
            {
                return;
            }
            int newp = filePath.LastIndexOf(".");
            string newpo = filePath.Substring(0, newp);
            string newpolast = filePath.Substring(newp + 1);
            string newlastimg = newpo + "SY800" + "." + newpolast;
            BitmapHelper.MakeThumbnail(filePath, newlastimg, 800, 800, "DB", "JPG");
            string ModifyImagePath = newlastimg;
            int lucencyPercent = 25;
            System.Drawing.Image modifyImage = null;
            System.Drawing.Image drawedImage = null;
            Graphics g = null;
            try
            {
                modifyImage = System.Drawing.Image.FromFile(ModifyImagePath, true);
                drawedImage = System.Drawing.Image.FromFile(waterFile, true);
                g = Graphics.FromImage(modifyImage);
                int x = (modifyImage.Width - drawedImage.Width)/2;
                int y = (modifyImage.Height - drawedImage.Height)/2;
                float[][] matrixItems ={   
                new float[] {1, 0, 0, 0, 0},   
                new float[] {0, 1, 0, 0, 0},   
                new float[] {0, 0, 1, 0, 0},   
                new float[] {0, 0, 0, (float)lucencyPercent/100f, 0},   
                new float[] {0, 0, 0, 0, 1}};

                ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
                ImageAttributes imgAttr = new ImageAttributes();
                imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                g.DrawImage(drawedImage, new Rectangle(x, y, drawedImage.Width, drawedImage.Height), 10, 10, drawedImage.Width, drawedImage.Height, GraphicsUnit.Pixel, imgAttr);
                string[] allowImageType = { ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
                FileInfo fi = new FileInfo(ModifyImagePath);
                ImageFormat imageType = ImageFormat.Gif;
                switch (fi.Extension.ToLower())
                {
                    case ".jpg": imageType = ImageFormat.Jpeg; break;
                    case ".gif": imageType = ImageFormat.Gif; break;
                    case ".png": imageType = ImageFormat.Png; break;
                    case ".bmp": imageType = ImageFormat.Bmp; break;
                    case ".tif": imageType = ImageFormat.Tiff; break;
                    case ".wmf": imageType = ImageFormat.Wmf; break;
                    case ".ico": imageType = ImageFormat.Icon; break;
                    default: break;
                }
                MemoryStream ms = new MemoryStream();
                modifyImage.Save(ms, imageType);
                byte[] imgData = ms.ToArray();
                modifyImage.Dispose();
                drawedImage.Dispose();
                g.Dispose();
                FileStream fs = null;
                fs = new FileStream(ModifyImagePath.Replace("SY800.", "SY."), FileMode.Create, FileAccess.Write);
                if (fs != null)
                {
                    fs.Write(imgData, 0, imgData.Length);
                    fs.Close();
                }
            }
            finally
            {
                try
                {
                    drawedImage.Dispose();
                    modifyImage.Dispose();
                    g.Dispose();
                }
                catch
                {
                }
            }
        }

public class BitmapHelper
    {
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalImagePath">源图路径(物理路径)</param>
        /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="mode">生成缩略图的方式</param>
        /// <param name="type">缩略图的图片类型</param>
        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode, string type)
        {
            Image originalImage = Image.FromFile(originalImagePath);

            int towidth = width;
            int toheight = height;

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            switch (mode)
            {
                case "HW"://指定高宽缩放(可能变形)
                    break;
                case "W"://指定宽,高按比例
                    toheight = originalImage.Height * width / originalImage.Width;
                    break;
                case "H"://指定高,宽按比例
                    towidth = originalImage.Width * height / originalImage.Height;
                    break;
                case "Cut"://指定高宽裁减(不变形)
                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                    {
                        oh = originalImage.Height;
                        ow = originalImage.Height * towidth / toheight;
                        y = 0;
                        x = (originalImage.Width - ow) / 2;
                    }
                    else
                    {
                        ow = originalImage.Width;
                        oh = originalImage.Width * height / towidth;
                        x = 0;
                        y = (originalImage.Height - oh) / 2;
                    }
                    break;
                case "DB"://等比缩放(不变形,如果高大按高,宽大按宽缩放)
                    if ((double)originalImage.Width / (double)towidth < (double)originalImage.Height / (double)toheight)
                    {
                        toheight = height;
                        towidth = originalImage.Width * height / originalImage.Height;
                    }
                    else
                    {
                        towidth = width;
                        toheight = originalImage.Height * width / originalImage.Width;
                    }
                    break;
                default:
                    break;
            }

            //新建一个bmp图片
            Image bitmap = new Bitmap(towidth, toheight);

            //新建一个画板
            Graphics g = Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
            new Rectangle(x, y, ow, oh),
            GraphicsUnit.Pixel);

            try
            {
                //保存缩略图
                if (type.ToUpper() == "JPG")
                {
                    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                if (type.ToUpper() == "BMP")
                {
                    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
                }
                if (type.ToUpper() == "GIF")
                {
                    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
                }
                if (type.ToUpper() == "PNG")
                {
                    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
                }
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }}

今天平安夜.一年又是一年,去年10月14号跟女友分手到现在,已经1年多了 .2013,我的本命年.也是不无心再谈感情的一年.这一年收获很多,明白了太多太多.
那在即将到来的2014,我会开始下一段感情. 希望我会幸福..  一直坚信自信是一个人自强、自立的根本,也许过去我会有不自信,会迷茫,但是现在不会了.我很清楚我要什么.我所期待的东西得不到,我可以等.但是我觉得不会再勉强. 2012.10.14-2013.12.24 这1年多的时间我觉得是我这辈子最宝贵的财富!!   一个懂得爱自己的人,才会知道如何去爱别人.一个太过依赖对方的人,不经历一个失败,永远都不会长大。 用一年的时间来沉淀、埋藏三年的感情,很有必要. bye ~   在这里谢谢我的前女友. 谢谢!! 最后一次祝你幸福~ 

转载于:https://www.cnblogs.com/xiong-QQ357253950/p/3489012.html

相关文章:

计算机书集下载链接

http://nic.biti.edu.cn/china-pub/ 我在那下了一本&#xff1a;XML高级编程转载于:https://www.cnblogs.com/dyuan/archive/2004/03/12/2966.html

Microsoft PlayReady DRM

支持在 Web 浏览器中使用加密媒体扩展和 Microsoft PlayReady DRM 本文内容 简介实现方法万维网联合会 (W3C) HTML5 加密媒体扩展 (EME) 为网站引入了一项功能&#xff0c;可在不使用插件的情况下播放受数字版权管理 (DRM) 保护的内容。Windows 8.1 上的 Internet Explorer 11 …

Android Binder概述

背景知识为了更好的理解binder&#xff0c;我们要先澄清一下概念&#xff0c;因为Android 基于Linux内核&#xff0c;我们有必要了解相关知识。进程隔离进程隔离是为了保护操作系统进程之间互不干扰而设计的&#xff0c;这个技术是为了避免进程A写入进程B准备的&#xff0c;进程…

如何用Asp判断你的网站的虚拟物理路径

appath方法 < p align"center" > < font size"4" face"Arial" > < b > The Physical path to this virtual website is: < /b > < /font > < font color"#FF0000" size"6" face"Aria…

总结:SQL的优缺点及与NoSQL对比

SQL在这里指的是关系型数据库&#xff0c;NoSQL指元组存储&#xff1f;

关于成功的因素-----谨记

引导语&#xff1a;一个人生活的环境&#xff0c;对他树立理想和取得成就有着重要的影响。坚忍&#xff0c;是成大事者的共同特征。没有足够的知识储备&#xff0c;一个人难以在工作和事业中取得突破性进展&#xff0c;难以向更高地位发展。 【意志力】 意志力是一种…

最常用的Linux命令

基础命令&#xff1a; &#xff08;一&#xff09;cd&#xff1a;进入文件目录 cd / #进入Linux根目录 cd ~ #进入当前用户Home目录&#xff0c;例如root用户 cd .. #进入上一层目录(根目录下没有上一层)&#xff0c;…

Delphi数据库开发之TTable篇1

既然是说Delphi中的SQL使用&#xff0c;肯定离不开TDataSet派生出来的DataSet控件&#xff08;应该说类比较确切些&#xff09;&#xff0c;这里我说的DataSet只是一个大类的称呼&#xff0c;并不是特指实际的TDataSet。在众多控件中其实可以根据如何操作数据分为两类。 其中一…

大型数据库入门

本文介绍大型数据库的概念及其性能决定因素&#xff0c;以及如何优化性能。 什么是大型数据库 -没有一个标准定义 -包含非常多元组(数据库行)的数据库&#xff0c;或者占用非常大的物理文件系统存储空间的数据库。 -占据TB量级的磁盘存储&#xff0c;包含数十亿表行。为什么需…

php安装pear和phpunit

php安装pear和phpunit http://hi.baidu.com/zjutxujie/item/7b08761f922df7476926bb2c 安装pear 下载go-pear.phar&#xff0c;放到phproot/pear目录下&#xff0c;地址http://pear.php.net/go-pear.phar。 管理员身份运行cmd&#xff0c;打开phproot&#xff0c;运行php -d p…

ResNets和Inception的理解

ResNets和Inception的理解 ResNet解析

软件体系结构风格(第5-8课时)

摘要&#xff1a;本文给出软件体系结构风格的定义&#xff0c;并介绍几种常用风格&#xff1a;管道过滤器、数据抽象和面向对象组织&#xff0c;基于事件的隐式调用&#xff0c;分层系统&#xff0c;仓库系统&#xff0c;过程控制环路 软件体系结构风格是什么 描述特定系统组织…

C#中调用Windows API时的数据类型对应关系

C#中调用Windows API时的数据类型对应关系 原文 C#中调用Windows API时的数据类型对应关系 BOOLSystem.Int32 BOOLEANSystem.Int32 BYTESystem.UInt16 CHARSystem.Int16 COLORREFSystem.UInt32 DWORDSystem.UInt32 DWORD32System.UInt32 DWORD64System.UInt64 …

linux下安装db2

最近研究了一下在 ubuntu下安装db2的过程&#xff0c;很快就完成安装&#xff0c;特贴出来供大家讨论&#xff0c;如有错误请多多指教。 注意&#xff1a;安装过程请使用root用户&#xff0c;否则会出现安装失败的情况&#xff1b; 安装过程&#xff1a; 准备工作&#xff1a; …

使用 SAX 解析器简化文档处理程序的编写

http://www-900.ibm.com/developerWorks/cn/xml/x-dochan.shtml有时候 SAX 文档处理程序的代码可能变得非常麻烦、结构性差而且难以维护&#xff0c;尤其是对于那些包含多种不同元素的复杂 XML 结构。本文介绍了应对这种问题的设计策略&#xff0c;从而有助于改进代码的质量和可…

软件体系结构风格之C/S,三层C/S,与BS

C/S的物理结构&#xff0c;其发展历程为(1)->(3)->(2)&#xff0c;本文接下来要介绍的C/S为(3),即胖客户端瘦服务器,服务器只管数据库&#xff0c;接下来要介绍的三层C/S为(2)&#xff0c;即客户端不胖不瘦。 C/S软件体系结构 背景&#xff1a;基于资源不对等&#xff0…

HDU-2020

绝对值排序 Problem Description输入n(n<100)个整数&#xff0c;按照绝对值从大到小排序后输出。题目保证对于每一个测试实例&#xff0c;所有的数的绝对值都不相等。 Input输入数据有多组&#xff0c;每组占一行&#xff0c;每行的第一个数字为n,接着是n个整数&#xff0c;…

Java归去来第2集:利用Eclipse创建Maven Web项目

一、前言 如果还不了解剧情&#xff0c;请返回第一集的剧情 Java归去来第1集&#xff1a;手动给Eclipse配置Maven环境 二、利用Eclipse创建Maven Web项目 选择File-New-Project 然后选择Maven-Maven Project 选择项目所在的工作空间目录&#xff0c;然后下一步 选择模…

vs2005什么时候能出正式版

2005试用版本出了很久了&#xff0c;不知道什么时候能出正式版&#xff0c;不会真得要推迟到2005年吧。企盼中…… 转载于:https://www.cnblogs.com/playboy2005/archive/2004/09/22/45563.html

人工神经网络是如何实现存算一体的

摘要 本文内容如题。 下图是一个神经元模型 可以将其抽象为一个数学函数yf(w1x1w2x2…wn*xnb)。也就是这个模型同时涵盖输入输出和进行计算。

编码能力的提升?_20131228

我在编程过程中发现&#xff0c;编码也是一很重要的能力&#xff0c;故花一点时间对其进行思考。 现将思考的成果与各位同行分享&#xff1b;我希望大家在看到以后&#xff0c;能提供宝贵的意见&#xff0c;先谢之&#xff01; 转载于:https://www.cnblogs.com/uestc999/p/3495…

Microsoft PowerToys for Windows XP

它们又卷土重来了&#xff01;PowerToys是由开发人员在软件产品正式发布并投放生产后相继编制的附加程序。它们的出现不仅使Windows体验更加兴味盎然&#xff0c;而且&#xff0c;还为Windows系统平添了许多功能特性。 说明&#xff1a;我们已为确保PowerToys的正常运行而付出了…

mysql主从配置读写分离笔记

第二次回头看了&#xff0c;第一次学完后感觉都会了&#xff0c;回头再看才发现什么都想不起来了。还得查资料再学习&#xff0c;虽然很简单。还是做个笔记吧&#xff01;笔记有点糙 就是自己看的因为主要是测试主从和读写分离 所以直接 yum install -y mariadb mariadb-server…

通用软件测试的6个角度

之后的课程都会围绕此展开 以QQ邮箱为例 外观界面测试 页面外观背景颜色、字体、字体格式、页面图案、动画、窗体布局功能测试 输入正确的用户名和密码可以登录&#xff0c;其他都不可性能测试 输入完邮箱的用户名和密码并单击登录按钮后&#xff0c;用户等待多长时间可以登录…

easyui源码翻译1.32--Messager(消息窗口)

前言 使用$.messager.defaults重写默认值对象。下载该插件翻译源码 消息窗口提供了不同的消息框风格&#xff0c;包含alert(警告框), confirm(确认框), prompt(提示框), progress(进度框)等。所有的消息框都是异步的。用户可以在交互消息之后使用回调函数去处理结果或做一些自己…

新闻网站个人中心(查询用户的关注信息)流程分析

1.获取当前用户登录状态 2.获取参数 用户的id user_id 3.获取新闻作者的id 4.如果新闻作者id和用户id 5.返回内容给前端转载于:https://www.cnblogs.com/zxt-cn/p/9158860.html

物联网技术与应用(第1-2课时)(cont.)

物联网的定义&#xff1a; 通过射频识别&#xff08;RFID&#xff09;、红外感应器、全球定位系统、激光扫描器等信息传感设备&#xff0c;按约定的协议&#xff0c;将任何物品通过有线与无线方式与互联网连接&#xff0c;进行通信和信息交换&#xff0c;以实现智能化识别、定位…

db2 基础语法

一、db2 基础 基本语法 注释&#xff1a;“--”&#xff08;两个减号&#xff09; 字符串连接&#xff1a;“||” 如set msg’aaaa’||’bbbb’&#xff0c;则msg为’aaaabbbb’ 字符串的引用&#xff1a;‘’&#xff08;一定用单引号&#xff09;&#xff0c;如果需要输入单引…

Repeater分页代码

//ASP.NET中的DataList和Repeater提供了简单快速的方法來显示,其间&#xff0c;我们可以使用<ItemTemplate>更是使我们能随心所欲的控制数据的排放样式&#xff01; //.可惜的是它们不像Datagrid那样&#xff0c;有内置的分页功能。 // //如何解决呢&#xff1f; // //其…