Using resources
-
I have couple of bitmaps in the root of my project (as Embaded Resources). When I want to reference them I'm using a code like this: Bitmap bmp=new Bitmap(GetType(),"Bitmap1.bmp"); pictureBox1.Image=bmp; But I would like to organize my project and move all the bitmaps to the folder "Bitmaps". Now the first line of the code above will bomb saying that "The resource "Bitmap1.bmp cannot be found in the class SomeNameSpace.SomeClass", where SomeClass is a class where I reference the Bitmap1.bmp. How could I instruct C# to find my bitmap? Jerzy
-
I have couple of bitmaps in the root of my project (as Embaded Resources). When I want to reference them I'm using a code like this: Bitmap bmp=new Bitmap(GetType(),"Bitmap1.bmp"); pictureBox1.Image=bmp; But I would like to organize my project and move all the bitmaps to the folder "Bitmaps". Now the first line of the code above will bomb saying that "The resource "Bitmap1.bmp cannot be found in the class SomeNameSpace.SomeClass", where SomeClass is a class where I reference the Bitmap1.bmp. How could I instruct C# to find my bitmap? Jerzy
Does "Bitmaps.Bitmap1.bmp" work? I've never used the syntax you have here before, glad to see it doesn't always have to be as long as
myAssembly.GetManifestStream yadda yadda yadda;
. James Sonork ID: 100.11138 - Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971 -
Does "Bitmaps.Bitmap1.bmp" work? I've never used the syntax you have here before, glad to see it doesn't always have to be as long as
myAssembly.GetManifestStream yadda yadda yadda;
. James Sonork ID: 100.11138 - Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971No, it doesn't. What is required here is a namespace: This is an example from Mr. Petzold: Icon=new Icon(typeof(ProgramWithIcon),"ProgramWithIcon.ProgramWithIcon.ico"); The first argument of the constructor refers to ProgramWithIcon class. Within that type operator, you can use the name of any class that your program defines. Or you can use the name of any structure, enumeration, interface, or delegate that you program defines. In any code in the ProgramWithIcon class, the expression: typeof(ProgramWithIcon) is equivalent to: GetType() This equivalence means that you can use the somewhat shorter constructor: Icon=new Icon(GetType(),"ProgramWithIcon.ProgramWithIcon.ico"); And the program still works the same. The second argument to the Icon constructor is more or less a filename. If you named the icon MyIcon.ico, the Icon constructor would look like this: Icon=new Icon(GetType(),"ProgramWithIcon.MyIcon.ico"); The first part of the quoted name is called a namespace, but it’s a resource namespace. Don’t confuse it with the .NET Framework namespace. By default, Visual C# .NET gives this resource namespace the same name as the project, but you can change it. It’s the field labeled Default Namespace in the Property Pages dialog box for the project. The name in that field must agree with the first part of the quoted name in the Icon constructor. You can even set Default Namespace field to nothing, in which case the second argument to the Icon constructor is just the bare filename: Icon=new Icon(GetType(),"ProgramWithIcon.ico"); or MyIcon.ico or whatever you’ve named the file. If you’re running the C# compiler from the command line, you use the /res switch for each resource. For example, if you use the compiler switch: /res:ProgramWithIcon.ico you load the icon like so: Icon =new Icon(GeType(),”ProgramWithIcon.ico”); Or you can give the icon an extended name following the filename and a comma: /res:ProgramWithIcon.ico,ProgramWithIcon.ProgramWithIcon.ico You then use the constructor: Icon =new Icon(GeType(),”ProgramWithIcon. ProgramWithIcon.ico”); to load the icon. Here’s a problem you might run into if you just use the default resource namespace name that Visual C# .NET assigns to your project: Suppose you create a new project named ProgramWithIconPlus in which the ProgramWithIconPlus inherits from the ProgramWithIcon class. In the ProgramWithIconPlus project, you create a new file named ProgramWithIconPlus.cs and you also add a link to the existing ProgramW