批量修改图片以及加水印

本文介绍了一种使用C#实现的批量为图片添加水印的方法,该方法通过遍历指定文件夹内的图片文件,并对其进行处理以实现水印效果。此外,文中还附带了一个个人情感分享。

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值