画面の幅と高さを取得する (プライマリスクリーンの解像度の取得)

C#で画面の解像度(スクリーンの幅と高さ)を取得するコードを紹介します。

コード

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScreenImageCapture
{
  public partial class FormMain : Form
  {
    public FormMain()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      int screen_width = Screen.PrimaryScreen.Bounds.Width;
      int screen_height = Screen.PrimaryScreen.Bounds.Height;

      textBox_Output.Text += string.Format("スクリーンの幅:{0:d}\r\n",screen_width);
      textBox_Output.Text += string.Format("スクリーンの高さ:{0:d}\r\n", screen_height);
    }
  }
}

解説

スクリーンの幅と高さはScreen.PrimaryScreen.Boundsプロパティに格納されています。名前からもわかるとおり複数のディスプレイがある場合は1番目のディスプレイに関する情報が格納されています。BoundsプロパティのWidthプロパティで幅を、Heightプロパティで高さを取得しています。取得した値をテキストボックスに表示します。

実行結果

プログラムを実行しButton1をクリックするとテキストボックスに画面の解像度が表示されます。
画面の幅と高さを取得する (プライマリスクリーンの解像度の取得):画像1

AuthorPortraitAlt
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
作成日: 2012-01-29
Copyright © 1995–2025 iPentec all rights reserverd.