C# .NET - WinForm - WinForm实例
WinFrom打开图片,查看图片信息
效果图如下:



WinFrom代码(.NET 7):
using System.Windows.Forms;


namespace HtWinImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.AutoScroll = true;
Text = "何问起图片";

panel1.Anchor= AnchorStyles.Left|AnchorStyles.Right| AnchorStyles.Top| AnchorStyles.Bottom;
panel1.AutoScroll= true;

button1.Text = "打开图片";

pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.BorderStyle = BorderStyle.FixedSingle;

textBox1.ReadOnly= true;
textBox1.ScrollBars = ScrollBars.Vertical;
textBox1.Anchor= AnchorStyles.Right;

}
string h_imageInfo="";

private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{


if (IsRealImage(openFileDialog1.FileName))
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
h_imageInfo= "宽度:"+ pictureBox1.Image.Width.ToString()
+"\r\n高度:"+pictureBox1.Image.Height.ToString()
+ "\r\n类型:" + pictureBox1.Image.RawFormat.ToString()
+"\r\n文件名:"+ openFileDialog1.FileName
;
textBox1.Text = h_imageInfo;
}
else
{
MessageBox.Show("您打开的不是图片文件");
textBox1.Text = "";
pictureBox1.Image = null;
}
}
}

/// <summary>
/// 判断文件是否是真正的图片。
/// </summary>
/// <param name="path">文件路径</param>
/// <returns></returns>
static bool IsRealImage(string path)
{
try
{
Image img = Image.FromFile(path);
return true;
}
catch (Exception e)
{
return false;
}
}
}
}
namespace HtWinImage
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.panel1 = new System.Windows.Forms.Panel();
this.textBox1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(15, 15);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(502, 323);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 10);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// panel1
//
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(12, 42);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(540, 396);
this.panel1.TabIndex = 2;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(558, 42);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(230, 257);
this.textBox1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private PictureBox pictureBox1;
private Button button1;
private OpenFileDialog openFileDialog1;
private Panel panel1;
private TextBox textBox1;
}
}
收藏 列表

评论:

导航