blog

Home / DeveloperSection / Blogs / Accessing image files from project resources

Accessing image files from project resources

Vilas Shende4085 06-Oct-2013
Step 1:- Open Visual Studio

Step 2:- Create New Project, Rename Project if required (I have renamed it

as SinglePictureBox)

Accessing image files from project resources


Step 3:- Add 1 No PictureBox control from Toolbox on your blank form
Accessing image files from project resources


Step 4:- Add 3 Nos Button Control from toolbox on your form. (you can add

less or more buttons as you desired)

Accessing image files from project resources



Our GUI is ready now, before adding any images please rename their file


names like Img1, Img2 & Img3, so that it will easy to add & pick names in


your code window, I have renamed 3 images as mentioned above.



Step 5:- Right click on picture box, go to property,

Accessing image files from project resources


Select ‘Image’ property,  select ‘Import’ option, you will get brows for

adding image files, now select any 3 Nos pictures (In this project I have

added 3 Nos Button Controls so we need only 3 Nos images also, you can

add less or more as you desired)

Accessing image files from project resources


Step 6:- After addition of image, select option as ‘none’ & press Ok, so that

when you run your project then by default Picture box will show as blank.

Accessing image files from project resources



Now time to code our app

Right click on your form & select view code,

Accessing image files from project resources


Your code window will open
Accessing image files from project resources

Step 7: Add namespace in your code for pick image resources in each

button, your name space will look like this

using SinglePictureBox.Properties;

Accessing image files from project resources

In above code, SinglePictureBox is project name as I renamed it while

creating new project, if you have given any different name to you project

then you have to use that name only instead on SinglePictureBox

Now come back on your GUL (form Design)

Step 8:- Double click on Button1 & code this in click event

private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Resources.Img1;
        }
In above code, Img1 will display after button click.

Again double click on Button2 & code this in click event

{
            pictureBox1.Image = Resources.Img2;
        }

In above code, Img2 will display after button click.


Once again double click on Button3 & code this in click event.

{
            pictureBox1.Image = Resources.Img3;
        }

In above code, Img3 will display after button click.

Your coding is done & your final code will look like this.

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 SinglePictureBox.Properties;
namespace SinglePictureBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Resources.Img1;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Resources.Img2;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Resources.Img3;
        }
    }
}

Accessing image files from project resources



Now run you code & check it works? Press all (3 Nos) buttons one by one

you will get all image one by one in single picture box.

Accessing image files from project resources


Thanks,

Updated 18-Sep-2014
I am a Junior Level Programmer, Software Developer as well as Experienced Virtual Assistant who supports to businesses & individuals with achieving their professional goals.

Leave Comment

Comments

Liked By