次の方法で共有


Graphics.FromImage メソッド

指定した Image オブジェクトから新しい Graphics オブジェクトを作成します。

Public Shared Function FromImage( _
   ByVal image As Image _) As Graphics
[C#]
public static Graphics FromImage(Imageimage);
[C++]
public: static Graphics* FromImage(Image* image);
[JScript]
public static function FromImage(
   image : Image) : Graphics;

パラメータ

  • image
    新しい Graphics オブジェクトの作成元の Image オブジェクト。

戻り値

このメソッドは、指定した Image オブジェクトの新しい Graphics を返します。

解説

イメージがインデックス付きのピクセル形式の場合、このメソッドは "インデックス付きのピクセル形式をもつイメージから Graphics オブジェクトを作成することはできません。" というメッセージと共に例外をスローします。インデックス付きのピクセル形式の一覧を次に示します。

  • PixelFormat.Format1bppIndexed
  • PixelFormat.Format4bppIndexed
  • PixelFormat.Format8bppIndexed

このメソッドは、イメージが次のピクセル形式の場合にも例外をスローします。

  • PixelFormat.Undefined
  • PixelFormat.DontCare
  • PixelFormat.Format16bppArgb1555
  • PixelFormat.Format16bppGrayScale

使用例

[Visual Basic, C#] 次の例は、Windows フォームでの使用を意図してデザインされており、 Paint イベント ハンドラのパラメータである PaintEventArgs e が必要です。このコードでは次のアクションを実行します。

  • この例が保存されているフォルダのグラフィックス ファイル SampImag.jpg から Image オブジェクトを作成します。
  • Image オブジェクトから Graphics オブジェクトを作成します。
  • 内部の四角形を塗りつぶしてイメージを変更します。
  • 画面に Image オブジェクトを描画します。
  • 作成された Graphics オブジェクトを解放します。
 
Public Sub FromImageImage(e As PaintEventArgs)
' Create image.
Dim imageFile As Image = Image.FromFile("SampImag.jpg")
' Create graphics object for alteration.
Dim newGraphics As Graphics = Graphics.FromImage(imageFile)
' Alter image.
newGraphics.FillRectangle(New SolidBrush(Color.Black), _
100, 50, 100, 100)
' Draw image to screen.
e.Graphics.DrawImage(imageFile, New PointF(0F, 0F))
' Dispose of graphics object.
newGraphics.Dispose()
End Sub
        
[C#] 
public void FromImageImage(PaintEventArgs e)
{
// Create image.
Image imageFile = Image.FromFile("SampImag.jpg");
// Create graphics object for alteration.
Graphics newGraphics = Graphics.FromImage(imageFile);
// Alter image.
newGraphics.FillRectangle(new SolidBrush(Color.Black), 100, 50, 100, 100);
// Draw image to screen.
e.Graphics.DrawImage(imageFile, new PointF(0.0F, 0.0F));
// Release graphics object.
newGraphics.Dispose();
}
        

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Graphics クラス | Graphics メンバ | System.Drawing 名前空間