这几天折腾一个IOS应用的Icon,一个版本就要生成好几种规格分辨率的Icon,起初使用PS什么的一个一个生成,后来Icon改一版本又得重新生成一次,很是费劲,干脆自己写个程序来干这活得了,思路很简单,先导入一张大图(注意:这张图必须大于等于512*512),然后生成对应像素的小图,最后保存出来。
这里我使用C#来写,主要是考虑到C#里Bitmap类有一个GetThumbnailImage()方法很容易实现图像缩小,经过简单的包装实现图像裁剪的函数如下:
/// <summary>
/// 获取等比例缩放图片的方法
/// </summary>
/// <param name="imgPath">待缩放图片路径</param>
/// <param name="savePath">缩放图片保存路径</param>
/// <param name="format">缩放图片保存的格式</param>
/// <param name="scaling">要保持的宽度或高度</param>
/// <returns></returns>
public bool GetThumbnail(string imgPath, string savePath, ImageFormat format, int scaling)
{
try
{
using (Bitmap myBitmap = new Bitmap(imgPath))//导入图像
{
using (Image myThumbnail = myBitmap.GetThumbnailImage(scaling, scaling, () => { return false; }, IntPtr.Zero))//生成小图
{
myThumbnail.Save(savePath, format);//导出小图
}
}
return true;
}
catch
{
return false;
}
}
别的不多说了,直接上代码:
string path = "";//生成图片保存的路径
private void btnOpen_Click(object sender, EventArgs e)//打开图片按钮
{
openFileDialog1.Filter = "PNG Image|*.png";
openFileDialog1.Title = "打开";
openFileDialog1.Multiselect = false;
openFileDialog1.CheckFileExists = false;
openFileDialog1.FileName = string.Empty;
openFileDialog1.ShowDialog();
txtPath.Text = openFileDialog1.FileName;
path = openFileDialog1.FileName.Substring(0, openFileDialog1.FileName.Length - openFileDialog1.SafeFileName.Length);
path = path + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\";
//txtPath.Text = path;
}
private void btnBuilder_Click(object sender, EventArgs e)//生成按钮
{
if (txtPath.Text.Equals("") || path.Equals(""))
return;
if (Directory.CreateDirectory(path) != null)
{
bool flag = true;
flag = flag && GetThumbnail(txtPath.Text, path + "iTunesArtwork", ImageFormat.Png, 512);//iTunesArtwork
flag = flag && GetThumbnail(txtPath.Text, path + "Icon@2x.png", ImageFormat.Png, 114);//Application Icon for iPhone (retina display)
flag = flag && GetThumbnail(txtPath.Text, path + "Icon-72@2x.png", ImageFormat.Png, 114);//Application Icon for the new iPad (retina display)
flag = flag && GetThumbnail(txtPath.Text, path + "Icon-Small-50@2x.png", ImageFormat.Png, 100);//Settings/Spotlight icon for iPad
flag = flag && GetThumbnail(txtPath.Text, path + "Icon-72.png", ImageFormat.Png, 72);//Application Icon for the iPad
flag = flag && GetThumbnail(txtPath.Text, path + "Icon-Small@2x.png", ImageFormat.Png, 58);//Settings/Spotlight icon for iPhone (retina display)
flag = flag && GetThumbnail(txtPath.Text, path + "Icon.png", ImageFormat.Png, 57);//Application Icon icon for iPhone
flag = flag && GetThumbnail(txtPath.Text, path + "Icon-Small-50.png", ImageFormat.Png, 50);//Settings/Spotlight icon for iPad
flag = flag && GetThumbnail(txtPath.Text, path + "Icon-Small.png", ImageFormat.Png, 29);//Settings/Spotlight icon for iPhone
if (flag)
{
MessageBox.Show("ICON生成完毕!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Diagnostics.Process.Start("Explorer.exe", path);
}
else
{
MessageBox.Show("出错啦!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
运行效果图:
生成的Icon:
程序简陋不过最终实现了目的,欢迎拍砖,最后附上工具源码:http://download.csdn.net/detail/wangqiuyun/4510176