namespace iconGen
{
public partial class Form1 : Form
{
public Form1(){
InitializeComponent();
var exts = new List<string>(){
".jpg",".png",".gif",".jpeg",".bmp"
};
foreach (var ext in exts)
{
cbl_exts.Items.Add(ext);
cbl_exts.SetItemChecked(cbl_exts.Items.Count-1,true);
}
tb_savepath.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
}
public Dictionary<String, Bitmap> scanDir(string path, List<String> extsVal)
{
Dictionary<String, Bitmap> result = new Dictionary<string, Bitmap>();
var dirs = Directory.GetDirectories(path);
foreach (var dir in dirs){
if (Directory.GetFiles(dir).Where(s => extsVal.Contains(new FileInfo(s).Extension)).Count() > 0)
{
string name = new FileInfo(dir).Name;
string newname = name;
for (var i = 1; result.ContainsKey(newname); i++)
newname = name + "_" + i;
result.Add(newname, createImg(dir, Int32.Parse(tb_width.Text), Int32.Parse(tb_height.Text), Int32.Parse(tb_wh.Text),
cb_auto.Checked, Int32.Parse(tb_padding.Text), extsVal));
}
var res = scanDir(dir, extsVal);
foreach (var key in res.Keys){
string newname = key;
for (var i = 1; result.ContainsKey(newname); i++)
newname = key+ "_" + i;
Bitmap bm;
res.TryGetValue(key, out bm);
result.Add(newname, bm);
bm.Dispose();
}
}
return result;
}
public Bitmap createImg(string path,int imgWidth,int imgHeight,int iconWH,bool isAutoWH,int padding,List<string> exts){
var files = Directory.GetFiles(path).Where(s => exts.Contains(new FileInfo(s).Extension)).ToArray();
var count = files.Length;
if (isAutoWH)
{
for (int i = Math.Min(imgWidth, imgHeight); i > 0; i--)
{
var maxCount = Math.Floor((double)imgWidth / i) * Math.Floor((double)imgHeight / i);
if (maxCount >= count)
{
iconWH = i - padding <= 0 ? i : i - padding;
break;
}
}
}
Bitmap bm = new Bitmap(imgWidth, imgHeight);
using (Graphics g = Graphics.FromImage(bm))
{
for (int i = 0; i < count; i++)
{
var img = Image.FromFile(files[i]);
int x = i % (imgWidth / (iconWH + padding)) * (iconWH + padding);
int y = (int)(Math.Floor((double)(i / (imgWidth / (iconWH + padding)))) * (iconWH + padding));
g.DrawImage(img, x, y, iconWH, iconWH);
img.Dispose();
}
}
return bm;
}
private void btn_scandir_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
tb_scandir.Text = fbd.SelectedPath;
}
private void btn_savepath_Click(object sender, EventArgs e){
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
tb_savepath.Text = fbd.SelectedPath;
}
private void button1_Click(object sender, EventArgs e){
try
{
List<string> extsVal = new List<string>();
foreach (var checkedItem in cbl_exts.CheckedItems)
extsVal.Add(checkedItem as string);
var t = scanDir(tb_scandir.Text, extsVal);
var p = new DirectoryInfo(tb_savepath.Text);
foreach (var key in t.Keys){
Bitmap bm;
t.TryGetValue(key, out bm);
bm.Save(p.FullName + @"\" + key + ".png", ImageFormat.Png);
bm.Dispose();
}
Process.Start("explorer.exe", p.FullName);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void cb_auto_CheckedChanged(object sender, EventArgs e)
{
tb_wh.ReadOnly = cb_auto.Checked;
}
}
}