lidds Posted November 24, 2005 Posted November 24, 2005 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 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 Quote
Leaders dynamic_sysop Posted November 24, 2005 Leaders Posted November 24, 2005 you need to specify the full name of your application in GetManifestResourceStream eg: [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: imgLogo.Image = [color=black][color=#0000ff][color=black]Image.FromStream(s)[/color][/color] [/color] Quote
Leaders snarfblam Posted November 24, 2005 Leaders Posted November 24, 2005 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: 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). Quote [sIGPIC]e[/sIGPIC]
lidds Posted November 24, 2005 Author Posted November 24, 2005 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: 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. http://www.i-c-a-s.co.uk/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 Quote
Leaders snarfblam Posted November 24, 2005 Leaders Posted November 24, 2005 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") Quote [sIGPIC]e[/sIGPIC]
lidds Posted November 24, 2005 Author Posted November 24, 2005 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 Quote
jo0ls Posted November 25, 2005 Posted November 25, 2005 (edited) If you are using vb.net 2003 then the msdn has: 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 me.backgroundimage = my.resources.logo after adding the image through the resources manager in "my project" Edited November 25, 2005 by jo0ls Quote
karimgarza Posted November 28, 2005 Posted November 28, 2005 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 Quote You're either a one or a zero. Alive or dead.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.