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をクリックするとテキストボックスに画面の解像度が表示されます。