Resolved Store the actual bitmap data in the resx file

aaaron

Well-known member
Joined
Jan 23, 2011
Messages
219
Programming Experience
10+
I deleted an old file and my solution then reported that it could not find the ico associated with a Toolstripbutton.

Actually the error was: <image name> is not a member of Resources.

I looked at the .resx file and it does point to the folder I deleted.

So I tried to add a resource (using Resources page of the project properties. and by double clicking resx file) for that image.

Tried a few different ways but the resulting file (looking with notepad) always points to the file I included.

What I would like is that the actual bitmap data be stored in the resx file.

If that's possible, how should I go about it?


BTW, as I typed I notice there is no spell checker. I think there use to be one,. No?
 
Last edited:
Solution
My apologies. It appears that I was wrong about what happens when adding a file to resources. It's actually quite a while since I've done it myself and I could have sworn that that's how it used to work, but maybe I'm remembering incorrectly. I guess you're going to have to add the image file to your project first, then add it as a resource from there to ensure that it doesn't get deleted from an external source. You can set the Build Action to Nothing to prevent the file being copied to the output folder.

Note that I did try adding a PictureBox to a Windows Form, selecting the Image and then adding a local resource and it did add the data in base-64 format to the RESX file for the form. The linked file resource looked something like...
When you add a file to your resources, the file is generally copied to a Resources folder in your project and that file is then compiled into the EXE when you build. If you deleted the file in the Resources folder then, unless you still have the original, you have basically screwed yourself. If the RESX file contains a long, random-looking string then that would be a base-64 version of the image data, so you can still convert that back to an image. You can write a little program that reads that text, calls Convert.FromBase64 to create a Byte array and then save that to an image file.
 
Thanks, I'm afraid I wasn't clear. The folder I deleted was not part of the soultion. It was under Documents.

I'm not adding the image to my resourses correctly. When I do I get something like this in the resx file:

<data name="Copy" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\..\..\..\..\Desktop\Images\Copy.bmp;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>

Instead of the the long random looking string.

How do I get it to store the string you mentioned?

I click on the project name, the select "Properties", from dropdown click "Image". click "Existing File", Selected a file from a Desktop folder

and I get the above rather than the image data.
 
I click on the project name, the select "Properties", from dropdown click "Image". click "Existing File", Selected a file from a Desktop folder
That's not adding a resource at all. That's adding an item to the project, i.e. a source file. If you add the file in its existing location and then you delete that file, of course it goes away. If you want to add a file to the project then copy the file into the project folder. If you want to add a resource then actually add a resource, i.e. open the Resources page of the project properties and add the file there.
 
In case I wasn't clear, this is what I did, here it is in more detail:

Right click project name and then select Properties

Click Resources on the left

Click dropdown arrow next to Add Resources

Select Add Existing File

Selected a OpenDir file from Desktop\Images Folder

Clicked File/SaveAll

Under My Project, Resources.resx, Open With, Notbook:

Find:
<data name="OpenDir" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\..\..\..\..\Desktop\Images\OpenDir.bmp;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>

If that's the same as you understood it before I'd like more detail, if you will, about:
i.e. open the Resources page of the project properties and add the file there.
 
My apologies. It appears that I was wrong about what happens when adding a file to resources. It's actually quite a while since I've done it myself and I could have sworn that that's how it used to work, but maybe I'm remembering incorrectly. I guess you're going to have to add the image file to your project first, then add it as a resource from there to ensure that it doesn't get deleted from an external source. You can set the Build Action to Nothing to prevent the file being copied to the output folder.

Note that I did try adding a PictureBox to a Windows Form, selecting the Image and then adding a local resource and it did add the data in base-64 format to the RESX file for the form. The linked file resource looked something like this:
Linked File:
  <data name="FileNameWithoutExtension" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>FilePath;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
  </data>
while the embedded resource looked something like this:
Embedded Resource:
  <data name="FileNameWithoutExtension" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
    <value>base-64 text</value>
  </data>
I opened the two RESX files as XML by doing a Find In Files for an appropriate property name, then I copied the embedded resource from the form file over the linked file in the project file and it worked as expected. It's a bit laborious but it works, if you really want the data embedded. Given the size of the data in my test case, I'm wondering whether they actually did switch from embedded resources to linked files specifically because of the potential size of those embedded resources.
 
Solution
I used to copy images in Windows Explorer and paste them to the Resources page, the image files were then automatically added to a Resources folder (with Build Action None, and Do not copy to output) and the resources were added pointing to the project file. Now something with the resources designer has changed and file is no longer added to project, the resource points to original file.
 

Latest posts

Back
Top