|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to simply load a freeekin bitmap from a resource?I'm coming over from a background in VC++, and in general I've been
pleased with the simplicity of the .NET environment. Not here. I've done google searches on this subject (loading bitmaps from a local resource) and always come up with complex solutions. This can't be right. Everyone packs bitmaps and other graphics and resources, integral to small executables, right? Setting up a local resource folder, loading in graphics, and setting their properties to 'Embedded Resource' would seem to incorporate them, as expected. Then what? It's tough to make 'FromResource' happy. There's got to be some simple solution to this. _BBB...
I have ran into this problem before. The solution is not complicated, but is hard to locate in the documentation. Here is what I have found and it works for me... I have added a folder to my project named "Images" and in this folder, I have added a few JPG files. I then set the Build Action property for each JPG file to "Embedded Resource". I placed this code on the load of a form to set the Background image of the form to one of the JPG images (the one named Sunset.jpg). Dim thisExe As System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly() Dim file As System.IO.Stream = thisExe.GetManifestResourceStream(thisExe.GetName.Name & ".Sunset.jpg")Me.BackgroundImage = Image.FromStream(file) Note that the function GetManifestResourceStream uses case sensitive parameters. Therefore, ".Sunset.jpg" is not the same as ".sunset.jpg". Here is some code that will cycle through the list of embedded resources. Dim thisExe As System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly() Dim resources() As String = thisExe.GetManifestResourceNames() Dim resource As String = "" For Each resource In resources Debug.WriteLine(resource & Microsoft.VisualBasic.ControlChars.CrLf) Next I hope this helps. Roger.Smith Show quoteHide quote "_BBB" <_BBB@nospam.org> wrote in message news:i1poq0t01ek5is4q450c503oa1g0vuh9hi@4ax.com... > I'm coming over from a background in VC++, and in general I've been > pleased with the simplicity of the .NET environment. Not here. > > I've done google searches on this subject (loading bitmaps from a local > resource) and always come up with complex solutions. This can't be right. > Everyone packs bitmaps and other graphics and resources, integral to small > executables, right? > > Setting up a local resource folder, loading in graphics, and setting their > properties to 'Embedded Resource' would seem to incorporate them, as > expected. > > Then what? It's tough to make 'FromResource' happy. There's got to be > some simple solution to this. > On Tue, 30 Nov 2004 07:28:48 -0500, _BBB <_BBB@nospam.org> wrote:
>I've done google searches on this subject (loading bitmaps from a local Right. That's why Microsoft created some special Icon and Bitmap>resource) and always come up with complex solutions. This can't be right. >Everyone packs bitmaps and other graphics and resources, integral to small >executables, right? constructors, just for you! :-) To create an icon from an embedded icon bitmap: Icon myIcon = new Icon(typeof(seeBelow), "NameOfMyIcon.ico") To create some other bitmap from an embedded bitmap: Bitmap myBitmap = new Bitmap(typeof(seeBelow), "NameOfMyBitmap.bmp") The "seeBelow" type is some arbitrary type in the assembly that contains the icon or bitmap with the specified name as an embedded resource. That's it! Look up the various Icon and Bitmap constructors for more details.
Other interesting topics
Hidding a window and preventing Alt-Tabbing to it
Monitor Dial-up Connection creating instance of a form from a variable Time of inactivity find process by processname Deployed simple C#.NET single form application to Windows server 2003 and the form will not display, Closing all instances of an application Panel & keyboard focus Hello, Japan? Creating VS Studio like control |
|||||||||||||||||||||||