Question

I would like to know how to use images that I have placed in an external .resources file. Long story short: my program is subjected to a lot of changes, while the images within it are only updated once a year. I figured I could make reduce the amount my users would need to download by storing these images into a binary resource file, so only the main executable needs to be re-downloaded. I generated this file using something like this:

Dim stream As System.IO.Stream
Dim rsxw As New System.Resources.ResXResourceWriter("MyData.resx")
stream = Me.GetType().Assembly.GetManifestResourceStream("MyProgram.File1.png")
Dim bmp = New Bitmap(stream)
stream.Close()
Dim img As System.Drawing.Image = CType(bmp, System.Drawing.Image)
rsxw.AddResource(name, img)
bmp.Dispose()
img.Dispose()
rsxw.Close()

I then compiled the resulting resx file to a .resources file using ResGen. I would now like to use these images in my program.

I was initially doing something like this:

dim filename as string = "File1"
stream =Me.GetType().Assembly.GetManifestResourceStream("MyProgram." & filename & ".png")
picboximg = New Bitmap(stream)
stream.Close()
PreviewBox.Image = picboximg

But since the resources are no longer embedded, this method no longer works. How do I go about getting the images from this resource file now?

Thanks for any help!

Was it helpful?

Solution

You can do this with the ResourceReader class.

But there's no good reason to give up on the convenience and type-safety of My.Resources. Simply add a class library project to your solution, call it (say) MyRes. Click the Show All Files button in the Solution Explorer window toolbar. Expand My Project and double-click Resources.resx. On the toolbar, change the Access Modifier property to "Public". Add resources like you normally do. Add the project reference, you can now simply access the resources in this separate dll in your code. For example:

    Me.BackgroundImage = MyRes.My.Resources.animation
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top