Modify images and watermarks in batches.
The effect is as follows:
Public void plAddSY (object sender, EventArgs e) {// C # traverse all the files in the specified folder DirectoryInfo TheFolder = new DirectoryInfo (Request. physicalApplicationPath + "images \ Product"); // DirectoryInfo TheFolder = new DirectoryInfo ("D: \ WebFiles \ Newb2c \ images \ Product "); // traverse the folder 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") {// traverse the file 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 ("ipv-380 _") {MarkWater (NextFile. fullName, Request. physicalApplicationPath + "images \" + "logo-back.png"); // MarkWater (NextFile. fullName, "D: \ WebFiles \ Newb2c \ images \" + "logo-back.png ");}}}}}}} /// <summary> /// watermark the image /// </summary> /// <param name = "filePath"> original image address </param> // /<param name = "waterFile"> watermark image address </param> public void MarkWater (string filePath, string w AterFile) {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, 80 0, "DB", "JPG"); string ModifyImagePath = newlastimg; int bytes cypercent = 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}, new float [] {0, 1, 0, 0, 0}, new float [] {0, 0, 1, 0, 0}, new float [] {0, 0, 0, (float) Then cypercent/100f, 0}, new float [] {0, 0, 0, 0, 1 }}; ColorMatrix colorMatrix = new ColorMatrix (matrixItems); ImageAttributes imgAttr = new ImageAttributes (); imgAttr. setColorMatrix (colorMatrix, ColorM AtrixFlag. 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 M EmoryStream (); 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> // generate a thumbnail /// </summary> /// <param name = "originalImagePath"> source Image PATH (physical path) </param> /// <param name = "thumbnailPath"> thumbnail path (physical path) </param> /// <param name = "width"> thumbnail width </param> /// <param name = "height"> thumbnail height </param> // /<param name = "mode"> how to generate a thumbnail </param> // <param name = "type"> the image type of the thumbnail </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": // specify the Height/width Scaling (may be deformed) break; case "W": // specify the width, high toheight = originalImage. height * width/originalImage. width; break; case "H": // specify the height. The Width is proportional to towidth = originalImage. width * height/originalImage. height; break; case "Cut": // specify the Height and width (not deformed) 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": // proportional Scaling (without deformation, if the Height is high, the width is wide and scaled) 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;} // create a new bmp Image bitmap = new Bitmap (towidth, toheight); // create a new drawing board Graphics g = Graphics. fromImage (bitmap); // set the high quality interpolation method g. interpolationMode = System. drawing. drawing2D. interpolationMode. high; // set High quality, low speed rendering smoothness g. smoothingMode = System. drawing. drawing2D. smoothingMode. highQuality; // clear the canvas and fill g with a transparent background color. clear (Color. transparent); // specify the position and size of the original image. drawImage (originalImage, new Rectangle (0, 0, towidth, toheight), new Rectangle (x, y, ow, oh), GraphicsUnit. pixel); try {// Save the thumbnail 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 ();}}}