【C#】コンソールアプリからフォームを起動して画像を表示する
ブログ用のコードはコンソールアプリケーションで書いています。(コードが長くなるので)
これだと画像表示に困るのでフォーム起動して画像を表示するコードを書きました。
画像はファイルから読み込んでいます。
void ImgFileDisplayByForm(string filePath) { if (!System.IO.File.Exists(filePath)) return; var form = new System.Windows.Forms.Form(); //画像ファイルを背景画像に設定 form.BackgroundImage = System.Drawing.Image.FromFile(filePath); //設定済みのフォームを起動 System.Windows.Forms.Application.Run(form); }
使う際は参照の追加でSystem.DrawingとSystem.Windows.Formsを追加して下さい。
usingは使わないほうが無難です。けっこう被るので。
背景画像なのでタイル状に配置されます。
まともに表示したい場合はPictureBoxなどを使ってください。
一応コード置いときます。
static void ImgFileDisplayByForm(string filePath) { if (!System.IO.File.Exists(filePath)) return; var form = new System.Windows.Forms.Form(); var pb = new System.Windows.Forms.PictureBox(); pb.Image = System.Drawing.Image.FromFile(filePath); pb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; form.Controls.Add(pb); //設定済みのフォームを起動 System.Windows.Forms.Application.Run(form); }
しかし今更Form使うと負けた気になるな・・・