Cannot load a bitmap into a C# dialog from a DLL.

JohnnyRoy wrote on 7/18/2004, 11:18 PM
I was updating my QuickPan script for Vegas 5 in which I created the dialog box in C# as a DLL. I wanted to add a logo to the dialog box to match an my icon button on the Vegas toolbar. To add the logo, I added a System.Windows.Forms.PictureBox to the dialog and set its Image property to a bitmap. When I tried to test my dialog in Vegas I got the following error:
Error: Specified cast is not valid.
The line of code it’s referring to is:
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
The resource is definitely an image (bitmap) and if I call this dialog from a test EXE program it displays perfectly with the image. So I can only assume that the resource is being compiled correctly into the DLL because its there when called from a test EXE, but somehow in the Vegas hosting environment, this call is failing.

I know the Scripting FAQ talked about Vegas only loading certain DLL’s and if you needed others assemblies that you should specify them in your XML file but I don’t know what to specify for loading resources (i.e., there is no System.Resources.dll).

Can someone shed some light on this? Thanks,

~jr

Comments

roger_74 wrote on 7/19/2004, 1:32 AM
I generally use a slightly different approach.

I set the build action for the resource to Embedded Resource and do this:

this.Icon = new Icon(GetType().Module.Assembly.GetManifestResourceStream("MyNameSpace.MyIconSubFolder.icon.ico"));
JohnnyRoy wrote on 7/19/2004, 6:49 AM
Hi Roger,

I had the build action set as Embedded Resource. I tried modifying your example and now I’m getting this error:
Error: ‘null’ is not a valid value for ‘stream’.
The reference to ‘null’ seems to indicate that the image did not get loaded properly so I’m assuming my problem is that I didn’t understand what you did with your icon example. Here is how I changed the line of code in my previous post.
this.pictureBox1.Image = new 
Bitmap(GetType().Module.Assembly.GetManifestResourceStream("QuickPan.PanOptionForm.pictureBox1.Image"));
QuickPan.PanOptionForm.resources is the name of the file that pictureBox1.Image is defined it. I’m not sure that’s the same as your icon example. Do you have an example with a PictureBox or can you tell me where I went wrong in translating your example? Thanks,

~jr
JohnnyRoy wrote on 7/20/2004, 7:58 PM
Sony,

I need some help here. I can call this DLL from an EXE file and the dialog displays correctly with the bitmap. So that proves to me that the resources are getting compiled into the DLL correctly. Why won’t this same DLL work in Vegas? What is different about your scripting engine and loading embedded resources?

~jr
SonyPJM wrote on 7/21/2004, 5:51 AM
I searched the news groups and it looks like it may be a long-standing bug in the .net scrpiting engine(s).

I'll let you know if I find a work-around.
JohnnyRoy wrote on 7/21/2004, 8:54 PM
Paul,

Wow, that is not very good news. I would be very much interested in any workaround you can find. I really appreciate you investigating this for me. Thanks,

~jr
roger_74 wrote on 8/17/2004, 12:10 AM
JohnnyRoy, I've been sending you emails but it seems you haven't received them. Maybe your Hotmail settings are too strict?
JohnnyRoy wrote on 8/17/2004, 8:22 PM
Roger, That's weird. I have the junk email filter on the lowest setting. I'll send you an email from another account. thanks for letting me know.

~jr
JohnnyRoy wrote on 8/25/2004, 10:21 PM
I finally got this working thanks to Roger. I bought a copy of Microsoft Visual C# .net 2003 and when I set the build action for the resource to Embedded Resource and used the line of code that Roger supplied above, it worked. Perhaps this is a bug in SharpDevelop not dealing with the embedded resource properly. I have no idea what MS VC# is doing different but it was worth the purchase just to get this working. (not to mention having a GUI debug environment again). You still have to manually add a line of code for each resource but at least it works if you do this. I just added them in my constructor after InitializeComponent();

Thank You Roger!

~jr
rcampbel wrote on 8/26/2004, 8:08 AM
rcampbel wrote on 8/26/2004, 9:55 AM
Ok. I finally figured it out. I was trying to access the image that I had added to my form as a background image. The solution is to add the image to the project as a embedded resource, then use GetManifestResouceStream. Now it works.

I have added the full explaination here.

Randall