13

i Have some images in resources folder in my project but i want to change the picture box from these resource files of the project

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mobin
  • 4,870
  • 15
  • 45
  • 51
  • Check out: http://stackoverflow.com/q/1192054/158288 – Koekiebox Nov 03 '11 at 13:10
  • This is better solution : https://stackoverflow.com/questions/1192054/load-image-from-resources-area-of-project-in-c-sharp/1192072 - for people coming later. – Soren Jan 04 '19 at 00:35

7 Answers7

28

Consider using Properties.Resources.yourImage

Properties.Resources contains everything that you've added as a resource (see your project properties, resources tab)

Other than that, if you embed the images as resource in your project, you can get at them by calling GetManifestResourceStream on the assembly that you've embedded the images in, something like

Stream imgStream = 
    Assembly.GetExecutingAssembly().GetManifestResourceStream(
    "YourNamespace.resources.ImageName.bmp");
pictureBox.Image = new Bitmap(imgStream);

Don't forget to mark the image as an embedded resource! (You'll need to set the build action for the image in its properties window)

If you're finding that you keep getting back null from GetManifestResourceStream, you may be giving the wrong name. (It can be hard to get the names right) Call GetManifestResourceNames on the assembly; that will give you back all the resource names, and you can find the one in the list that you need.

Daniel LeCheminant
  • 50,583
  • 16
  • 120
  • 115
2

Below is the Code to Fetch Image From Resources Folder. Normally we keep images in Resources Folder. but Sometime we have image name only with us. in that case you can Access images from Resources Folder using Image Name only.

Below Code will Demonstrate about it.

private System.Resources.ResourceManager RM = new System.Resources.ResourceManager("YourAppliacationNameSpace.Properties.Resources", typeof(Resources).Assembly);

PbResultImage.Image = (Image)RM.GetObject(YourPictureName);
  • YourAppliacationNameSpace means name of your Application.
  • YourPictureName means the Picture you Want to access from Resources Folder. but Picture name must be without Extension e.g. (PNG, GIF, JPEG etc)

hope i will be beneficial to some one.

Thank you.

2
string img = null;

private void btnShow_Click(object sender, EventArgs e)
{
    string imgName;
    img = textBox1.Text;
    imgName = "images/" + img + ".jpg";

    if (imgName == null)
    {
        MessageBox.Show("no photo");
    }
    else if (imgName != null)
    {
        this.picBox1.Image = Image.FromFile("images/" + img + ".jpg");
    }
}
Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
Narayan
  • 89
  • 1
  • 2
1

Right click on the project. Go to Resources tab. Select the existing option and add the image.

enter image description here

Now access in the code by

Image = Properties.Resources.ImageName
Hassan Rahman
  • 4,953
  • 1
  • 34
  • 32
0

Worked for me:

(Bitmap) Properties.Resources.ResourceManager.GetObject("ImageName");
halfer
  • 19,824
  • 17
  • 99
  • 186
Sayed Abolfazl Fatemi
  • 3,678
  • 3
  • 36
  • 48
0

this works for your control too, if you need to call your control by a string or concatenated reference.

Just call the control type first (in this case, a picturebox). Assume i=some number and d=some string:

var i = <some number>;
var d = <some string>;

((PictureBox)this.Controls["pictureBox" + i.ToString()]).Image = (Image)Properties.Resources.ResourceManager.GetObject("dice" + d);
Paulo
  • 8,690
  • 5
  • 20
  • 34
0

Now it's Possible

pictureBox.Image = Properties.Resources.yourImagename;