Click to See Complete Forum and Search --> : using bmps by adding to resources?


virginie
June 26th, 2005, 05:57 AM
hi,
i have to build a gui that will be composed of bmps.
i tried to create it in a similar way of mfc ( by ading bmps to resources) but i am very lost and i dont event see resources file....

how can i use my bmps?
i give the full path from where to retrieve the pics but this is not the solution, i need to include the pics locally to resources and then use them...

how we do that in c# ?? :eek:

Jason Isom
June 26th, 2005, 11:27 AM
Are you talking about adding resources at compile-time or runtime? If you're talking about compile-time, do you have Visual Studio .NET ?

If you do have Visual Studio .NET, just right click in the Solution Explorer and click Add > New Item... and select Bitmap if you want to create your own bitmaps, or Add > Existing Item... to add an already created bitmap.

Then find your bitmap in the Solution Explorer, and in the properties window, change Build Action to Embedded Resource if you wish it to be compiled into the executable and not linked...

To get a list of all resources, use the following code

// get a reference to the current assembly
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();

// get a list of resource names from the manifest
string [] resNames = a.GetManifestResourceNames();


Console.WriteLine(String.Format("Found {0} resources\r\n", resNames.Length));
Console.WriteLine("----------\r\n");
foreach(string s in resNames)
{
Console.WriteLine("\r\n");
if(s.EndsWith(".bmp"))
{
Console.WriteLine(s);
}
}

I use the following code to use the resource images in my code....

public Card(char f, char s)
{
face = f;
suit = s;

System.Reflection.Assembly myAssembly;
myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream imageFile =
myAssembly.GetManifestResourceStream("Card_Deck.images." + f + s + ".bmp");
image = Image.FromStream(imageFile);

}

The reason this line says "myAssembly.GetManifestResourceStream("Card_Deck.images." + f + s + ".bmp");" is because it seems like it names the resource files in the form of "<NAMESPACE>.<FOLDERNAME>.<IMGFILE>"

I put all my bitmaps in an image folder in my solution explorer, so there is a chance that you don't need it.



If you don't have Visual Studio.NET, then I'm sure there are some flags for csc that will do it, although I have absolutely no clue what they are.

I hope the above posts helps...I'm relatively new to C#.NET, so I apologise in advance if I'm WAY off base.

Jason Isom
June 26th, 2005, 11:36 AM
Actually, I just found this link. It seems a lot easier to just use the Bitmap constructor to get/use bitmap files instead of using the Image Stream from GetManifestResourceStream

http://www.codeguru.com/forum/showthread.php?t=346331

virginie
June 28th, 2005, 04:25 AM
thx :)