Introduction
Upload and re-size images with dimensions read from textbox inputs, while using drag and drop features for listing up files to upload.
Background
Used the following for ftp upload http://dotnet-snippets.de/snippet/ftp-file-upload-mit-buffer/886.
Using the code
The code is available for download and also the Blocks of code should be self explanatory. The ftpHelper class is available from the link above.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.AllowDrop = true;
this.DragEnter += new DragEventHandler(Form_DragEnter);
this.DragDrop += new DragEventHandler(Form_DragDrop);
}
void Form_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
void Form_DragDrop(object sender, DragEventArgs e)
{
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (string File in FileList)
{
lstFiles.Items.Add(File);
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (string File in lstFiles.Items)
{
Image imgToResize = Image.FromFile(File); Size size =
new Size(Convert.ToInt32(txtWidth.Text), Convert.ToInt32(txtHeight.Text));
Bitmap b = new Bitmap(size.Width, size.Height);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, new Rectangle(0, 0, Convert.ToInt32(txtWidth.Text),
Convert.ToInt32(txtHeight.Text)));
System.Configuration.AppSettingsReader app = new System.Configuration.AppSettingsReader();
b.Save(app.GetValue("imagedirectory", "".GetType()).ToString() + "\\" +
System.IO.Path.GetFileNameWithoutExtension(File)+"1"+
System.IO.Path.GetExtension(File));
string strFtp = app.GetValue("ftp", "".GetType()).ToString();
string strUsr = app.GetValue("user", "".GetType()).ToString();
string strPwd = app.GetValue("pwd", "".GetType()).ToString();
string strPort = app.GetValue("port", "".GetType()).ToString();
string strDir = app.GetValue("imagedirectory", "".GetType()).ToString();
try
{
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(strFtp +
Guid.NewGuid().ToString() + ".jpg");
req.Credentials = new NetworkCredential(strUsr, strPwd);
FtpHelper.UploadFile(req, strDir + "\\" +
System.IO.Path.GetFileNameWithoutExtension(File) +
"1" + System.IO.Path.GetExtension(File), -1);
}
catch
{}
}
lstFiles.Items.Clear();
}
}
App.config
="1.0"="utf-8"
<configuration>
<appSettings>
<add key="ftp" value="ftp://127.0.0.1/"/>
<add key="port" value="21"/>
<add key="user" value="test"/>
<add key="pwd" value="123"/>
<add key="imagedirectory" value="c:/tmpimage/tmp"/>
</appSettings>
</configuration>
Points of Interest
Used the Filezilla FTP server and the ftpHelper class from the link, and arranged the settings in the app.config for dynamic settings.
History
There might be more to come if planned a better solution containing resume of failed uploads.