Bitmap-Konstruktor (Type, String)
Initialisiert eine neue Instanz der Bitmap-Klasse aus einer angegebenen Ressource.
Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)
Syntax
'Declaration
Public Sub New ( _
type As Type, _
resource As String _
)
'Usage
Dim type As Type
Dim resource As String
Dim instance As New Bitmap(type, resource)
public Bitmap (
Type type,
string resource
)
public:
Bitmap (
Type^ type,
String^ resource
)
public Bitmap (
Type type,
String resource
)
public function Bitmap (
type : Type,
resource : String
)
Parameter
- type
Die Klasse, mit der die Ressource extrahiert wird.
- resource
Der Name der Ressource.
Hinweise
Dieser Konstruktor kombiniert den Namespace des angegebenen Typs mit dem Zeichenfolgennamen der Ressource und sucht nach einem übereinstimmenden Eintrag im Assemblymanifest. Sie können beispielsweise an diesen Konstruktor den Button-Typ und Button.bmp übergeben, und der Konstruktor sucht nach einer Ressource mit dem Namen System.Windows.Forms.Button.bmp
.
Beispiel
Im folgenden Codebeispiel wird veranschaulicht, wie anhand eines Typs eine neue Bitmap erstellt und wie die Save-Methode verwendet wird. Fügen Sie den Code in ein Windows Form ein, um das Beispiel auszuführen. Behandeln Sie das Paint-Ereignis des Formulars, und rufen Sie die ConstructFromResourceSaveAsGif
-Methode auf, wobei Sie e als PaintEventArgs übergeben.
Private Sub ConstructFromResourceSaveAsGif(ByVal e As PaintEventArgs)
' Construct a bitmap from the button image resource.
Dim bmp1 As New Bitmap(GetType(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.
Dim bmp2 As 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()
End Sub
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 = gcnew Bitmap(Button::typeid, "Button.bmp");
String^ savePath =
Environment::GetEnvironmentVariable("TEMP") + "\\Button.bmp";
try
{
// Save the image as a GIF.
bmp1->Save(savePath, System::Drawing::Imaging::ImageFormat::Gif);
}
catch (IOException^)
{
// Carry on regardless
}
// Construct a new image from the GIF file.
Bitmap^ bmp2 = nullptr;
if (File::Exists(savePath))
{
bmp2 = gcnew Bitmap(savePath);
}
// Draw the two images.
e->Graphics->DrawImage(bmp1, Point(10, 10));
// If bmp1 did not save to disk, bmp2 may be null
if (bmp2 != nullptr)
{
e->Graphics->DrawImage(bmp2, Point(10, 40));
}
// Dispose of the image files.
delete bmp1;
if (bmp2 != nullptr)
{
delete bmp2;
}
}
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
Plattformen
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
Siehe auch
Referenz
Bitmap-Klasse
Bitmap-Member
System.Drawing-Namespace