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

1
  • 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. Commented Aug 13, 2015 at 6:22

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

6 Comments

You are right. But I nees to add it to resources, programatically
@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.
@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?
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.
@AhmedFaizan: I guess you haven't included your image to the solution. If you have included it in the solution Image.FromFile should work.
|
0

Try the Bitmap.FromFile static method:

Bitmap image = Bitmap.FromFile("path_to_file.jpg");

2 Comments

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"
Yes. Or just add @ before your string like: @"Visual Studio 2015\Projects..."
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.

2 Comments

I have put it there as well but the same error comes
Can you please provide printscreen with your project's file list?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.