using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;
namespace photo
{
public partial class Form1 : Form
{
int currentIndex=0;
int maxIndex = 0;
DirectoryInfo dirInfo;
FileInfo[] files;
public string str_NOT_VALID="";
public string str_TYPE_SKIP = "";
public string str_OF = "";
public string str_FILES = "";
public string str_PHOTOS_FOLDER = "";
public string str_NO_PHOTOS = "";
public string str_SELECT_FOLDER = "";
public string str_ERR_NUMBER = "";
public string str_ERR_ALPHA = "";
public string lbl_SELECT = "";
public string lbl_SOURCE = "";
public string bt_SEARCH = "";
public string lbl_EXTENSION = "";
public string bt_SKIP = "";
public string lbl_ABOUT = "";
public string bt_OPEN = "";
public string ch_BANNER = "";
public string lbl_PRE = "";
public string lbl_DEST = "";
public string lbl_SUB = "";
public string lbl_TEXT = "";
public string lbl_OUT_W = "";
public string lbl_OUT_H = "";
public string lbl_ORI_W = "";
public string lbl_ORI_H = "";
public string lbl_MAX = "";
public string bt_OPTIONS = "";
public string bt_CLOSE = "";
public string bt_LANGUAGE = "";
public bool no_photos = false;
public Form1()
{
InitializeComponent();
SetLanguage(1);
englishUSToolStripMenuItem.Checked = true;
this.comboBox1.SelectedIndex=0;
System.Windows.Forms.Cursor.Current = Cursors.Default;
}
public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int NewHeight)
{
Image FullsizeImage = null;
try
{
FullsizeImage = Image.FromFile(OriginalFile);
}
catch
{
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
FullsizeImage =Image.FromStream(thisExe.GetManifestResourceStream("photo.error.jpg"));
}
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
float prop = (float)FullsizeImage.Width / FullsizeImage.Height;
if (FullsizeImage.Width > 640)
{
if (prop < 1.35 && prop > 1.30)
{
if (radio.Checked)
NewWidth = 640;
else if (radio2.Checked)
NewWidth = 800;
else if (radio3.Checked)
NewWidth = 1024;
NewHeight = Convert.ToInt32((float)FullsizeImage.Height / ((float)FullsizeImage.Width / NewWidth));
}
else if (prop == 1)
{
if (radio.Checked)
NewWidth = 640;
else if (radio2.Checked)
NewWidth = 800;
else if (radio3.Checked)
NewWidth = 1024;
NewHeight = NewWidth;
}
else
{
if (radio.Checked)
NewWidth = 480;
else if (radio2.Checked)
NewWidth = 600;
else if (radio3.Checked)
NewWidth = 768;
NewHeight = Convert.ToInt32((float)FullsizeImage.Height / ((float)FullsizeImage.Width / NewWidth));
}
}
else
{
NewWidth = FullsizeImage.Width;
NewHeight = FullsizeImage.Height;
}
Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
foreach (PropertyItem e in FullsizeImage.PropertyItems)
{
NewImage.SetPropertyItem(e);
}
FullsizeImage.Dispose();
InsertBannerAndText(NewImage, NewFile);
}
private void InsertBannerAndText(Image imagem, string filepath)
{
Image imgPhoto = imagem;
//imgPhoto.Save("c:/teste.jpg");
int phWidth = imagem.Width;
int phHeight = imagem.Height;
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
Image imgWatermark;
imgWatermark = Image.FromStream(thisExe.GetManifestResourceStream("photo.watermark.GIF"));
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.DrawImage(
imgPhoto,
new Rectangle(0, 0, phWidth, phHeight),
0,
0,
phWidth,
phHeight,
GraphicsUnit.Pixel);
Bitmap bmWatermark = bmPhoto;
//bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grWatermark = Graphics.FromImage(bmWatermark);
ImageAttributes imageAttributes = new ImageAttributes();
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.5f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
if (this.chcBanner.Checked == true)
{
int xPosOfWm = 0;
int yPosOfWm = Convert.ToInt32(0.8*Convert.ToInt32(imagem.Height));
int x2 = Convert.ToInt32(imagem.Width);
int y2 = Convert.ToInt32(imagem.Height) - yPosOfWm;
Rectangle space = new Rectangle(xPosOfWm, yPosOfWm,x2 ,y2);
grWatermark.DrawImage(imgWatermark,space, 0, 0, x2, y2, GraphicsUnit.Pixel, imageAttributes);
}
Font crFont = null;
SizeF crSize = new SizeF();
if (txtText.Text.Length > 0)
{
float size_def = Convert.ToInt32(0.0375 * Convert.ToInt32(imagem.Height));
crFont = new Font("Comic Sans MS", size_def, FontStyle.Bold);
crSize = grWatermark.MeasureString(txtText.Text, crFont);
int prop_min = Convert.ToInt32(0.0580 * Convert.ToInt32(imagem.Height));
int prop_max = Convert.ToInt32(0.0625 * Convert.ToInt32(imagem.Height));
while (crSize.Height > prop_max)
{
size_def--;
crFont = new Font("Comic Sans MS", size_def, FontStyle.Bold);
crSize = grWatermark.MeasureString(txtText.Text, crFont);
}
while (crSize.Height < prop_min)
{
size_def++;
crFont = new Font("Comic Sans MS", size_def, FontStyle.Bold);
crSize = grWatermark.MeasureString(txtText.Text, crFont);
}
}
int rec1,rec2,rec3,rec4;
rec1 = Convert.ToInt32(0.04 * Convert.ToInt32(imagem.Width));
rec2 = Convert.ToInt32(0.83 * Convert.ToInt32(imagem.Height));
rec3 = Convert.ToInt32(Convert.ToInt32(imagem.Width) - rec1);
rec4 = Convert.ToInt32(Convert.ToInt32(imagem.Height) - rec2);
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(255, 255, 255, 255));
grWatermark.DrawString(txtText.Text, //string of text
crFont, //font
semiTransBrush, //Brush
new Rectangle(rec1,rec2,rec3,rec4)); //Text alignment
imgPhoto = bmWatermark;
foreach (PropertyItem e in imagem.PropertyItems)
{
imgPhoto.SetPropertyItem(e);
}
grPhoto.Dispose();
grWatermark.Dispose();
imgPhoto.Save(filepath, ImageFormat.Jpeg);
imgPhoto.Dispose();
imgWatermark.Dispose();
bmPhoto.Dispose();
}
private void Draw()
{
int problema = 0;
txtFolder.Text = files[currentIndex].DirectoryName + "\\Photo_Resize_Save" + "\\";
int tam_w = 0, tam_h = 0;
button3.Enabled = true;
button4.Enabled = true;
System.Drawing.Image FullsizeImage=null;
try
{
FullsizeImage = System.Drawing.Image.FromFile(files[currentIndex].FullName);
}
catch
{
problema = 1;
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
FullsizeImage = Image.FromStream(thisExe.GetManifestResourceStream("photo.error.jpg"));
}
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
float prop = (float) FullsizeImage.Width / FullsizeImage.Height;
if (FullsizeImage.Width > 640)
{
if (prop < 1.35 && prop > 1.3)
{
if (radio.Checked)
tam_w = 640;
else if (radio2.Checked)
tam_w = 800;
else if (radio3.Checked)
tam_w = 1024;
tam_h = Convert.ToInt32((float)FullsizeImage.Height / ((float)FullsizeImage.Width / tam_w));
}
else if (prop == 1)
{
if (radio.Checked)
tam_w = 640;
else if (radio2.Checked)
tam_w = 800;
else if (radio3.Checked)
tam_w = 1024;
tam_h = tam_w;
}
else
{
if (radio.Checked)
tam_w = 640;
else if (radio2.Checked)
tam_w = 800;
else if (radio3.Checked)
tam_w = 1024;
tam_h = Convert.ToInt32((float)FullsizeImage.Height / ((float)FullsizeImage.Width / tam_w));
}
}
else
{
tam_w = FullsizeImage.Width;
tam_h = FullsizeImage.Height;
}
this.nHei.Text = tam_h.ToString();
this.nWid.Text = tam_w.ToString();
this.oHei.Text = FullsizeImage.Height.ToString();
this.oWid.Text = FullsizeImage.Width.ToString();
tam_w = tam_w / 2;
if (tam_w == 0)
{
tam_w = 1;
}
tam_h = tam_h / 2;
if (tam_h == 0)
{
tam_h = 1;
}
System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(tam_w, tam_h, null, IntPtr.Zero);
pictureBox1.Image = NewImage;
if (problema == 1)
{
MessageBox.Show(str_NOT_VALID);
}
// NewImage.Dispose();
//FullsizeImage.Dispose();
FullsizeImage = null;
//NewImage = null;
}
private void button2_Click(object sender, EventArgs e)
{
if (txtSource.Text != "")
{
try
{
dirInfo = new DirectoryInfo(txtSource.Text);
System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;
if (subfolders.Checked == true)
{
files = dirInfo.GetFiles(this.comboBox1.SelectedItem.ToString(), SearchOption.AllDirectories);
}
else
{
files = dirInfo.GetFiles(this.comboBox1.SelectedItem.ToString());
}
System.Windows.Forms.Cursor.Current = Cursors.Default;
currentIndex = 0;
maxIndex = files.Length-1;
if (files.Length > 0)
{
txtFolder.Text = files[currentIndex].DirectoryName + "\\Photo_Resize_Save" + "\\";
no_photos = false;
Draw();
status.Text = str_TYPE_SKIP;
status2.Text = "1 " + str_OF + " " + (maxIndex + 1) + " " + str_FILES + "!";
MessageBox.Show((maxIndex + 1) + str_PHOTOS_FOLDER, "Photo Resize");
}
else
{
MessageBox.Show(str_NO_PHOTOS,"Photo Resize");
no_photos = true;
status.Text = str_NO_PHOTOS;
status2.Text = "";
pictureBox1.Image = null;
}
}
catch
{
files = null;
MessageBox.Show("Error! Access Denied!", "Photo Resize", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
}
private void fdo_HelpRequest(object sender, EventArgs e)
{
}
private void ChangePicture()
{
if (currentIndex <= maxIndex)
{
ResizeImage(files[currentIndex].FullName, files[currentIndex].DirectoryName + "\\Photo_Resize_Save" + "\\"+nPre.Text+"_" + files[currentIndex].Name, Convert.ToInt32(oWid.Text), Convert.ToInt32(oHei.Text));
}
else
{
status2.Text = "";
status.Text = str_SELECT_FOLDER;
}
}
private void button1_Click(object sender, EventArgs e)
{
fdo.ShowDialog();
if (fdo.SelectedPath.Length > 0)
{
txtSource.Text = fdo.SelectedPath;
txtFolder.Text = fdo.SelectedPath + "\\" + "Photo_Resize_Save";
btOpen.Enabled = true;
currentIndex = 0;
}
}
private void button3_Click(object sender, EventArgs e)
{
txtFolder.Text = files[currentIndex].DirectoryName + "\\Photo_Resize_Save" + "\\";
if (txtFolder.Text.Length > 0 && !Directory.Exists(txtFolder.Text))
{
Directory.CreateDirectory(txtFolder.Text);
}
ChangePicture();
ChangePictureIndex();
if (currentIndex <= maxIndex)
{
Draw();
}
else
{
status2.Text = "";
status.Text = str_SELECT_FOLDER;
button3.Enabled = false;
button4.Enabled = false;
}
txtText.Text = "";
}
private void ChangePictureIndex()
{
currentIndex++;
status2.Text = (currentIndex+1)+" " + str_OF + " " + (maxIndex + 1) + " " + str_FILES + "!";
}
private void button4_Click(object sender, EventArgs e)
{
ChangePictureIndex();
if (currentIndex <= maxIndex)
{
Draw();
}
else
{
status2.Text = "";
status.Text = str_SELECT_FOLDER;
button3.Enabled = false;
button4.Enabled = false;
}
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
}
private void About()
{
MessageBox.Show("Renan Duarte\nv 1.7.1 - 24/Oct/2007\nrenan@rodriguesduarte.net", "Photo Resize", MessageBoxButtons.OK, MessageBoxIcon.Information ,MessageBoxDefaultButton.Button1);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
About();
}
private void button5_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(txtSource.Text);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
if (objAlphaNumericPattern.IsMatch(nPre.Text))
{
MessageBox.Show(str_ERR_ALPHA, "Photo Resize");
nPre.Text = "small";
}
}
private void SetLanguage(int choice)
{
if (choice == 1)
{
//English
str_NOT_VALID = "This is not a valid image!";
str_TYPE_SKIP = "Type a text or skip to the next file!";
str_OF = "of";
str_FILES = "files";
str_PHOTOS_FOLDER = " photo(s) in this folder!";
str_NO_PHOTOS = "No files in this folder. Select another folder!";
str_SELECT_FOLDER = "Select a folder to start.";
str_ERR_NUMBER = "Only numbers!";
str_ERR_ALPHA = "Only alphanumeric!";
lbl_SELECT = "Select";
lbl_SOURCE = "Source Folder";
bt_SEARCH = "Search";
lbl_EXTENSION = "Extension";
bt_SKIP = "Skip";
lbl_ABOUT = "About";
bt_OPEN = "Show Folder";
ch_BANNER = "Add Banner";
lbl_PRE = "Prefix:";
lbl_DEST = "Destination Folder";
lbl_SUB = "Search Subfolders?";
lbl_TEXT = "Subtitle:";
lbl_OUT_W = "Output Width:";
lbl_OUT_H = "Output Height:";
lbl_ORI_W = "Original Width:";
lbl_ORI_H = "Original Height:";
lbl_MAX = "Maximum Size:";
bt_OPTIONS = "Options";
bt_CLOSE = "Close";
bt_LANGUAGE = "Language";
}
else if (choice == 2)
{
//Germain
str_NOT_VALID = "Keine g�ltige Bilddatei!";
str_TYPE_SKIP = "Schreibe ein Text oder springe zur n�chsten Datei!";
str_OF = "von";
str_FILES = "Dateien";
str_PHOTOS_FOLDER = " Bild(er) in diesem Ordner!";
str_NO_PHOTOS = "Keine Dateien in diesem Ordner. W�hle einen anderen Ordner!";
str_SELECT_FOLDER = "W�hle einen Ordner um zu starten.";
str_ERR_NUMBER = "Es sind nur nummerische Werte erlaubt!";
str_ERR_ALPHA = "Es sind nur alphanumerische Werte erlaubt!";
lbl_SELECT = "Auswahl";
lbl_SOURCE = "Ordner";
bt_SEARCH = "Suche";
lbl_EXTENSION = "Dateiendung";
bt_SKIP = "Springen";
lbl_ABOUT = "�ber";
bt_OPEN = "Ordner anzeigen";
ch_BANNER = "Banner hinzuf�gen";
lbl_PRE = "Pr�fix:";
lbl_DEST = "Zielordner";
lbl_SUB = "Unterordner mit einbeziehen?";
lbl_TEXT = "Untertitel:";
lbl_OUT_W = "Ausgabebreite:";
lbl_OUT_H = "Ausgabeh�he:";
lbl_ORI_W = "Originalbreite:";
lbl_ORI_H = "Originalh�he:";
lbl_MAX = "Maximale Gr�sse:";
bt_OPTIONS = "Optionen";
bt_CLOSE = "Schliessen";
bt_LANGUAGE = "Sprache";
}
else
{
//Portugu�s
str_NOT_VALID = "Esta imagem n�o � v�lida!";
str_TYPE_SKIP = "Digite um texto ou pule para pr�xima foto!";
str_OF = "de";
str_FILES = "arquivos";
str_PHOTOS_FOLDER = " arquivo(s) nesta pasta!";
str_NO_PHOTOS = "N�o h� arquivos nesta pasta. Selecione outra!";
str_SELECT_FOLDER = "Selecione uma pasta para come�ar.";
str_ERR_NUMBER = "Somente n�meros!";
str_ERR_ALPHA = "Somente caract�res alfanum�ricos!";
lbl_SELECT = "Selecione";
lbl_SOURCE = "Pasta Base";
bt_SEARCH = "Buscar";
lbl_EXTENSION = "Extens�o";
bt_SKIP = "Pular";
lbl_ABOUT = "Sobre";
lbl_MAX = "Tamanho M�ximo:";
bt_OPEN = "Mostrar Pasta";
ch_BANNER = "Adicionar Banner (Rodap�)";
lbl_PRE = "Prefixo:";
lbl_DEST = "Pasta Destino";
lbl_SUB = "Incluir Subdiret�rios?";
lbl_TEXT = "Legenda:";
lbl_OUT_W = "Novo Comprimento:";
lbl_OUT_H = "Nova Altura:";
lbl_ORI_W = "Comprimento Original:";
lbl_ORI_H = "Altura Original:";
bt_OPTIONS = "Op��es";
bt_CLOSE = "Sair";
bt_LANGUAGE = "Idioma";
}
this.languageToolStripMenuItem.Text = bt_LANGUAGE;
this.optionsToolStripMenuItem.Text = bt_OPTIONS;
this.closeToolStripMenuItem.Text = bt_CLOSE;
this.lblouth.Text = lbl_OUT_H;
this.lbloutw.Text = lbl_OUT_W;
this.lblWidth.Text = lbl_ORI_W;
this.lblHeight.Text = lbl_ORI_H;
this.chcBanner.Text = ch_BANNER;
this.subfolders.Text = lbl_SUB;
this.label4.Text = lbl_PRE;
this.label5.Text = lbl_DEST;
this.button1.Text = lbl_SELECT;
this.label1.Text = lbl_SOURCE;
this.button2.Text = bt_SEARCH;
this.label2.Text = lbl_EXTENSION;
this.lblMax.Text = lbl_MAX;
this.label6.Text = lbl_TEXT;
this.button4.Text = bt_SKIP;
aboutToolStripMenuItem1.Text = lbl_ABOUT;
if (maxIndex > 0)
{
this.status.Text = str_TYPE_SKIP;
}
else if (no_photos ==true)
{
this.status.Text = str_NO_PHOTOS;
}
else
{
this.status.Text = str_SELECT_FOLDER;
}
if (this.status2.Text.Length > 0)
{
status2.Text = (currentIndex + 1) + " " + str_OF + " " + (maxIndex + 1) + " " + str_FILES + "!";
}
this.aboutToolStripMenuItem.Text = lbl_ABOUT;
this.btOpen.Text = bt_OPEN;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txtFolder_TextChanged(object sender, EventArgs e)
{
}
private void englishUSToolStripMenuItem_Click(object sender, EventArgs e)
{
ChangeLanguageButtons(1);
}
private void portugueseToolStripMenuItem_Click(object sender, EventArgs e)
{
ChangeLanguageButtons(3);
}
private void germainToolStripMenuItem_Click(object sender, EventArgs e)
{
ChangeLanguageButtons(2);
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void ChangeLanguageButtons(int number)
{
if (number == 1)
{
englishToolStripMenuItem.Checked = true;
englishUSToolStripMenuItem.Checked = true;
portugu�sToolStripMenuItem.Checked = false;
portugueseToolStripMenuItem.Checked = false;
deutschToolStripMenuItem.Checked = false;
germainToolStripMenuItem.Checked = false;
SetLanguage(1);
this.Refresh();
}
else if (number == 2)
{
englishToolStripMenuItem.Checked = false;
englishUSToolStripMenuItem.Checked = false;
portugu�sToolStripMenuItem.Checked = false;
portugueseToolStripMenuItem.Checked = false;
deutschToolStripMenuItem.Checked = true;
germainToolStripMenuItem.Checked = true;
SetLanguage(2);
this.Refresh();
}
else
{
englishToolStripMenuItem.Checked = false;
englishUSToolStripMenuItem.Checked = false;
portugu�sToolStripMenuItem.Checked = true;
portugueseToolStripMenuItem.Checked = true;
deutschToolStripMenuItem.Checked = false;
germainToolStripMenuItem.Checked = false;
SetLanguage(3);
this.Refresh();
}
}
private void englishToolStripMenuItem_Click(object sender, EventArgs e)
{
ChangeLanguageButtons(1);
}
private void portugu�sToolStripMenuItem_Click(object sender, EventArgs e)
{
ChangeLanguageButtons(3);
}
private void deutschToolStripMenuItem_Click(object sender, EventArgs e)
{
ChangeLanguageButtons(2);
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
About();
}
}
}