修改
using System;using System.Collections.Generic;using System.Collections;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;using System.Drawing;using System.Threading;using System.Drawing.Imaging;
using DotNetNuke;using DotNetNuke.Common.Utilities;using DotNetNuke.Security;using DotNetNuke.Services.Exceptions;using DotNetNuke.Services.Localization;using DotNetNuke.Entities.Modules;using DotNetNuke.Entities.Modules.Actions;using Telerik.Web.UI;
namespace xyz_CarPT{ public partial class BatchWatermarkfight : PortalModuleBase, IActionable {
#region "Optional Interfaces"
/// ----------------------------------------------------------------------------- /// <summary> /// Registers the module actions required for interfacing with the portal framework /// </summary> /// <value></value> /// <returns></returns> /// <remarks></remarks> /// <history> /// </history> /// ----------------------------------------------------------------------------- public ModuleActionCollection ModuleActions { get { ModuleActionCollection Actions = new ModuleActionCollection(); Actions.Add(GetNextActionID(), Localization.GetString(ModuleActionType.AddContent, this.LocalResourceFile), ModuleActionType.AddContent, "", "add.gif", EditUrl(), false, DotNetNuke.Security.SecurityAccessLevel.Edit, true, false); return Actions; } } #endregion protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GetWatermark(DropDownList1); }
}
public void GetWatermark(DropDownList combox) { if (!Page.IsPostBack) { string path = Server.MapPath(".") + "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\Watermark\\"; string[] files = Directory.GetFiles(path); List<string> fileslist = new List<string>(); DropDownList combobox = combox; int i = 0; for (i = 0; i < files.Length; i++) { string oimg = files[i].ToString(); if (IsImg(oimg)) { fileslist.Add(oimg); } } combobox.DataSource = fileslist; combobox.DataBind(); } }
protected void Button1_Click(object sender, EventArgs e) { string path = Server.MapPath(".") + "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\showcar2\\"; //图片文件夹; string[] files = Directory.GetFiles(path); //获得图片文件夹下所有图片 string watchwatermarkurl = DropDownList1.SelectedValue.ToString(); //水印图片路径 string saveimgurl = "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\showcar2"; //新图片保存的路径 int i = 0; for(i=0;i<files.Length;i++) { string oimg = files[i].ToString(); if (i % 100 == 0) { Thread.Sleep(10); GC.Collect(); } if (IsImg(oimg)) {
if (!oimg.Contains("_0_")) { try { batchwater(oimg, watchwatermarkurl,saveimgurl); }catch(Exception ex) { Console.WriteLine(ex.ToString()); } } } } }
private void batchwater(string imgurl,string imgurl2,string saveimgurl) { if (imgurl != "" && imgurl2 != "") { //加图片水印 System.Drawing.Image image = System.Drawing.Image.FromFile(imgurl); System.Drawing.Image copyImage = System.Drawing.Image.FromFile(imgurl2); //FromFile(Server.MapPath(".") + imgurl2); Graphics g = Graphics.FromImage(image); g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); g.Dispose(); //保存加水印过后的图片" string path = Server.MapPath(".") + saveimgurl; string newPath = path+imgurl.Substring(imgurl.LastIndexOf("\\")); if (File.Exists(newPath)) { File.Delete(newPath); } image.Save(newPath); copyImage.Dispose(); image.Dispose(); } }
//using System.Drawing; //using System.IO; //using System.Drawing.Imaging;
private void AddTextToImg(string fileName, string text) { if (!File.Exists(fileName)) { throw new FileNotFoundException("The file don't exist!"); }
if (text == string.Empty) { return; } //还需要判断文件类型是否为图像类型,这里就不赘述了
System.Drawing.Image image = System.Drawing.Image.FromFile(fileName); Bitmap bitmap = new Bitmap(image, image.Width, image.Height); Graphics g = Graphics.FromImage(bitmap);
float fontSize = 12.0f; //字体大小 float textWidth = text.Length * fontSize; //文本的长度 //下面定义一个矩形区域,以后在这个矩形里画上白底黑字 float rectX = 0; float rectY = 0; float rectWidth = text.Length * (fontSize + 8); float rectHeight = fontSize + 8; //声明矩形域 RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);
Font font = new Font("宋体", fontSize); //定义字体 Brush whiteBrush = new SolidBrush(Color.White); //白笔刷,画文字用 Brush blackBrush = new SolidBrush(Color.Black); //黑笔刷,画背景用
g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);
g.DrawString(text, font, whiteBrush, textArea); MemoryStream ms = new MemoryStream(); //保存为Jpg类型 bitmap.Save(ms, ImageFormat.Jpeg);
//输出处理后的图像,这里为了演示方便,我将图片显示在页面中了 Response.Clear(); Response.ContentType = "image/jpeg"; Response.BinaryWrite(ms.ToArray());
g.Dispose(); bitmap.Dispose(); image.Dispose(); }
#region 判断文件是否为图片 /// </summary> /// <param name="filePath">filePath</param> /// <returns></returns> public static bool IsImg(string filePath) { bool v = false; if (File.Exists(filePath)) { try { FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(fs); string fileClass; byte buffer; byte[] b = new byte[2]; buffer = reader.ReadByte(); b[0] = buffer; fileClass = buffer.ToString(); buffer = reader.ReadByte(); b[1] = buffer; fileClass += buffer.ToString(); reader.Close(); fs.Close(); //255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar if (fileClass == "255216" || fileClass == "7173" || fileClass == "6677" || fileClass == "13780") { v = true; } else { v = false; } } catch { v = false; } } else { v = false; } return v; } #endregion
protected void Button2_Click(object sender, EventArgs e) { AddTextToImg(Server.MapPath(".") + "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\showcar2\\27_2_9.jpg","sikaodelang"); }
}}