以下是引用有容就大在2013-4-24 11:29:59的发言:
搞的这么专业。。。
搞的这么专业。。。
如此简陋的东西你这样说叫我情何以堪!

授人以渔,不授人以鱼。

程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using myTools;
namespace party_pics
{
class Program
{
static void Main(String[] args)
{
Int32 width, height;
if ((args.Length == 2) && Int32.TryParse(args[0], out width) && Int32.TryParse(args[1], out height) && (width > 0) && (height > 0))
{
Process(width, height);
}
}
static void Process(Int32 width, Int32 height)
{
if (Directory.Exists("Target") || (Directory.CreateDirectory("Target") != null))
{
String[] files = Directory.GetFiles("Source");
foreach (String filename in files)
{
String new_filename = filename.Substring(filename.LastIndexOf(@"\") + 1);
new_filename = new_filename.Substring(0, new_filename.LastIndexOf(".")) + ".PNG";
Console.Write("{0}...", new_filename);
Console.WriteLine("{0}", (mytools.Convert_Image(filename, @"Target\" + new_filename, width, height) ? "success" : "failure"));
}
}
else
{
Console.WriteLine("Subdirectory \"Target\" create failure!");
}
}
}
}
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
namespace myTools
{
public static partial class mytools
{
//------------------------------------------------------------
// 功能:将图像转换为指定尺寸的图像,并在目标区域内居中
// 参数:sourceImageFileName -- 源文件名
// targetImageFileName -- 目标文件名
// width -- 目标区域像素宽度
// height -- 目标区域像素高度
// 备注:转换图像为96DPI、白色背景、24位色的PNG格式
//------------------------------------------------------------
public static Boolean Convert_Image(String sourceImageFileName, String targetImageFileName, Int32 width, Int32 height)
{
Boolean success = false;
if (sourceImageFileName.ToUpper().EndsWith(".JPG") || sourceImageFileName.ToUpper().EndsWith(".PNG"))
{
Int32 top = 0; // 新图像在目标区域中的顶坐标
Int32 left = 0; // 新图像在目标区域中的左坐标
Image sourceImage = Image.FromFile(sourceImageFileName);
Bitmap newBitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newBitmap.SetResolution(96, 96);
if (sourceImage.Width > sourceImage.Height)
{
height = sourceImage.Height * width / sourceImage.Width;
top = (newBitmap.Height - height) / 2;
}
else
{
width = sourceImage.Width * height / sourceImage.Height;
left = (newBitmap.Width - width) / 2;
}
Rectangle rect = new Rectangle(left, top, width, height);
Graphics theGraphics = Graphics.FromImage(newBitmap);
theGraphics.Clear(System.Drawing.Color.White);
theGraphics.DrawImage(sourceImage, rect);
newBitmap.Save(targetImageFileName, System.Drawing.Imaging.ImageFormat.Png);
sourceImage.Dispose();
newBitmap.Dispose();
theGraphics.Dispose();
success = true;
}
return success;
}
}
}
