C#ShowAcquireImage
时间: 2023-07-12 08:44:00 浏览: 212
您!C#中获取图像的方式有很多种,以下是其中一种示例代码:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ShowAcquireImage
{
public partial class Form1 : Form
{
private PictureBox pictureBox1;
private Button button1;
private OpenFileDialog openFileDialog1;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(12, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(300, 300);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(318, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Open";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(405, 324);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Show and Acquire Image";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
}
}
```
这个示例代码中,我们使用了 `OpenFileDialog` 控件来打开本地的图像文件,并且将图像显示在 `PictureBox` 控件中。当用户单击“Open”按钮时,会弹出一个文件对话框,用户可以选择本地的图像文件。然后,我们使用 `Image.FromFile()` 方法来加载图像文件,并将其赋值给 `PictureBox` 控件的 `Image` 属性,以显示图像。
阅读全文