要合成图片的主图片 ///fileFoldUrl">需要合成图片的根目录,去循环里面的图片 publicvoidSynthesisPhoto(string muban,stringfileFoldUrl) { //_c#中图像怎样合成方法">
赞
踩
///<summary>
///合成图片
///</summary>
///<paramname="muban">要合成图片的主图片</param>
///<paramname="fileFoldUrl">需要合成图片的根目录,去循环里面的图片</param>
publicvoidSynthesisPhoto(string muban,stringfileFoldUrl)
{
//stringmuban = "E:\\muban";
//stringfileFoldUrl = "E:\\20160831";
if(Directory.GetDirectories(fileFoldUrl).Length > 0)
{
foreach (stringfoleder in Directory.GetDirectories(fileFoldUrl))
{
foreach (stringfile in Directory.GetFiles(foleder))
{
stringnewFile = string.Empty;
//给图片加水印图片
System.Drawing.Image image = System.Drawing.Image.FromFile(Directory.GetFiles(muban)[0]);
System.Drawing.Image copuImage = System.Drawing.Image.FromFile(file);
Graphics g = Graphics.FromImage(image);
g.DrawImage(copuImage, newRectangle(319, (image.Height - copuImage.Height) / 2,copuImage.Width, copuImage.Height), 0, 0, copuImage.Width, copuImage.Height, GraphicsUnit.Pixel);//用于给图片定位
g.Dispose();
newFile =file.Substring(0, file.LastIndexOf('\\'));
stringfileName = Path.GetFileName(file);
fileName =fileName.Substring(1,fileName.LastIndexOf("."));
image.Save(newFile + "\\" + fileName+".png");
image.Dispose();
copuImage.Dispose();
//文件己存在
if(File.Exists(file))
{
FileInfo fi = new FileInfo(file);
//判断当前文件属性是否是只读
if(fi.Attributes.ToString().IndexOf("ReadyOnly")>= 0)
{
fi.Attributes =FileAttributes.Normal;
}
//删除文件
File.Delete(file);
}
}
}
}
else
{
if (Directory.GetFileSystemEntries(fileFoldUrl).Length> 0)
{
foreach (stringfile in Directory.GetFiles(fileFoldUrl))
{
stringnewFile = string.Empty;
//给图片加水印图片
System.Drawing.Image image = System.Drawing.Image.FromFile(Directory.GetFiles(muban)[0]);
System.Drawing.Image copuImage = System.Drawing.Image.FromFile(file);
Graphics g = Graphics.FromImage(image);
g.DrawImage(copuImage, newRectangle((image.Width - copuImage.Width) / 2, (image.Height -copuImage.Height) / 2, copuImage.Width, copuImage.Height), 0, 0,copuImage.Width, copuImage.Height, GraphicsUnit.Pixel);
g.Dispose();
if(file.EndsWith("\\"))
{
newFile =file.Substring(0, file.Length - 1);
}
image.Save(newFile + "\\new" + Path.GetFileName(file));
image.Dispose();
copuImage.Dispose();
//文件己存在
if(File.Exists(file))
{
FileInfo fi = new FileInfo(file);
//判断当前文件属性是否是只读
if(fi.Attributes.ToString().IndexOf("ReadyOnly")>= 0)
{
fi.Attributes =FileAttributes.Normal;
}
//删除文件
File.Delete(file);
}
}
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。