Friday, September 30, 2011

Uploading a picture at runtime

I was trying an addressbook application, and then it struck me... wouldn't it be awesome, if we could add pictures to each entry, so one can also look at a contact? The following is the coding for uploading a picture into a PictureBox using open file dialog control. For any doubts, or suggestions, please mail me at nardz07@gmail.com.

Here's the basic cs code - explained :




Firstly all the packages are added:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

We select a picturebox and we place it on our form. Say we add a link label, and to its click event, we try to add a picture from a file at run time onto the picture box.

the event will look something like this...

 
Now, we must add a property that allows the user to extract files at runtime. This is the OpenFileDialog control. Select and drag the control onto the form, and set the name and other properties as you see fit.

On editing the properties...


Now we enter the coding.

In any open file dialog, there are three important sectors that we must define:

1. Title

This is the title of the dialog box that opens...

openFD.Title = "Insert an Image";

2.  InitialDirectory

The initial directory that the open file dialog must look at by default...
openFD.InitialDirectory = "C:";

3. Filter
This is the place where we define what all types of files should be allowed to be opened by this file dialog...

openFD.Filter = "JPEG|*.jpg|GIF|*.gif|BITMAPS|*.bmp";



Here, what we have done is given titles, and also the actual extensions they will correspond to...
Now, by default, some initial savename must be given...

So we add the following line:

openFD.FileName = "";

However, we still haven't inserted a new image. To place a selected image into the picture box, you have to get the file name that the user selected. You can add a string variable to your code for this:

string Chosen_File = "";

You then access the FileName property of openFD. Like this:

Chosen_File = openFD.FileName;

The file name will then be in the variable we've called Chosen_File. To place a new image into the picture box you have on the form, you need the Image property:

pictureBox1.Image

To place your chosen file into the Image property, you need this:

pictureBox1.Image = Image.FromFile(Chosen_File);

So after the equals sign, you can use the Image object. This has a method called FromFile( ). In between the round brackets of this method, you type the name of the image file. For us, this image file is stored in our Chosen_File variable.

We have passed all the parameters for the file dialog, but not actually called it yet. So, we call it here:

openFD.ShowDialog( );

Now, on adding all this code, our event will look somewhat like this:


So, we have successfully finished our code. but, say the user presses the cancel button, it will throw an error as no value has been stored in the Chosen_File variable.

So, we put in this code, to ensure our dialog recognises the cancel statement:


if (openFD.ShowDialog() != DialogResult.Cancel)
  {
    Chosen_File = openFD.FileName;
    pictureBox1.Image = Image.FromFile(Chosen_File);
  }

This checks the dialog result - which must be one of two - open or cancel... and if it isn't cancel, filename is stored in the Chosen_File variable. Or else, no action takes place, and the cancel event can take place without throwing any errors.

Hope this has been of some help. Please mail me your suggestions at the aforementioned email ID.
Thank You.

2 comments:

  1. If your blogging purpose is really to help others exactly like u, you can provide the download links of your scrap work which would speak louder than your posts ! :)

    ReplyDelete
  2. Its really good and helpful to all the beginners like me

    ReplyDelete