如何遍历文件夹里的图片,,然后给这些图片打水印呢
如何遍历文件夹里的图片,,然后给这些图片打水印呢
遍历文件夹里的图片,是用什么方法
谢谢了
[解决办法]
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 image
FrameDimension 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);
}
[解决办法]
下面是遍历文件夹的代码
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.addWatermarkImage( g,Page.Server.MapPath(Watermarkimgpath),WatermarkPosition,image.Width,image.Height);
break;
//如果是文字
case "WM_TEXT ":
this.addWatermarkText( g, WatermarkText,WatermarkPosition
,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
估计能符合你的要求