แก้ไข

แชร์ผ่าน


Graphics.DrawIcon Method

Definition

Draws the image represented by the specified Icon at the specified coordinates.

Overloads

DrawIcon(Icon, Rectangle)

Draws the image represented by the specified Icon within the area specified by a Rectangle structure.

DrawIcon(Icon, Int32, Int32)

Draws the image represented by the specified Icon at the specified coordinates.

DrawIcon(Icon, Rectangle)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws the image represented by the specified Icon within the area specified by a Rectangle structure.

public:
 void DrawIcon(System::Drawing::Icon ^ icon, System::Drawing::Rectangle targetRect);
public void DrawIcon (System.Drawing.Icon icon, System.Drawing.Rectangle targetRect);
member this.DrawIcon : System.Drawing.Icon * System.Drawing.Rectangle -> unit
Public Sub DrawIcon (icon As Icon, targetRect As Rectangle)

Parameters

icon
Icon

Icon to draw.

targetRect
Rectangle

Rectangle structure that specifies the location and size of the resulting image on the display surface. The image contained in the icon parameter is scaled to the dimensions of this rectangular area.

Exceptions

icon is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an icon from a standard Windows icon file SampIcon.ico in the example folder.

  • Creates a rectangle in which to draw the icon.

  • Draws the icon to the screen.

The position of the rectangle locates the icon on the screen, and the size of the rectangle determines the scaling of the drawn icon.

private:
   void DrawIconRectangle( PaintEventArgs^ e )
   {
      // Create icon.
      System::Drawing::Icon^ newIcon = gcnew System::Drawing::Icon( "SampIcon.ico" );

      // Create rectangle for icon.
      Rectangle rect = Rectangle(100,100,200,200);

      // Draw icon to screen.
      e->Graphics->DrawIcon( newIcon, rect );
   }
private void DrawIconRectangle(PaintEventArgs e)
{        
    // Create icon.
    Icon newIcon = new Icon("SampIcon.ico");
             
    // Create rectangle for icon.
    Rectangle rect = new Rectangle(100, 100, 200, 200);
             
    // Draw icon to screen.
    e.Graphics.DrawIcon(newIcon, rect);
}
Private Sub DrawIconRectangle(ByVal e As PaintEventArgs)

    ' Create icon.
    Dim newIcon As New Icon("SampIcon.ico")

    ' Create rectangle for icon.
    Dim rect As New Rectangle(100, 100, 200, 200)

    ' Draw icon to screen.
    e.Graphics.DrawIcon(newIcon, rect)
End Sub

Applies to

DrawIcon(Icon, Int32, Int32)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws the image represented by the specified Icon at the specified coordinates.

public:
 void DrawIcon(System::Drawing::Icon ^ icon, int x, int y);
public void DrawIcon (System.Drawing.Icon icon, int x, int y);
member this.DrawIcon : System.Drawing.Icon * int * int -> unit
Public Sub DrawIcon (icon As Icon, x As Integer, y As Integer)

Parameters

icon
Icon

Icon to draw.

x
Int32

The x-coordinate of the upper-left corner of the drawn image.

y
Int32

The y-coordinate of the upper-left corner of the drawn image.

Exceptions

icon is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an icon from a standard Windows icon file SampIcon.ico in the example folder.

  • Creates the coordinates of the upper-left corner at which to draw the icon.

  • Draws the icon to the screen.

The drawn icon is unscaled.

private:
   void DrawIconInt( PaintEventArgs^ e )
   {

      // Create icon.
      System::Drawing::Icon^ newIcon = gcnew System::Drawing::Icon( "SampIcon.ico" );

      // Create coordinates for upper-left corner of icon.
      int x = 100;
      int y = 100;

      // Draw icon to screen.
      e->Graphics->DrawIcon( newIcon, x, y );
   }
private void DrawIconInt(PaintEventArgs e)
{
    // Create icon.
    Icon newIcon = new Icon("SampIcon.ico");
             
    // Create coordinates for upper-left corner of icon.
    int x = 100;
    int y = 100;
             
    // Draw icon to screen.
    e.Graphics.DrawIcon(newIcon, x, y);
}
Private Sub DrawIconInt(ByVal e As PaintEventArgs)

    ' Create icon.
    Dim newIcon As New Icon("SampIcon.ico")

    ' Create coordinates for upper-left corner of icon.
    Dim x As Integer = 100
    Dim y As Integer = 100

    ' Draw icon to screen.
    e.Graphics.DrawIcon(newIcon, x, y)
End Sub

Applies to