2

I have been trying to add an image to resources programatically, following instructions from this page.

Here is a picture of my code and the error:

ArgumentException was unhandled

Maybe the error is due to image location. I have put it in

Projects\AddToVSResourcesProgramatically\AddToVSResourcesProgramatically\bin\Debug

What I need: import the image to resources for later use.

edit: VS suggeted to use Image.bitmap and I have. But it is still not working. I have tried these 4 ways enter image description here

Ahmed Faizan
  • 446
  • 5
  • 12
  • 22
  • You don't add anything to resources programmatically. Resources are compiled into your EXE when you build and then you use them at run time. Your EXE can't recompile itself at run time. – jmcilhinney Aug 13 '15 at 06:22

3 Answers3

1

The issue is that it fails to get your image from resources. Add image to your Resources and access through resources.

Bitmap image= new Bitmap(Application1.Properties.Resources.Gooner);

How to add image to resources.

Edit (If you want to load the file)

        // Construct an image object from a file in the local directory.
        // ... This file must exist in the solution.
        Image image = Image.FromFile("Gooner.jpg");

        pb.Image = image;
Community
  • 1
  • 1
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • You are right. But I nees to add it to resources, programatically – Ahmed Faizan Aug 13 '15 at 05:42
  • @AhmedFaizan: The issue in your code is that it cannot find the image. Why did you comment out InitializeComponents method? It should be the first line in the Form constructor normally. – CharithJ Aug 13 '15 at 05:45
  • @AhmedFaizan: What is your requirement? Did you want to user to select any image and set in the PictureBox or do you want to add an image included in your solution? – CharithJ Aug 13 '15 at 05:49
  • I have un-commented the initialize part and I want to add an image to the solution. I can add it to picture box later if I want. – Ahmed Faizan Aug 13 '15 at 05:54
  • @AhmedFaizan: I guess you haven't included your image to the solution. If you have included it in the solution Image.FromFile should work. – CharithJ Aug 13 '15 at 05:57
  • With all due respect, I have put it in every nook and cranny of my application. – Ahmed Faizan Aug 13 '15 at 06:02
0

Try the Bitmap.FromFile static method:

Bitmap image = Bitmap.FromFile("path_to_file.jpg");
Dmitry
  • 13,797
  • 6
  • 32
  • 48
  • Thanks for feedback, When I give the path to the file VS displays error in the "\" area. Do I need to use "\\" when writing the image source something like: Visual Studio 2015\\Projects\\AddToVSResourcesProgramatically\\AddToVSResourcesProgramatically\\bin\\Debug\\Gooner.jpg" – Ahmed Faizan Aug 13 '15 at 05:33
  • 1
    Yes. Or just add @ before your string like: @"Visual Studio 2015\Projects..." – Artem Kulikov Aug 13 '15 at 05:34
0

You should put your file not to this path:

Projects\AddToVSResourcesProgramatically\AddToVSResourcesProgramatically\bin\Debug

but to this:

Projects\AddToVSResourcesProgramatically\AddToVSResourcesProgramatically

Also it's not necessary to copy file manually. To do it automatically - you should find your image in your project's file list and choose Properties. Now, change the property Copy to Output Directory to Copy if newer or Copy always.

Artem Kulikov
  • 2,250
  • 19
  • 32