仏陀ギルのソースコード




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Collections;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;


namespace bultutaGiru
{
public partial class Form1 : Form
{
Graphics g;
long fileCount;
Form2 fm;
public Form1()
{
InitializeComponent();
}


public void GetSubfolders(
string folderName, ref ArrayList subFolders)
{
try
{
foreach (string folder in
System.IO.Directory.GetDirectories(folderName))
{
//リストに追加
subFolders.Add(folder);
//再帰的にサブフォルダを取得する
GetSubfolders(folder, ref subFolders);
}
}
catch (ArgumentException)
{
MessageBox.Show("フォルダが見つかりません。
変換元フォルダを確認してください");
panel1.Enabled = true;
}
}
public void UnZipFile()
{
fileCount = 0;
fm.progressBar1.Maximum = 100;
ArrayList subFolders = new ArrayList();
GetSubfolders(folderBrowserDialog1.SelectedPath, ref subFolders);
subFolders.Add(folderBrowserDialog1.SelectedPath);
foreach (string s in subFolders)
{
string[] files = System.IO.Directory.GetFiles(
s, "*", System.IO.SearchOption.AllDirectories);
foreach (string f in files)
{
UnZip(f);
if (fm.canceled)
{
return;
}
fileCount++;
}
}
}
public void GetFile()
{
g = fm.pictureBox1.CreateGraphics();
fm.progressBar1.Maximum = (int)fileCount;
ArrayList subFolders = new ArrayList();
GetSubfolders(folderBrowserDialog1.SelectedPath, ref subFolders);
subFolders.Add(folderBrowserDialog1.SelectedPath);
foreach (string s in subFolders)
{
string[] files = System.IO.Directory.GetFiles(
s, "*", System.IO.SearchOption.AllDirectories);
foreach (string f in files)
{
fm.textInfo.Text = "画像処理中:" + Path.GetFileName(f);
ImageScale(f);
if (fm.progressBar1.Value < fm.progressBar1.Maximum)
{
fm.progressBar1.Value++;
}
Application.DoEvents();
if (fm.canceled)
{
g.Dispose();
return;
}
}
}
g.Dispose();
}

public void ImageScale(string f)
{
switch (System.IO.Path.GetExtension(f))
{
case ".JPG":
case ".JPEG":
case ".PNG":
case ".jpg":
case ".jpeg":
case ".png":
break;
default:
return;
}
Bitmap img = new Bitmap(f);

string outFolder = folderBrowserDialog2.SelectedPath;
string inFolder = folderBrowserDialog1.SelectedPath;
int flag = 0;
if (radioButton2.Checked)
{
flag = 1;
}
if (radioButton3.Checked)
{
flag = 2;
}

for (int i = 0; i < 2; i++)
{
float sizeX = (float)numericUpDownX.Value;
float sizeY = (float)numericUpDownY.Value;
float imgX = img.Width;
float imgY = img.Height;
float scle = 0;
string fn;
if (flag == 1 || (flag == 2 && imgX > imgY))
{
imgX /= 2;
}
sizeX = sizeX / imgX;
sizeY = sizeY / imgY;
if (sizeX < sizeY)
{
scle = sizeX;
}
else
{
scle = sizeY;
}
Rectangle rect =
new Rectangle((int)(i * imgX), 0, (int)imgX, (int)imgY);
sizeX = scle * imgX;
sizeY = scle * imgY;
Rectangle rectOut =
new Rectangle(0, 0, (int)(sizeX * 200 / imgX),
(int)(sizeY * 200 / imgX));

g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(img, rectOut, rect, GraphicsUnit.Pixel);


rectOut =
new Rectangle(0, 0, (int)(sizeX), (int)(sizeY));

fn = outFolder + f.Substring(inFolder.Length);

string n;
n = System.IO.Path.GetFileNameWithoutExtension(fn);
if (flag == 1 || (flag == 2 && imgX > imgY))
{
if (i == 0)
{
n = n + "_2";
}
else
{
n = n + "_1";
}
}
n = n + ".jpeg";//System.IO.Path.GetExtension(fn);
fn = System.IO.Path.GetDirectoryName(fn) + "\\" + n;
//フォルダ作成
System.IO.Directory.CreateDirectory(
System.IO.Path.GetDirectoryName(fn));


Bitmap outBmp = new Bitmap((int)sizeX, (int)sizeY);
Graphics go = Graphics.FromImage(outBmp);
go.DrawImage(img, rectOut, rect, GraphicsUnit.Pixel);
go.Dispose();
outBmp.Save(fn, System.Drawing.Imaging.ImageFormat.Jpeg);
outBmp.Dispose();
if (!(flag == 1 || (flag == 2 && imgX > imgY)))
{
break;
}
}
img.Dispose();
}
public void UnZip(string f)
{
if (System.IO.Path.GetExtension(f) != ".zip"
&& System.IO.Path.GetExtension(f) != ".ZIP")
{
return;
}
string inFolder = folderBrowserDialog1.SelectedPath;
using (ZipInputStream s = new ZipInputStream(File.OpenRead(f)))
{


ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
fm.textInfo.Text = "ZIP解凍中:" + Path.GetFileName(f);
Application.DoEvents();
if (fm.progressBar1.Value < fm.progressBar1.Maximum)
{
fm.progressBar1.Value++;
}
if (fm.canceled)
{
return;
}
Console.WriteLine(theEntry.Name);


string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(inFolder + "\\" + theEntry.Name);


// create directory
if (directoryName.Length > 0)
{
Directory.CreateDirectory(inFolder + "\\" + directoryName);
}


if (fileName != String.Empty)
{
using (FileStream streamWriter
= File.Create(inFolder + "\\" + theEntry.Name))
{


int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
switch (System.IO.Path.GetExtension(theEntry.Name))
{
case ".jpeg":
case ".jpg":
case ".JPEG":
case ".JPG":
fileCount++;
break;
case ".zip":
case ".ZIP":
UnZip(inFolder + "\\" + theEntry.Name);
System.IO.File.Delete(inFolder + "\\" + theEntry.Name);
break;
}
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
panel1.Enabled = false;
fm = new Form2();
this.AddOwnedForm(fm);
fm.Show(this);
fm.progressBar1.Value = 0;
UnZipFile();
if (!fm.canceled)
{
fm.progressBar1.Value = 0;
GetFile();
}
panel1.Enabled = true;
fm.progressBar1.Value = 100;
fm.Close();
}
catch (Exception ex)
{
MessageBox.Show("エラーが発生しました:" + ex.Message);
panel1.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
Properties.Settings.Default.SrcPath = folderBrowserDialog1.SelectedPath;
}


private void button3_Click(object sender, EventArgs e)
{
folderBrowserDialog2.ShowDialog();
Properties.Settings.Default.DestPath
= folderBrowserDialog2.SelectedPath;
}


private void Form1_Load(object sender, EventArgs e)
{
folderBrowserDialog1.SelectedPath = Properties.Settings.Default.SrcPath;
folderBrowserDialog2.SelectedPath = Properties.Settings.Default.DestPath;
if (Properties.Settings.Default.giru == 0)
{
radioButton1.Checked = true;
}
if (Properties.Settings.Default.giru == 1)
{
radioButton2.Checked = true;
}
if (Properties.Settings.Default.giru == 2)
{
radioButton3.Checked = true;
}
numericUpDownX.Value = Properties.Settings.Default.x;
numericUpDownY.Value = Properties.Settings.Default.y;
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
int flag = 0;
if (radioButton2.Checked)
{
flag = 1;
}
if (radioButton3.Checked)
{
flag = 2;
}
Properties.Settings.Default.giru = flag;
Properties.Settings.Default.x = (long)numericUpDownX.Value;
Properties.Settings.Default.y = (long)numericUpDownY.Value;
Properties.Settings.Default.Save();
}


private void linkLabel1_LinkClicked(
object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.asahi-net.or.jp/~vi8r-kizm/");
}

}
}













スポンサーサイト

テーマ : プログラミング - ジャンル : コンピュータ

コメント

コメントの投稿

管理者にだけ表示を許可する

トラックバック


この記事にトラックバックする(FC2ブログユーザー)

プロフィール

TEA(てぃあ)

Author:TEA(てぃあ)
親サイト:
フリーのゲームがダウンロードできます
主にシューティング系

最新記事
最新コメント
カテゴリ
PSP Vita
カウンター
ジャスティスレギオン 無料ダウンロード
インディーズPSPVitaゲーム
リンクや作った物
検索フォーム
RSSリンクの表示