LoginSignup
0
0

More than 1 year has passed since last update.

VisualStudioでフォームに画像を表示する

Posted at

環境
Visual Studio 2015 (Win10標準の.net4.6がデフォルトで使えるため)

手順
1,新規プロジェクトを作る
2,フォームにPanelをドラッグする
image.png

必要であればPanel1のDockをFillにする.
image.png

3,プロパティのイベントからPaintのイベントハンドラをダブルクリックする
image.png

4,作成されたPaintベントハンドラpanel1_Paintにプログラムを記述する

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        if (currentImage != null)
        {
            // 画像を0,0に描画する
            e.Graphics.DrawImage(currentImage,
                0, 0, currentImage.Width, currentImage.Height);
        }
    }

5,フォームクラスのメンバ変数にImageを登録する

public partial class Form1 : Form
{

    //画像ファイルを読み込む
    Image currentImage = Image.FromFile(@"C:\test\test.gif");

    public Form1()
    {

6,実行すると画像が表示できる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0