Graphics.FromHdc 方法

定义

从设备上下文的指定句柄创建新的 Graphics

重载

FromHdc(IntPtr)

从设备上下文的指定句柄创建新的 Graphics

FromHdc(IntPtr, IntPtr)

从设备上下文的指定句柄和设备的句柄创建新的 Graphics

FromHdc(IntPtr)

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

从设备上下文的指定句柄创建新的 Graphics

public:
 static System::Drawing::Graphics ^ FromHdc(IntPtr hdc);
public static System.Drawing.Graphics FromHdc (IntPtr hdc);
static member FromHdc : nativeint -> System.Drawing.Graphics
Public Shared Function FromHdc (hdc As IntPtr) As Graphics

参数

hdc
IntPtr

nativeint

设备上下文的句柄。

返回

此方法为指定的设备上下文返回新的 Graphics

示例

下面的代码示例旨在与 Windows 窗体 一起使用,它需要 PaintEventArgse,这是事件处理程序的参数Paint。 代码执行以下操作:

  • 创建一个内部指针类型变量 hdc ,并将其设置为窗体图形对象的设备上下文的句柄。

  • 使用 hdc创建新的图形对象。

  • 在屏幕上) 使用新图形对象 (绘制矩形。

  • 使用 hdc释放新的图形对象。

public:
   void FromHdcHdc( PaintEventArgs^ e )
   {
      // Get handle to device context.
      IntPtr hdc = e->Graphics->GetHdc();

      // Create new graphics object using handle to device context.
      Graphics^ newGraphics = Graphics::FromHdc( hdc );

      // Draw rectangle to screen.
      newGraphics->DrawRectangle( gcnew Pen( Color::Red,3.0f ), 0, 0, 200, 100 );

      // Release handle to device context and dispose of the      // Graphics object
      e->Graphics->ReleaseHdc( hdc );
      delete newGraphics;
   }
private void FromHdcHdc(PaintEventArgs e)
{
    // Get handle to device context.
    IntPtr hdc = e.Graphics.GetHdc();

    // Create new graphics object using handle to device context.
    Graphics newGraphics = Graphics.FromHdc(hdc);

    // Draw rectangle to screen.
    newGraphics.DrawRectangle(new Pen(Color.Red, 3), 0, 0, 200, 100);

    // Release handle to device context and dispose of the      // Graphics object
    e.Graphics.ReleaseHdc(hdc);
    newGraphics.Dispose();
}
<System.Security.Permissions.SecurityPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, Flags := _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Private Sub FromHdcHdc(ByVal e As PaintEventArgs)

    ' Get handle to device context.
    Dim hdc As IntPtr = e.Graphics.GetHdc()

    ' Create new graphics object using handle to device context.
    Dim newGraphics As Graphics = Graphics.FromHdc(hdc)

    ' Draw rectangle to screen.
    newGraphics.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100)

    ' Release handle to device context and dispose of the Graphics 	' object
    e.Graphics.ReleaseHdc(hdc)
    newGraphics.Dispose()
End Sub

注解

应始终调用 Dispose 方法以释放 Graphics 方法 FromHdc 创建的 和相关资源。

即使显示设备具有关联的 ICM 颜色配置文件,GDI+ 默认情况下也不会使用该配置文件。 若要为 启用 ICM, Graphics请在将 HDC (并ICM_ON) 传递给函数后,从 HDC 构造 GraphicsSetICMMode 。 然后, Graphics 将根据与显示设备关联的 ICM 配置文件调整 由 完成的任何绘图。 启用 ICM 将导致性能降低。

调用 FromHdc 时 (映射模式、逻辑单元等) 的设备上下文状态可能会影响 由 完成的 Graphics呈现。

适用于

FromHdc(IntPtr, IntPtr)

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

从设备上下文的指定句柄和设备的句柄创建新的 Graphics

public:
 static System::Drawing::Graphics ^ FromHdc(IntPtr hdc, IntPtr hdevice);
public static System.Drawing.Graphics FromHdc (IntPtr hdc, IntPtr hdevice);
static member FromHdc : nativeint * nativeint -> System.Drawing.Graphics
Public Shared Function FromHdc (hdc As IntPtr, hdevice As IntPtr) As Graphics

参数

hdc
IntPtr

nativeint

设备上下文的句柄。

hdevice
IntPtr

nativeint

设备的句柄。

返回

此方法为指定的设备上下文和设备返回新的 Graphics

注解

应始终调用 Dispose 方法以释放 Graphics 方法 FromHdc 创建的 和相关资源。

即使显示设备具有关联的 ICM 颜色配置文件,GDI+ 默认情况下也不会使用该配置文件。 若要为 启用 ICM, Graphics请在将 HDC (并ICM_ON) 传递给函数后,从 HDC 构造 GraphicsSetICMMode 。 然后, Graphics 将根据与显示设备关联的 ICM 配置文件调整 由 完成的任何绘图。 启用 ICM 将导致性能降低。

调用 FromHdc 时 (映射模式、逻辑单元等) 的设备上下文状态可能会影响 由 完成的 Graphics呈现。

设备句柄通常用于查询特定打印机功能。

适用于