using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Drawing;using System.Drawing.Design;using System.Drawing.Imaging;using System.IO;
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 CarImageUpload : 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
private static string carid = ""; Random r = null; protected void Page_Load(object sender, EventArgs e) { carid = "888"; //Request.QueryString["carid"].ToString(); 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 buttonSubmit_Click(object sender, System.EventArgs e) { string imagedirectory = "\\akd\\dnn\\Portals\\_default\\Skins\\xyz_akd\\img\\CarsPhoto\\showcar\\";
if (RadUpload1.UploadedFiles.Count > 0) { for (int i = 0; i < RadUpload1.UploadedFiles.Count; i = i++) { string filename = RadUpload1.UploadedFiles[i].FileName;
int j = i; string imagename = carid + DropDownList1.SelectedValue.ToString() + j+".jpg"; if (!System.IO.Directory.Exists(imagedirectory + imagename)) { UpPic(RadUpload1.UploadedFiles[i], imagedirectory, imagename); } else { j++; UpPic(RadUpload1.UploadedFiles[i], imagedirectory, imagename);
} } }
}
/// <summary> /// 上传图片代码 /// </summary> /// <param name="image_file">HtmlInputFile控件</param> /// <param name="ImgPath">存放的文件夹绝对位置</param> /// <param name="ImgLink">生成的图片的名称带后缀</param> /// <returns></returns> public bool UpPic(UploadedFile image_file, string ImgPath, string ImgLink) { if (image_file.FileName != null && image_file.FileName.Trim() != "") { try { if (!System.IO.Directory.Exists(ImgPath)) { System.IO.Directory.CreateDirectory(ImgPath); } //如果显示水印 if (true) { image_file.SaveAs(ImgPath + ImgLink); //加水印 this.addWaterMark((ImgPath + ImgLink), (ImgPath + ImgLink)); } return true; }
catch { return false; } } else { return false; } }
/// <summary> /// 添加图片水印 /// </summary> /// <param name="oldpath">原图片绝对地址</param> /// <param name="newpath">新图片放置的绝对地址</param> private void addWaterMark(string oldpath, string newpath) { try { System.Drawing.Image image = System.Drawing.Image.FromFile(oldpath); Bitmap b = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(b); g.Clear(Color.White); //g.SmoothingMode = SmoothingMode.HighQuality; //g.InterpolationMode = InterpolationMode.High;
g.DrawImage(image, 0, 0, image.Width, image.Height);
if (true) { switch ("WM_IMAGE") { //是图片的话 case "WM_IMAGE": this.addWatermarkImage(g, "d:\\shuiying.png", "WM_TOP_LEFT", image.Width, image.Height); break; default: break; } b.Save(newpath); b.Dispose(); image.Dispose(); } } catch { if (File.Exists(oldpath)) { //File.Delete(oldpath); } } finally { if (File.Exists(oldpath)) { //File.Delete(oldpath); } } }
/// <summary> /// 加水印图片 /// </summary> /// <param name="picture">imge 对象</param> /// <param name="WaterMarkPicPath">水印图片的地址</param> /// <param name="_watermarkPosition">水印位置</param> /// <param name="_width">被加水印图片的宽</param> /// <param name="_height">被加水印图片的高</param> private void addWatermarkImage(Graphics picture, string WaterMarkPicPath, string _watermarkPosition, int _width, int _height) { Bitmap watermark = new Bitmap(WaterMarkPicPath);
ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} };
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
int xpos = 0; int ypos = 0; int WatermarkWidth = 0; int WatermarkHeight = 0; double bl = 1d; //计算水印图片的比率 //取背景的1/4宽度来比较 if ((_width > watermark.Width * 4) && (_height > watermark.Height * 4)) { bl = 1; } else if ((_width > watermark.Width * 4) && (_height < watermark.Height * 4)) { bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);
} else
if ((_width < watermark.Width * 4) && (_height > watermark.Height * 4)) { bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width); } else { if ((_width * watermark.Height) > (_height * watermark.Width)) { bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);
} else { bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
}
}
WatermarkWidth = Convert.ToInt32(watermark.Width * bl); WatermarkHeight = Convert.ToInt32(watermark.Height * bl);
switch (_watermarkPosition) { case "WM_TOP_LEFT": xpos = 10; ypos = 10; break; case "WM_TOP_RIGHT": xpos = _width - WatermarkWidth - 10; ypos = 10; break; case "WM_BOTTOM_RIGHT": xpos = _width - WatermarkWidth - 10; ypos = _height - WatermarkHeight - 10; break; case "WM_BOTTOM_LEFT": xpos = 10; ypos = _height - WatermarkHeight - 10; break; }
picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
watermark.Dispose(); imageAttributes.Dispose(); }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="oldpath">原图片地址</param> /// <param name="newpath">新图片地址</param> /// <param name="tWidth">缩略图的宽</param> /// <param name="tHeight">缩略图的高</param> private void GreateMiniImage(string oldpath,string newpath,int tWidth, int tHeight) { try {
System.Drawing.Image image = System.Drawing.Image.FromFile(oldpath); double bl=1d; if((image.Width<=image.Height)&&(tWidth>=tHeight)) { bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight); } else if((image.Width>image.Height)&&(tWidth<tHeight)) { bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth); } else if((image.Width<=image.Height)&&(tWidth<=tHeight)) { if(image.Height/tHeight>=image.Width/tWidth) { bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth); } else { bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight); } } else { if(image.Height/tHeight>=image.Width/tWidth) { bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight); } else { bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth); } }
Bitmap b = new Bitmap(image ,Convert.ToInt32(image.Width/bl), Convert.ToInt32(image.Height/bl));
b.Save(newpath); b.Dispose(); image.Dispose();
} catch { } }
}}