Quantcast
Channel: Alextv.net feed from blogs.msdn.com
Viewing all articles
Browse latest Browse all 25

Extracting embedded images

$
0
0

Periodically we get questions about how to extract and use an image compiled as an embedded resource in an assembly. It’s easily done using the Bitmap or Icon constructor that takes a type and string parameters.

The documentation for these constructors describes how they work, but I’ll summarize here. The string parameter is the image name, and the type parameter is used to retrieve the namespace for the resource and construct the long name for it. For example, say you want to leverage the Windows Forms button icon. You need to pass in a type in the System.Windows.Forms namespace (the example in the documentation uses the Button type but you could use any type in this namespace; DataGridView, for example) and the name of the image, in this case "Button.bmp". The constructor combines the namespace of  the type and resource name to find the resource in the assembly manifest. For this example, it would search for a resource with a long name of "System.Windows.Forms.Button.bmp". The method call looks like this:

C#:

Bitmap myBitmap = new Bitmap (typeof (DataGridView), “Button.bmp”);

 

Visual Basic:

myBitmap as new Bitmap (GetType (DataGridView), “Button.bmp”)

 

On a related note, a useful addition to the Icon class in the .NET Framework  version 2.0 is the ExtractAssociatedIcon method. As its name indicates, this static method allows you to extract the icon associated with a file or application. The example in the documentation shows how to extract the icon associated with an application, but you can extract the icon associated with any type of file. The method call is very straightforward; the string parameter is the path to the file and the method returns an icon. The following example sets the form’s icon to the icon for Microsoft Word:

 

C#

this.Icon = Icon.ExtractAssociatedIcon(@"c:\myResume.doc");

 

Visual Basic:

Me.Icon = Icon.ExtractAssociatedIcon("c:\myResume.doc")

 

 


Viewing all articles
Browse latest Browse all 25

Latest Images

Trending Articles





Latest Images