Export (0) Print
Expand All

Bitmap.Bitmap(Type, String) Constructor

Initializes a new instance of the Bitmap class from a specified resource.

Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)

public Bitmap (
	Type type,
	string resource
)
public Bitmap (
	Type type, 
	String resource
)
public function Bitmap (
	type : Type, 
	resource : String
)
Not applicable.

Parameters

type

The class used to extract the resource.

resource

The name of the resource.

This constructor combines the namespace of the given type with the string name of the resource and looks for a match in the assembly manifest. For example you can pass in the Button type and Button.bmp to this constructor and it will look for a resource named System.Windows.Forms.Button.bmp.

The following code example demonstrates how to construct a bitmap from a type, and how to use the Save method. To run this example, paste the code into a Windows Form. Handle the form’s Paint event and call the ConstructFromResourceSaveAsGif method, passing e as PaintEventArgs

private void ConstructFromResourceSaveAsGif(PaintEventArgs e)
{

    // Construct a bitmap from the button image resource.
    Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp");

    // Save the image as a GIF.
    bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif);

    // Construct a new image from the GIF file.
    Bitmap bmp2 = new Bitmap("c:\\button.gif");

    // Draw the two images.
    e.Graphics.DrawImage(bmp1, new Point(10, 10));
    e.Graphics.DrawImage(bmp2, new Point(10, 40));

    // Dispose of the image files.
    bmp1.Dispose();
    bmp2.Dispose();
}

private void ConstructFromResourceSaveAsGif(PaintEventArgs e)
{
    // Construct a bitmap from the button image resource.
    Bitmap bmp1 = new Bitmap(Button.class.ToType(), "Button.bmp");
    // Save the image as a GIF.
    bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.get_Gif());
    // Construct a new image from the GIF file.
    Bitmap bmp2 = new Bitmap("c:\\button.gif");
    // Draw the two images.
    e.get_Graphics().DrawImage(bmp1, new Point(10, 10));
    e.get_Graphics().DrawImage(bmp2, new Point(10, 40));
    // Dispose of the image files.
    bmp1.Dispose();
    bmp2.Dispose();
} //ConstructFromResourceSaveAsGif

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft