Edit

Bagikan melalui


Icon Constructors

Definition

Initializes a new instance of the Icon class.

Overloads

Icon(Stream)

Initializes a new instance of the Icon class from the specified data stream.

Icon(String)

Initializes a new instance of the Icon class from the specified file name.

Icon(Icon, Size)

Initializes a new instance of the Icon class and attempts to find a version of the icon that matches the requested size.

Icon(Stream, Size)

Initializes a new instance of the Icon class of the specified size from the specified stream.

Icon(String, Size)

Initializes a new instance of the Icon class of the specified size from the specified file.

Icon(Type, String)

Initializes a new instance of the Icon class from a resource in the specified assembly.

Icon(Icon, Int32, Int32)

Initializes a new instance of the Icon class and attempts to find a version of the icon that matches the requested size.

Icon(Stream, Int32, Int32)

Initializes a new instance of the Icon class from the specified data stream and with the specified width and height.

Icon(String, Int32, Int32)

Initializes a new instance of the Icon class with the specified width and height from the specified file.

Icon(Stream)

Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs

Initializes a new instance of the Icon class from the specified data stream.

public:
 Icon(System::IO::Stream ^ stream);
public Icon (System.IO.Stream stream);
new System.Drawing.Icon : System.IO.Stream -> System.Drawing.Icon
Public Sub New (stream As Stream)

Parameters

stream
Stream

The data stream from which to load the Icon.

Exceptions

The stream parameter is null.

Remarks

An icon resource can contain multiple icon images. One icon file may contain images in several sizes and color depths. The image that is used in an application depends on the operating system and settings. The following list details the typical sizes for an icon:

  • 16 pixels x 16 pixels

  • 32 pixels x 32 pixels

  • 48 pixels x 48 pixels

This constructor returns the smallest image that is contained in the specified stream.

Applies to

Icon(String)

Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs

Initializes a new instance of the Icon class from the specified file name.

public:
 Icon(System::String ^ fileName);
public Icon (string fileName);
new System.Drawing.Icon : string -> System.Drawing.Icon
Public Sub New (fileName As String)

Parameters

fileName
String

The file to load the Icon from.

Remarks

An icon resource can contain multiple icon images. One icon file may contain images in several sizes and color depths. The image that is used in an application depends on the operating system and settings. The following list details the typical sizes for an icon:

  • 16 pixels x 16 pixels

  • 32 pixels x 32 pixels

  • 48 pixels x 48 pixels

This constructor returns the smallest image that is contained in the specified file.

Applies to

Icon(Icon, Size)

Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs

Initializes a new instance of the Icon class and attempts to find a version of the icon that matches the requested size.

public:
 Icon(System::Drawing::Icon ^ original, System::Drawing::Size size);
public Icon (System.Drawing.Icon original, System.Drawing.Size size);
new System.Drawing.Icon : System.Drawing.Icon * System.Drawing.Size -> System.Drawing.Icon
Public Sub New (original As Icon, size As Size)

Parameters

original
Icon

The Icon from which to load the newly sized icon.

size
Size

A Size structure that specifies the height and width of the new Icon.

Exceptions

The original parameter is null.

Remarks

If a version cannot be found that exactly matches the size, the closest match is used. If the original parameter is an Icon that has a single size, this method only creates a duplicate icon. Use the stretching capabilities of the DrawImage method to resize the icon.

Applies to

Icon(Stream, Size)

Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs

Initializes a new instance of the Icon class of the specified size from the specified stream.

public:
 Icon(System::IO::Stream ^ stream, System::Drawing::Size size);
public Icon (System.IO.Stream stream, System.Drawing.Size size);
new System.Drawing.Icon : System.IO.Stream * System.Drawing.Size -> System.Drawing.Icon
Public Sub New (stream As Stream, size As Size)

Parameters

stream
Stream

The stream that contains the icon data.

size
Size

The desired size of the icon.

Exceptions

The stream is null or does not contain image data.

Applies to

Icon(String, Size)

Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs

Initializes a new instance of the Icon class of the specified size from the specified file.

public:
 Icon(System::String ^ fileName, System::Drawing::Size size);
public Icon (string fileName, System.Drawing.Size size);
new System.Drawing.Icon : string * System.Drawing.Size -> System.Drawing.Icon
Public Sub New (fileName As String, size As Size)

Parameters

fileName
String

The name and path to the file that contains the icon data.

size
Size

The desired size of the icon.

Exceptions

The string is null or does not contain image data.

Remarks

If the specified file does not contain an image that matches the desired size, the icon that has the closest size is returned.

The fileName should include the complete path if it is not in the current application directory.

Applies to

Icon(Type, String)

Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs

Initializes a new instance of the Icon class from a resource in the specified assembly.

public:
 Icon(Type ^ type, System::String ^ resource);
public Icon (Type type, string resource);
new System.Drawing.Icon : Type * string -> System.Drawing.Icon
Public Sub New (type As Type, resource As String)

Parameters

