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

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

批量修改图片以及加水印相关推荐

  1. 如何批量在图片上加统一的文字?

    工作中我们对图片的需求可谓是五花八门,比如批量在图片上加上相同的文字,相信不少小伙伴遇到过, 遇到这种情况的时候你一般会怎么处理呢?有些小伙伴会乖乖的用画图工具或PS软件一个一个添加,这样既不能做到统 ...

  2. python批量修改图片格式和名称

    在制作数据集的过程中发现找到的图片名称和格式都很混乱,找了别人的发现老是报错. 同时总结了一下我自己这边程序报的错误: 1..jpg已存在** 比如我现在存在2.jpg,通过代码修改也会得到2.jpg ...

  3. 批量修改图片尺寸的方法

    批量修改图片尺寸的方法 在日常的工作和生活中,我们经常会遇到需要对图片的尺寸进行修改,只需要修改几张还好,可以要修改几十张甚至更多的情况下,就真的很枯燥很费时间了.如果能够批量修改就好了. 我发现还真 ...

  4. 批量修改图片大小软件easy image modifier plus绿色版下载

    easy image modifier是非常简单的图片批量调整修改工具.它拥有翻转.旋转.添加浮水印.修改图片大小.排序.重命名等等功能.使用操作也是非常的简单,软件的整体界面也是简单,一目了然,就算 ...

  5. 用Python批量修改图片大小的代码

    可以使用Python的PIL库来实现批量修改图片大小,示例代码如下: from PIL import Image # 加载图片 image = Image.open('filename') #修改图片 ...

  6. 怎么批量在图片上加logo?

    怎么批量在图片上加logo?对于很多刚入职场不久的小白来说,批量给图片添加logo确实是一项艰难的操作,甚至很多人压根不知道如何给图片添加logo.不过大部分的小伙伴只知道如何一张一张的处理,虽然能达 ...

  7. 如何在图片上加水印?只要四个步骤

    如何在图片上加水印?大家平时都是用什么方法在图片上添加水印的?其实现在给图片添加水印的方法还是有很多的,在网上能够找到不少的教程,但是真正操作简单又好用的方法并不多.就拿最常见的ps给图片加水印的方法 ...

  8. 如何批量在图片上加二维码?

    二维码可以包含很多的信息,比如联系方式,个人简介等信息,所以很多小伙伴都想推广自己的二维码,所以有不少人将二维码加在图片上以达到更好的推广效果. 在图片上加二维码其实和在图片上加logo水印的方式是一 ...

  9. idea批量修改变量快捷键mac_使用Mac自带功能批量修改图片名称、类型和压缩图片大小...

    Mac电脑自带的聚焦搜索功能已经很强大,可以快速准确的搜索电脑内的文件,但是从网上下载的文件,特别是一些图片文件都是一串数字组成的文件名,很难记忆和搜索,我们可以使用Mac自带的『自动操作』功能给图片 ...

最新文章

  1. 网络爬虫:采用“负载均衡”策略来优化网络爬虫
  2. [Android] Implementation vs API dependency
  3. 最炫国漫《雾山五行》用 Python 了解一下到底有多优秀
  4. java的守护进程与非守护进程
  5. oracle模糊查询很慢,采用全文索引解决模糊查询速度慢的问题
  6. MATLAB使用教程(三)——在文件中编程
  7. 摘:多线程和异步的区别
  8. 精心整理全网最全Tomcat面试专题及答案(共19题,含答案解析),tomcat面试看这篇就够了!
  9. 做真正Hacker 的乐趣──自己动手去实践
  10. j2me 移植 android,J2me移植Android初步探索
  11. HashMap排序(java)
  12. BIM技术在各阶段的应用简单介绍
  13. cad完全卸载教程_如何完全卸载(删除)cad-百度经验
  14. 【HTTP图片服务器】【项目记录2】:安装、配置MySQL环境
  15. 学习VTK9笔记(三)打开stl文件
  16. 南京外企100万,杭州阿里p8 150万,怎么选?
  17. 人工智能技术与专利技术变革
  18. 一文搞定校验码(奇偶校验,海明,CRC 码)
  19. 1660s功耗多少w_华硕GTX1660S功耗及散热对比GTX1060 3G 这才是甜品显卡的优势
  20. php handle 用用法,PHP_关于php fread()使用技巧,说明 string fread ( int handle, int le - phpStudy...

热门文章

  1. 打包图片上传cdn_紧抓5G新时代机遇 又拍云创新发展CDN行业
  2. android html.fromhtml 字体加粗,Android Html设置TextView的颜色、加粗样式
  3. jquery控制只监听数字_jQuery老矣,尚能饭否
  4. C语言基础知识(自己做个笔记,云储存一下)
  5. java数据和窗口怎么结合_卖jsp编程技巧的那个垃圾的所有实例的答案全部
  6. laravel ajax返回json,Laravel validate error处理,ajax,json示例
  7. 树莓派/PC实现实时摄像头数据共享(Python—OpenCV)
  8. 深度学习(4)基础4 -- 神经网络架构激活函数过拟合处理
  9. 【MediaPipe】(2) AI视觉,人体姿态关键点实时跟踪,附python完整代码
  10. 【opencv】(3) 图像滤波:均值、方框、中值、高斯