Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Upload and resize images on the fly (resume no-support)

5.00/5 (5 votes)
23 Dec 2013CPOL 15.3K   460  
This Article explains the upload and resizeing of a load of images in one go

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.

C#
// Initializing the Form1.cs
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Enable drag and drop for this form
        // (this can also be applied to any controls)
        this.AllowDrop = true;                                                                    
        // Add event handlers for the drag & drop functionality
        this.DragEnter += new DragEventHandler(Form_DragEnter);
        this.DragDrop += new DragEventHandler(Form_DragDrop);
    }
    // This event occurs when the user drags over the form with 
    // the mouse during a drag drop operation
    void Form_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effect = DragDropEffects.Copy; // Okay
        else
            e.Effect = DragDropEffects.None; // Unknown data, ignore it

    }
    // Occurs when the user releases the mouse over the drop target 
    void Form_DragDrop(object sender, DragEventArgs e)
    {
        // Extract the data from the DataObject-Container into a string list
        string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);

        foreach (string File in FileList)
        {
            lstFiles.Items.Add(File);
        }

    }
    // Occurs when the user has pressed the upload button with resize dimensions for the upload list
    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
            {}  
        }// for ends 
        lstFiles.Items.Clear();
    } //button1_Click ends
}  // Form1 ends
XML
App.config
<?xml version="1.0" encoding="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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)