type
Type

A Type that specifies the assembly in which to look for the resource.

resource
String

The resource name to load.

Exceptions

An icon specified by resource cannot be found in the assembly that contains the specified type.

Examples

The following code example demonstrates how to use the Icon constructor. To run this example, paste the code into a Windows Form and handle the form's Paint event. Call the ConstructAnIconFromAType method from the Paint event handler, passing e as EventArgs.

private void ConstructAnIconFromAType(PaintEventArgs e)
{

    Icon icon1 = new Icon(typeof(Control), "Error.ico");
    e.Graphics.DrawIcon(icon1, new Rectangle(10, 10, 50, 50));
}
Private Sub ConstructAnIconFromAType(ByVal e As PaintEventArgs)

    Dim icon1 As New Icon(GetType(Control), "Error.ico")
    e.Graphics.DrawIcon(icon1, New Rectangle(10, 10, 50, 50))

End Sub

Remarks

This constructor creates an Icon from a resource with the name specified by the resource parameter in the assembly that contains the type specified by the type parameter.

This constructor combines the namespace of the given type together with the string name of the resource and looks for a match in the assembly manifest. For example you can pass in the Control type and Error.ico to this constructor, and it looks for a resource that is named System.Windows.Forms.Error.ico.

Applies to

Icon(Icon, Int32, Int32)

Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs

Initializes a new instance of the Icon class and attempts to find a version of the icon that matches the requested size.

public:
 Icon(System::Drawing::Icon ^ original, int width, int height);
public Icon (System.Drawing.Icon original, int width, int height);
new System.Drawing.Icon : System.Drawing.Icon * int * int -> System.Drawing.Icon
Public Sub New (original As Icon, width As Integer, height As Integer)

Parameters

original
Icon

The icon to load the different size from.

width
Int32

The width of the new icon.

height
Int32

The height of the new icon.

Exceptions

The original parameter is null.

Examples

The following code example demonstrates how to use the ToBitmap method. This example is designed to be used with Windows Forms. Create a form and paste the following code into it. Call the IconToBitmap method in the form's Paint event handler, passing e as PaintEventArgs.

private:
   void IconToBitmap( PaintEventArgs^ e )
   {
      // Construct an Icon.
      System::Drawing::Icon^ icon1 = gcnew System::Drawing::Icon( SystemIcons::Exclamation,40,40 );

      // Call ToBitmap to convert it.
      Bitmap^ bmp = icon1->ToBitmap();

      // Draw the bitmap.
      e->Graphics->DrawImage( bmp, Point(30,30) );
   }
private void IconToBitmap(PaintEventArgs e)
{
    // Construct an Icon.
    Icon icon1 = new Icon(SystemIcons.Exclamation, 40, 40);

    // Call ToBitmap to convert it.
    Bitmap bmp = icon1.ToBitmap();

    // Draw the bitmap.
    e.Graphics.DrawImage(bmp, new Point(30, 30));
}
Private Sub IconToBitmap(ByVal e As PaintEventArgs)

    ' Construct an Icon.
    Dim icon1 As New Icon(SystemIcons.Exclamation, 40, 40)

    ' Call ToBitmap to convert it.
    Dim bmp As Bitmap = icon1.ToBitmap()

    ' Draw the bitmap.
    e.Graphics.DrawImage(bmp, New Point(30, 30))
End Sub

Remarks

If a version cannot be found that exactly matches the size, the closest match is used. If the original parameter is an Icon that has a single size, this method only creates a duplicate icon. Use the stretching capabilities of the DrawImage method to resize the icon.

Applies to

Icon(Stream, Int32, Int32)

Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs

Initializes a new instance of the Icon class from the specified data stream and with the specified width and height.

public:
 Icon(System::IO::Stream ^ stream, int width, int height);
public Icon (System.IO.Stream stream, int width, int height);
new System.Drawing.Icon : System.IO.Stream * int * int -> System.Drawing.Icon
Public Sub New (stream As Stream, width As Integer, height As Integer)

Parameters

stream
Stream

The data stream from which to load the icon.

width
Int32

The width, in pixels, of the icon.

height
Int32

The height, in pixels, of the icon.

Exceptions

The stream parameter is null.

Applies to

Icon(String, Int32, Int32)

Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs
Source:
Icon.cs

Initializes a new instance of the Icon class with the specified width and height from the specified file.

public:
 Icon(System::String ^ fileName, int width, int height);
public Icon (string fileName, int width, int height);
new System.Drawing.Icon : string * int * int -> System.Drawing.Icon
Public Sub New (fileName As String, width As Integer, height As Integer)

Parameters

fileName
String

The name and path to the file that contains the Icon data.

width
Int32

The desired width of the Icon.

height
Int32

The desired height of the Icon.

Exceptions

The string is null or does not contain image data.

Remarks

If the specified file does not contain an image that matches the desired height and width, the icon that has the closest size is returned.

The fileName should include the complete path if it is not in the current application directory.

Applies to