sd
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) { Image1.ImageUrl = "/dnn/Portals/_default/Skins/xyz_akd/img/CarsPhoto/Watermark/CLS63AMG.jpg"; Image2.ImageUrl = "/dnn/Portals/_default/Skins/xyz_akd/img/CarsPhoto/wmarkexample.jpg"; GetWatermark(DropDownList1); }
}
public void GetWatermark(DropDownList combox) { 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\\Orashowcar\\"; //图片文件夹; string[] files = Directory.GetFiles(path); //获得图片文件夹下所有图片 string watchwatermarkurl = DropDownList1.SelectedValue.ToString(); //水印图片路径 string saveimgurl = "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\showcar"; //新图片保存的路径 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); Graphics g = Graphics.FromImage(image);
int xpos = 0; int ypos = 0; switch (RadioButtonList1.SelectedValue.ToString()) { case "左上角": xpos = 0; ypos = 0; break; case "右上角": xpos = ((image.Width - copyImage.Width)); ypos = 0; break; case "右下角": xpos = ((image.Width - copyImage.Width)); ypos = image.Height - copyImage.Height; break; case "左下角": xpos = 0; ypos = image.Height - copyImage.Height; break; case "上中": xpos = ((image.Width)/2 - (copyImage.Width/2)); ypos = 0; break; case "下中": xpos = ((image.Width) / 2 - (copyImage.Width / 2)); ypos = image.Height - copyImage.Height; break; } g.DrawImage(copyImage, new Rectangle(xpos,ypos,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(); } }
private void AddTextToImg(string imgurl,string saveimgurl,string text) { if (!File.Exists(imgurl)) { throw new FileNotFoundException("The file don't exist!"); }
if (text == string.Empty) { return; } //还需要判断文件类型是否为图像类型,这里就不赘述了
System.Drawing.Image image = System.Drawing.Image.FromFile(imgurl); 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 DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string imagname = this.DropDownList1.SelectedValue.ToString(); string filename = imagname.Substring(imagname.LastIndexOf("\\")+1); Image1.ImageUrl = "/dnn/Portals/_default/Skins/xyz_akd/img/CarsPhoto/Watermark/"+filename; string image2 = Image2.ImageUrl.ToString(); string exampleimg = Server.MapPath(".") + "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\" + image2.Substring(image2.LastIndexOf("/") + 1);
string imgstate = RadioButtonList1.SelectedValue.ToString(); switch (imgstate) { case "左上角": movebtchwater(exampleimg, imagname, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "1"); break; case "右上角": movebtchwater(exampleimg, imagname, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "2"); break; case "右下角": movebtchwater(exampleimg, imagname, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "3"); break; case "左下角": movebtchwater(exampleimg, imagname, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "4"); break; case "上中": movebtchwater(exampleimg, imagname, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "5"); break; case "下中": movebtchwater(exampleimg, imagname, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "6"); break; default: waterbtchwater(exampleimg, imagname, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample"); break; } }
private void waterbtchwater(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); 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); Image2.ImageUrl = "/dnn/Portals/_default/Skins/xyz_akd/img/CarsPhoto/moveexample/" + newPath.Substring(newPath.LastIndexOf("\\")+1); copyImage.Dispose(); image.Dispose(); } }
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) { string imgstate = RadioButtonList1.SelectedValue.ToString(); string imgurl2 = this.DropDownList1.SelectedValue.ToString(); string imgurl1 = Image2.ImageUrl.ToString(); string exampleimg = Server.MapPath(".") + "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\" + imgurl1.Substring(imgurl1.LastIndexOf("/") + 1); switch (imgstate) { case "左上角": movebtchwater(exampleimg, imgurl2, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "1"); break; case "右上角": movebtchwater(exampleimg, imgurl2, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "2"); break; case "右下角": movebtchwater(exampleimg, imgurl2, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "3"); break; case "左下角": movebtchwater(exampleimg, imgurl2, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "4"); break; case "上中": movebtchwater(exampleimg, imgurl2, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "5"); break; case "下中": movebtchwater(exampleimg, imgurl2, "\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\moveexample", "6"); break; default : break; } }
private void movebtchwater(string imgurl, string imgurl2, string saveimgurl,string weizhi) { if (imgurl != "" && imgurl2 != "") { //加图片水印 System.Drawing.Image image = System.Drawing.Image.FromFile(imgurl); System.Drawing.Image copyImage = System.Drawing.Image.FromFile(imgurl2); Graphics g = Graphics.FromImage(image);
int xpos = 0; int ypos = 0; switch (weizhi) { case "1": xpos = 0; ypos = 0; break; case "2": xpos = ((image.Width - copyImage.Width)); ypos = 0; break; case "3": xpos = ((image.Width - copyImage.Width)); ypos = image.Height - copyImage.Height; break; case "4": xpos = 0; ypos = image.Height - copyImage.Height; break; case "5": xpos = ((image.Width) / 2 - (copyImage.Width / 2)); ypos = 0; break; case "6": xpos = ((image.Width) / 2 - (copyImage.Width / 2)); ypos = image.Height - copyImage.Height; break; } g.DrawImage(copyImage, new Rectangle(xpos, ypos, 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); Image2.ImageUrl = "/dnn/Portals/_default/Skins/xyz_akd/img/CarsPhoto/moveexample/" + newPath.Substring(newPath.LastIndexOf("\\") + 1); copyImage.Dispose(); image.Dispose(); } }
}}