Embedded image won't work...

lidds

Junior Contributor
Joined
Nov 9, 2004
Messages
210
I'm having a problem using embedded image resource file. I have added a .jpg file as an embedded resource and set the build action on the image to Embedded Resource. However when I use the follow code which I found on this forum, it returns the following error "Additional information: 'null' is not a valid value for 'stream'." the code that I am using is this

Visual Basic:
        Dim myAssem As [Assembly]
        myAssem = [Assembly].GetExecutingAssembly()

        Dim s As System.IO.Stream
        s = myAssem.GetManifestResourceStream("EmbeddedResources.logo.jpg")

        imgLogo.Image = New Bitmap(s)

Has anyone got any ideas???

Thanks

Simon
 
you need to specify the full name of your application in GetManifestResourceStream

eg:
Visual Basic:
[color=#0000ff]Dim[/color] s [color=#0000ff]As[/color] System.IO.Stream = [Assembly].GetExecutingAssembly().GetManifestResourceStream(Application.ProductName & ".EmbeddedResources.logo.jpg")
also you should be using Image.FromStream to create imgLogo
eg:
Visual Basic:
imgLogo.Image = [color=black][color=#0000ff][color=black]Image.FromStream(s)
[/color][/color]
[/color]

 
dynamic_sysop said:
also you should be using Image.FromStream to create imgLogo
The Bitmap class constructor does have an overload that accepts a stream. Either way works, except that using the Bitmap constructor does not require a cast and assignment.

I don't know how the resources are named in different versions of .Net but this is my favorite way to create a bitmap from a resource:
Visual Basic:
Dim MyImage As New Bitmap(GetType(Me), "MyRootNamespace.MyImageFile.extension")
'Exmaple:
'I know that in VB7, resource names are 
'RootNamespace.Filename (including extension)
Dim imgLogo As New Bitmap(GetType(Me), "CssSoftware.Logo.gif")
No finding assemblies, no loading streams. The constructor finds the assembly that the type you specify belongs to, obtains the stream, and loads the image, all in one line of code (on your part).
 
dynamic_sysop, I have tried to use your example code but it still gives me the same error.

The code that I am using is:

Visual Basic:
                Dim s As System.IO.Stream = [Assembly].GetExecutingAssembly().GetManifestResourceStream(Application.ProductName & ".EmbeddedResources.logo.jpg")

        imgLogo.Image = Image.FromStream(s)

The image file is embedded under the following directory structure images\logo\logo.jpg if this makes a difference, as shown in image below.

image.bmp


marble_eater, I've still trying to get to grips with vb.net, how do I declare myRootNameSpace?? Also would appreshiate if you could explain what it does and what it is used for??

Thanks for your help

Simon
 
The root namespace is most likely the name of your solution, the name of your project, the name of the folder it is contained in, and the name of the executable that is produced. It is the namespace that all your types (classes, enums, structs, etc.) are located in, just as the .Net framework is located in the System namespace. The root namespace may be looked up/changed by going into your projects properties (under the "Project" menu). And, again, I don't know about resource names in .Net 2.0, but in 1.1 the name would be your root namespace (again, probably the name of your project) and the name of the file, separated by a dot (i.e. "MyProject.MyImage.bmp"). Sub folders are ignored in VB, where as they are taken into consideration in C# (for example: "MyProject.Images.ToolBarImages.Open.gif")
 
For some reason it does not like the 'Me' code after the gettype. The error it gives is 'Keyword does not name a type'

Thanks
 
If you are using vb.net 2003 then the msdn has:
Visual Basic:
        Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
        ' <namespace>.<image name>.<format>
        ' doesn't matter if it is in a folder
        Dim myStream As IO.Stream = myAssembly.GetManifestResourceStream("WindowsApplication1.icon1.png")
        Dim image As New Bitmap(myStream)
        Me.BackgroundImage = image

You need the right bit in that string. The "EmbeddedResources." part (in your first post) is the name of the project in the msdn example which is why it didn't work for you. I tested with WindowsApplication1, so my example has that. You should try your project name, or the exe name - it looks like you should use ICAS.logo.jpg

If you have vb.net 2005 then you just do

Visual Basic:
me.backgroundimage = my.resources.logo

after adding the image through the resources manager in "my project"
 
Last edited:
Hi,

One of the things that helps me to troubleshoot resources and their names is to use reflector to open the compiled .NET assembly and check for the complete name of the resource that I am trying to load.

Most likely the problem is that you are trying to get a resource name that does not exist in the assembly. I included an attachment of an assembly's resource viewed with reflector.

thanks,

kmg
 

Attachments

Back
Top