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

Accessing image files from project resources


C# C# 
Ratings:
0 Comment(s)
 2218  View(s)
Rate this:
Step 1:- Open Visual Studio

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



Step 3:- Add 1 No PictureBox control from Toolbox on your blank form


Step 4:- Add 3 Nos Button Control from toolbox on your form. (you can add less or more buttons as you desired)




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,



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)



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.




Now time to code our app

Right click on your form & select view code,



Your code window will open

Step 7: Add namespace in your code for pick image resources in each button, your name space will look like this

using SinglePictureBox.Properties;


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;
        }
    }
}




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.



Thanks,

Don't want to miss updates? Please click the below button!

Follow MindStick