Share via


方法 : フォームに背景イメージを設定します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

フォームの背景としてイメージを描画するフォームの OnPaint メソッドをオーバーライドすることができます。

フォーム上に背景イメージを描画するには

  1. フォームの OnPaint メソッドをオーバーライドします。

  2. デバイス上またはアセンブリに埋め込まれたリソースとして外部ファイルからイメージを取得します。

  3. Graphics Graphics プロパティから PaintEventArgs オブジェクトにイメージを描画するのに使用します。 フォームの ClientRectangle プロパティによって指定された寸法を使用します。

使用例

次の使用例では、フォームの背景イメージ、埋め込みリソースとしてコンパイルされたイメージファイルを使用します。 Visual Studioでプロジェクトに追加する、背景イメージと、 のソリューション エクスプローラーで、埋め込まれたリソース に、ビルド アクション プロパティを設定する必要があります。

注意

また、Visual Studio 内のプロジェクト リソースとしてイメージを含めるでき、コード内で名前で、リソースを参照できます。詳細については、「リソースの追加と編集 (Visual C#)」または「方法 : Visual Basic でのイメージ リソースを取得します。」を参照してください。

                        Protected
                        Overrides
                        Sub OnPaint(e As PaintEventArgs)

    ' For an image added as a project resource in Visual Studio,    ' get the resource by name:    ' Bitmap backgroundImage = My.Resources.mypicture;     ' Otherwise, get the image compiled as an embedded resource.Dim asm As Assembly = Assembly.GetExecutingAssembly()
    Dim backGroundImage AsNew Bitmap(asm.GetManifestResourceStream("mypicture.bmp"))

    e.Graphics.DrawImage(backgroundImage, Me.ClientRectangle, _
        New Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height), _
        GraphicsUnit.Pixel)

EndSub
                        protected
                        override
                        void OnPaint(PaintEventArgs e)
{
    // For an image added as a project resource in Visual Studio,// get the resource by name:// Bitmap backgroundImage = Properties.Resources.mypicture; // Otherwise, get the image compiled as an embedded resource.
    Assembly asm = Assembly.GetExecutingAssembly();
    Bitmap backgroundImage = new Bitmap(asm.GetManifestResourceStream("mypicture.jpg"));

    e.Graphics.DrawImage(backgroundImage, this.ClientRectangle,
        new Rectangle(0,0, backgroundImage.Width, backgroundImage.Height),
        GraphicsUnit.Pixel);
}

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

参照

概念

カスタム コントロールの開発

その他の技術情報

Windows フォーム コントロール、.NET Framework を最適化します。