locked
How do I import a binary resource? RRS feed

  • Question

  • I need to add a PNG file as a binary resource to avoid having the IDE accidently write out the image in "2.0" format after which the IDE will refuse to build due to error RC2170. But I cannot figure out how to add the file as a binary resource in VS 2008. The IDE keeps importing it as a PNG resource. If I rename the extension from .png to something else (.mypng), after I go thru the steps to import the file, absolutely nothing shows up in my resource view (no error, no message, nothing seems to happen). I have tried to use the "Custom" button, but I have found no way to specify the file on disk.
    Thursday, June 11, 2009 3:01 PM

Answers

  • From the Add Resource dialog, click Import.  Select the file, you'll get a dialog to specify the resource type name.  Type something, like MYPNG.

    Hans Passant.
    Thursday, June 11, 2009 3:19 PM

All replies

  • From the Add Resource dialog, click Import.  Select the file, you'll get a dialog to specify the resource type name.  Type something, like MYPNG.

    Hans Passant.
    Thursday, June 11, 2009 3:19 PM
  • I'm sorry, I didn't mention I am using Visual Studio 2008. With Visual Studio 2008, I ran Add Resource, I clicked Import, a File Open dialog appears. I select the resource. So far this is what you describe, assuming by specify the resource type name you mean select it when the file browse dialog appears, which I do. There is no field that lets me specify the resource type in the file open dialog. So I click the "OK" button. That's all there is. The IDE immediately imports the resource, adds a new entry under my resource view named "PNG". If I open up the PNG tree, my resource is there with an id of IDB_PNG1. If I double click it, instead of opening up as a binary, it opens up as an image. Now is when things get really nasty. Make the wrong move and Visual Studio 2008 will save the PNG file to disk apparently in "2.0" format (I don't ever edit the image). Try to build the resources and the resource compiler claims I have saved the image out in an invalid format and I need to save it out in "3.x" format (i.e., I get error RC2170). The build fails and compliments of the IDE, I am hosed. I have to delete the file on disk AND remove the entry from the resource and import it again (I tried just overwriting the image but the build still fails with error RC2170). So I have to avoid ever opening the resource in the IDE to avoid doing anything that will cause the IDE to rewrite the image to disk and corrupt it again. I believe what you describe is almost how one imported binary resources back in the Visual Studio 6 days (have to choose custom and then type in "Binary" for the resource type). Visual Studio 2008 has a "Custom" button and I can type in "Binary" and I get a tiny binary resource. But there is no chance to select the file on disk. The IDE immediately creates a small binary resource and opens it up in the IDE. It also adds a "BINARY" tree.

    Before I posted I tried a number of ways to get the file added as a binary but never found a way to do so.

    So, following your exact steps fails with Visual Studio 2008.

    Sorry I missed replying quickly. I have the "Alert me" button checked but never received any alert. Well at least nothing made it to my inbox. Nor in my junk/spam folder. I must have something wrong in my profile ...
    Friday, June 19, 2009 1:29 PM
  • I almost got a PNG imported as a binary. When the add resource dialog comes up I click the custom button and then I keyin "Binary" in the "Resource type:" of the "New Custom resource" dialog. As I mentioned before, a new binary resource is added with an id of IDR_BINARY1. In the resource tree, a new "folder" named "Binary" is created and IDR_BINARY1 is present. I right-click on it, no "Properties" on the shortcut menu. So I right-click on any other resource and click "Properties" to get the properties window to display. Then I click the "IDR_BINARY1" entry.

    The properties window stays open and I see that "External File" is TRUE, and there is a Filename entry with a value of "res\binary1.bin". Build, save etc and there is no such file on disk. I tried changing the name to the file on disk I want, close the resource if it is open (if I double-click "IDR_BINARY1" it opens in the IDE with one word worth of data) and reopen it. It still opens as a small binary (shows as 00000000).

    So I had another ideal. I closed the binary in the IDE and went to disk and renamed my file to "binary1.bin". Finally when I double click IDR_BINARY1, a large binary opens in the IDE! Time for celebration!

    Well almost. Save everything, build, build build, save save save. Close the solution. Go home happy as heck. But wait, first I better reopen the solution to double check. So I do. Guess what, the entire "Binary" folder in the resource view has disappeared! In fact, after testing, no matter what I do, "Binary" always gets lost when I close the solution and reopen it (that is just import a custom "Binary" type and save and build, close and reopen the solution and the entry is gone).

    I think I paid too much for Visual Studio 2008. Should have stayed with 6.0 (except I made the additional mistake of moving to Vista and 6.0 has "known compatibility issues").

    Friday, June 19, 2009 2:11 PM
  • I think the steps I described where for VS2005, I don't remember.  VS2008 indeed has .png awareness, it automatically creates the PNG resource type.  I cannot repro your "make the wrong move" step at all, what ever it might be.  The resource editor doesn't actually allow editing the image so there's no reason for it to save the file back to disk.  I can do Image + Rotate to force a change, rc.exe doesn't have a problem with it.

    If you actually can edit the image, consider the possibility that the .png file is not actually in the PNG format.  That's possible, the image decoders in Windows don't go by the filename extension, they go by the file header.  You'd have to look at the file with a hex viewer to see its true format.  PNG files have "PNG" at offset 1.  BMP files have "BM" at offset 0.

    Hans Passant.
    Friday, June 19, 2009 2:11 PM
  • Hang on, I did get a repro.  I opened the test project some time later and did get the same RC error you did.  It changed the .rc content from

    IDB_PNG1    PNG    "C:\\temp\\test.png"

    to

    IDB_PNG1    BITMAP    "C:\\temp\\test.png"


    Not sure when the IDE made this change.  This quacks like a bug.  Barks.  Post to connect.microsoft.com to see if they have a workaround.  Meanwhile, you could perhaps bypass this bug by renaming the file and giving it another filename extension.


    Hans Passant.
    Friday, June 19, 2009 2:53 PM
  • I never did figure out exactly what I did either (hence my "wrong move" with no details). One day I just did a build and got the error. I don't recall even having the file open though I can't say I didn't. And you are right, I never edited the image. My guess is I had the image open in one window and modified a resource in another window (or added a new resource) and then did a build and the IDE saved the PNG file out. In any case, I am not even sure I can make the resource a binary resource since we don't load the image directly. We are using new UI capabilities to build an Office 2007(?) GUI (with the command ribbon bar, docking panes etc.) and thus we simply pass the resource ID to code we don't control. When we pass a resource ID that is for a PNG resource, the code loads the resource as an image and sees that it is an "alpha" image and renders it just fine. When I step thru the code that loads the resource, it sure looks like to me that the attempt to load the image could fail if the resource is not really an image. But that is conjecture, which is why I was trying to add it as binary to see what would happen when we passed an ID of a binary resource to the API that normally expects an image.

    By the way, when I rename to something like xyz.notpng and do an import, the only change in the project is the resource.h file bumps the _APS_NEXT_RESOURCE_VALUE up by one (I copied resource.h before adding it, added it, did a build and then ran windiff on the copied file and the newly written resource.h file.) Nothing else changes but the next resource value. The resource never shows up in the resource view, nor does the file show up in the Solution Explorer view.

    My advice is to stick with 2005. 2008 has other problems. It likes to crash a lot. Sometimes you get a "check online for a solution and restart" and sometimes it just plain disappears with no message box. And sometimes Vista just tosses up a dialog and tells me 2008 has stopped responding and needs to be shutdown. Yesterday I was working in another application and all of a sudden the system popped up the "check online for a solution and restart dialog". I hadn't touched 2008 for a couple of hours when that happened. Microsoft has previously told me to turn off all of my add-ins in 2008. But the only add-in I have is TFS, a microsoft add-in for source control and we surely don't want to turn that off. Oh and it mysteriously starts skipping breakpoints (even with the hotfix for that bug introduced in SP1) ...

    Friday, June 19, 2009 3:51 PM
  • microsoft.connect is a cluster-f**. I have never had any success there. I just spent more time again. Registered so I can get a "dashboard page". Replied to the e-mail they sent as verification and that took me to the "register" page again so I can get a dashboard. So I gave up on getting a "dashboard". Instead I searched and navigated around connect and I finally found a place to report a problem (I found a "Help and Support" link). Now I only need to give MS my credit card number so they can charge me $259.00 to report a bug in their product!!! I'm sure there's probably a nice big button that says "Report a bug" and I just plain miss it every time I go to that site.

    Besides, when does Microsoft ever fix a bug? They just seem to put out a KB article and propogate the bug on up the line infinitum. Heck I recently reported a bug in the OS (via our corporate support account) and was told there would be no fix because the bug has existed since Win95! But there is a KB article that tells one how to try and work-around the issue! I suppose that's a clue as to why the task manager shows me my dual-quad core 64 bit CPU is mostly unutilized (CPU usage rarely moves very high) while I sit and wait for some action to complete while Vista spins that little blue cursor around. That is, I think somewhere deep in the bowels of the OS, Microsoft is still executing 16 bit code on my high-end box! There's probably another KB article somewhere that tells me that is so if I just know what keyword to google for ...
    Friday, June 19, 2009 4:16 PM