如何遍历文件夹里的图片,,然后给这些图片打水印呢 如何遍历文件夹里的图片,,然后给这些图片打水印呢遍历文件夹里的图片,是用什么方法谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 system.io判断文件扩展名然后打开图片,在上面画图 缩略图水印组件wsImage3.5 批量加就可以 string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper(); string fileName = DateTime.Now.ToString("yyyyMMddhhmmss"); string path = Server.MapPath(".") + "/upload/" + fileName + extension; File1.PostedFile.SaveAs(path); System.Drawing.Image image = System.Drawing.Image.FromFile(path);System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/logo.gif"); //Create a new FrameDimension object from this imageFrameDimension ImgFrmDim = new FrameDimension( image.FrameDimensionsList[0] );int nFrameCount = image.GetFrameCount( ImgFrmDim );// Save every frame into jpeg format for( int i = 0; i < nFrameCount; i++ ){image.SelectActiveFrame( ImgFrmDim, i );image.Save( string.Format( Server.MapPath(".") + "/upload/Frame{0}.jpg", i ), ImageFormat.Jpeg );}image.Dispose();for( int i = 0; i < nFrameCount; i++ ){string pa = Server.MapPath(".") + "/upload/Frame"+i+".jpg";System.Drawing.Image Image = System.Drawing.Image.FromFile(pa);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 newPath = Server.MapPath(".") + "/upload/" + fileName + "_new"+i.ToString()+ extension;Image.Save(newPath);Image.Dispose();if(File.Exists(pa)){ File.Delete(pa);} }if(File.Exists(path)) { File.Delete(path); } .net 可以直接弄。。不用组件。 下面是遍历文件夹的代码 private void Page_Load(object sender, System.EventArgs e) { if (Session["UserName"] == null) { Response.Redirect("login.aspx"); } DelFile(); // 在此处放置用户代码以初始化页面 string strdirlist = Request.QueryString.Get("dir"); string FileList,ImgFilePath,SysPath; SysPath = "文件夹的虚拟路径"; if (strdirlist == null) { strdirlist = Server.MapPath(Application["UpLoadFilePath"].ToString()); } ImgFilePath = strdirlist.Replace(SysPath, "../"); DirectoryInfo directory = null; try { directory = new DirectoryInfo(strdirlist); //读取目录属性 FileList = ("<p>Creation:" + directory.CreationTime.ToString() + "</p>"); if (!(strdirlist == null || strdirlist == Server.MapPath(Application["UpLoadFilePath"].ToString()))) { FileList = FileList + "<ul><li><a href=\"javascript:history.go(-1);\">...</a></li></ul>"; } DirectoryInfo[] subDirectory = directory.GetDirectories(); FileList = FileList + "<ul>\n"; for (int i = 0; i < subDirectory.Length; i++) { FileList = FileList + "<li>"; FileList = FileList + "<a href=\"FileList.aspx?dir=" + strdirlist + "&IsFile=false \">×</a> "; FileList = FileList + "<a href=\"FileList.aspx?dir=" + Server.UrlEncode(subDirectory[i].FullName) + "\">" + subDirectory[i].Name; FileList = FileList + "</a></li>\n"; } FileList = FileList + "</ul>\n"; FileInfo[] theFiles = directory.GetFiles();//可以使用GetFiles("*.txt")进行文件搜索 FileList = FileList + "<ul>\n"; for (int i = 0; i < theFiles.Length; i++) { FileList = FileList + "<li>"; FileList = FileList + "<a href=\"#\"onclick=\"javascript:if (confirm('你确定删除吗?')) href='FileList.aspx?dir=" + strdirlist + "&IsFile=ok&DelFile=" + ImgFilePath + "/" + theFiles[i].Name + " ';else return; \">×</a> "; FileList = FileList + " <a href=\" " + ImgFilePath + "/" + theFiles[i].Name + " \" target=\"_blank\">" + theFiles[i].Name + "</a> <span style='color: #999999'>" + theFiles[i].Length.ToString() + " Byte " + theFiles[i].LastWriteTimeUtc.ToString() + "</span></li>\n"; } FileList = FileList + "</ul>\n"; FileDir.Text = FileList; } catch (Exception ex) { Response.Write("Access not possible. error:<i>"); Response.Write(e.ToString() + "</i>"); Response.End(); } } private void DelFile() { string DelImgUrl = Request.QueryString["DelFile"]; DelImgUrl = Server.MapPath(DelImgUrl); if (Request.QueryString["IsFile"] == "ok") { if (File.Exists(DelImgUrl)) { File.Delete(DelImgUrl); } Response.Write(Request.QueryString["DelFile"]); } if (Request.QueryString["IsFile"] == "false") { Response.Write(Request.QueryString["dir"]); } } /**//// <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(如果需要填加水印) { switch(水印类型) { //是图片的话 case "WM_IMAGE": this.addWaterImage( g,Page.Server.MapPath(Waterimgpath),WaterPosition,image.Width,image.Height); break; //如果是文字 case "WM_TEXT": this.addWaterText( g, WaterText,WaterPosition ,image.Width,image.Height); break; } b.Save(newpath); b.Dispose(); image.Dispose(); } } catch { if(File.Exists(oldpath)) { File.Delete(oldpath); } } finally { if(File.Exists(oldpath)) { File.Delete(oldpath); } } } 前期写了一个代码网站水印工具 。http://download.csdn.net/source/161135估计能符合你的要求 服务器控件treeView用jquery技术怎么实现拖拽的效果啊 Provider模式和基于Web的三层模式的区别 如何用正则排除特殊符号 这个calendar控件怎么变丑了? 导出gridview的数据到excel Repeater ItemTemplate的简单问题!在线求解决马上结贴! 问一个关于用户session、cookies过期的问题 Spring.Net配置文件出错 一个关于弹出窗口的问题?请赐教! 用.net调用水晶报表 把鼠标放到控件上没有提示出来了 问个临时cookie方式登录验证后的问题
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss"); string path = Server.MapPath(".") + "/upload/" + fileName + extension;
File1.PostedFile.SaveAs(path);
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/logo.gif");
//Create a new FrameDimension object from this imageFrameDimension ImgFrmDim = new FrameDimension( image.FrameDimensionsList[0] );int nFrameCount = image.GetFrameCount( ImgFrmDim );// Save every frame into jpeg format for( int i = 0; i < nFrameCount; i++ )
{
image.SelectActiveFrame( ImgFrmDim, i );image.Save( string.Format( Server.MapPath(".") + "/upload/Frame{0}.jpg", i ), ImageFormat.Jpeg );
}
image.Dispose();for( int i = 0; i < nFrameCount; i++ )
{
string pa = Server.MapPath(".") + "/upload/Frame"+i+".jpg";
System.Drawing.Image Image = System.Drawing.Image.FromFile(pa);
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 newPath = Server.MapPath(".") + "/upload/" + fileName + "_new"+i.ToString()+ extension;
Image.Save(newPath);Image.Dispose();if(File.Exists(pa))
{
File.Delete(pa);
} }
if(File.Exists(path))
{
File.Delete(path);
}
{
if (Session["UserName"] == null)
{
Response.Redirect("login.aspx");
}
DelFile();
// 在此处放置用户代码以初始化页面
string strdirlist = Request.QueryString.Get("dir");
string FileList,ImgFilePath,SysPath;
SysPath = "文件夹的虚拟路径";
if (strdirlist == null)
{
strdirlist = Server.MapPath(Application["UpLoadFilePath"].ToString());
}
ImgFilePath = strdirlist.Replace(SysPath, "../");
DirectoryInfo directory = null;
try
{
directory = new DirectoryInfo(strdirlist);
//读取目录属性
FileList = ("<p>Creation:" + directory.CreationTime.ToString() + "</p>");
if (!(strdirlist == null || strdirlist == Server.MapPath(Application["UpLoadFilePath"].ToString())))
{
FileList = FileList + "<ul><li><a href=\"javascript:history.go(-1);\">...</a></li></ul>";
}
DirectoryInfo[] subDirectory = directory.GetDirectories();
FileList = FileList + "<ul>\n";
for (int i = 0; i < subDirectory.Length; i++)
{
FileList = FileList + "<li>";
FileList = FileList + "<a href=\"FileList.aspx?dir=" + strdirlist + "&IsFile=false \">×</a> ";
FileList = FileList + "<a href=\"FileList.aspx?dir=" + Server.UrlEncode(subDirectory[i].FullName) + "\">" + subDirectory[i].Name;
FileList = FileList + "</a></li>\n";
}
FileList = FileList + "</ul>\n";
FileInfo[] theFiles = directory.GetFiles();//可以使用GetFiles("*.txt")进行文件搜索
FileList = FileList + "<ul>\n";
for (int i = 0; i < theFiles.Length; i++)
{
FileList = FileList + "<li>";
FileList = FileList + "<a href=\"#\"onclick=\"javascript:if (confirm('你确定删除吗?')) href='FileList.aspx?dir=" + strdirlist + "&IsFile=ok&DelFile=" + ImgFilePath + "/" + theFiles[i].Name + " ';else return; \">×</a> ";
FileList = FileList + " <a href=\" " + ImgFilePath + "/" + theFiles[i].Name + " \" target=\"_blank\">" + theFiles[i].Name + "</a> <span style='color: #999999'>" + theFiles[i].Length.ToString() + " Byte " + theFiles[i].LastWriteTimeUtc.ToString() + "</span></li>\n";
}
FileList = FileList + "</ul>\n";
FileDir.Text = FileList;
}
catch (Exception ex)
{
Response.Write("Access not possible. error:<i>");
Response.Write(e.ToString() + "</i>");
Response.End();
}
}
private void DelFile()
{
string DelImgUrl = Request.QueryString["DelFile"];
DelImgUrl = Server.MapPath(DelImgUrl);
if (Request.QueryString["IsFile"] == "ok")
{
if (File.Exists(DelImgUrl))
{
File.Delete(DelImgUrl);
}
Response.Write(Request.QueryString["DelFile"]);
}
if (Request.QueryString["IsFile"] == "false")
{
Response.Write(Request.QueryString["dir"]);
}
}
/// 添加图片水印
/// </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(如果需要填加水印)
{
switch(水印类型)
{
//是图片的话
case "WM_IMAGE":
this.addWaterImage( g,Page.Server.MapPath(Waterimgpath),WaterPosition,image.Width,image.Height);
break;
//如果是文字
case "WM_TEXT":
this.addWaterText( g, WaterText,WaterPosition
,image.Width,image.Height);
break;
} b.Save(newpath);
b.Dispose();
image.Dispose();
} }
catch
{
if(File.Exists(oldpath))
{
File.Delete(oldpath);
}
}
finally
{
if(File.Exists(oldpath))
{
File.Delete(oldpath);
}
}
}
估计能符合你的要求