我在 C# winform 中尝试了以下代码
private void ComboCar_SelectedIndexChanged(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
if (opf.ShowDialog() == DialogResult.OK)
{
PictureBox.Image = Image.FromFile(opf.FileName);
}
}
但我收到错误
错误 1 非静态字段需要对象引用, 方法或属性"System.Windows.Forms.PictureBox.Image.get"
静态方法是无需创建类对象即可调用的方法。您可以从您想要的地方调用它。示例:String.IsNullOrWhitespace("exampleString")
是一个静态方法。
非静态方法是您必须创建对象实例的方法。这就是您的PictureBox.Image.Get,请检查此示例以进行修复例:
PictureBox box = new PictureBox();
box.Image = Image.FromFile(opf.FileName);
>PictureBox
是窗体上控件的类的名称。例如,您需要在表单上使用实际图片框的名称
pictureBox1.Image = Image.FromFile(opf.FileName);