0

Once i add this line to the code:

this.tsbAdd.Image = Bitmap.FromFile(@"..\..\Resources\add.bmp");

I'm unable to open editor of that form.

Screenshot of designer

I can compile app and images work as they should.

Expected results - new image is displayed without breaking designer.

Real results - new image breaks designer.

Once i build it into .exe it doesn't open. Without images it works flawlessly.

  • If you are expecting that image to be distributed with the app, then you should compile it into the exe as a resource, not as a file. – DavidG Apr 06 '20 at 11:07
  • @DavidG if i understand correctly once i build project it should be in .exe –  Apr 06 '20 at 11:10

1 Answers1

0

Nope it won't. The picture will be built, but referencing it by this path won't be working.

The resource file WOULD be built into your exe, but not at "....\Resources\add.bmp". This path only exists in your IDE configuration position, when your program is at "bin\Debug", you understand me?

Imagine you put your exe into C:\, then where is "....\Resources"? You cannot refer to a image in this way.

You should add resources in the project panel (I believe you have done that), and the way you get this file is via ResourceManager, not using this path. Like this:

ResourceManager rm = Resources.ResourceManager;
this.tsbAdd.Image = (Image) rm.GetObject("add");

The resourcemanager will pull the resource bitmap out from your built exe. Just using that path won't work. As the designer is not ran in \bin\Debug, no wonder it is broken too because it cannot find your file using that path.

Nemo Zhang
  • 146
  • 7
  • Would replacing "add" to "add.bmp" work? And make sure you have added the bmp file as a resource in the resource panel of project settings. – Nemo Zhang Apr 06 '20 at 12:16
  • added .bmp, nothing. Marked as a Resource. –  Apr 06 '20 at 12:17
  • Both embedded resource and resource gives the same results. –  Apr 06 '20 at 12:21
  • You open project "Properties", and then to the left select "Resource". Press Ctrl+2 or select from the top-left toolstrip will let you add Image resources. Click on the dropdown menu of "Add resource", and select "add existing file". Did you add your resource in this way? Sorry for inaccurate naming because I am using a Chinese version of VS2017. I have tested the code on my local project, and it worked fine. – Nemo Zhang Apr 06 '20 at 12:25
  • Added it this way, nothing. But i also added resources to form that used it. Now it works. Either way, thanks for help. Otherwise it would take a few more hours. –  Apr 06 '20 at 12:35
  • Oh I forgot the form resource problem. Sorry! – Nemo Zhang Apr 06 '20 at 12:50