将图像添加到 C# 中的对象列表

我正在尝试在 C# 中创建图像列表。我的问题是我不确定如何引用图像以将其添加到列表中。我已经尝试在括号中添加文件路径,但它抛出错误“无法创建抽象类或接口‘Image’的实例”。下面是我尝试过的示例。


我对此很陌生,所以如果有明显的错误或解决方案,我深表歉意。谢谢!:)


private void button1_Click(object sender, EventArgs e)

{

    imageList = new List<Image>();

    imageOne = new Image("image.jpg");

}


忽然笑
浏览 301回答 2
2回答

慕妹3242003

Image是一个抽象类。这意味着你不能new它尝试从文件imageOne&nbsp;=&nbsp;Image.FromFile("image.jpg");它返回一个Image对象,您可以将其添加到列表中

婷婷同学_

如果要从文件加载图像,可以使用FromFile 方法。来自 msdn 的例子:private void Button2_Click(System.Object sender, System.EventArgs e){&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Bitmap image1 = (Bitmap) Image.FromFile(@"C:\Documents and Settings\" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @"All Users\Documents\My Music\music.bmp", true);&nbsp; &nbsp; &nbsp; &nbsp; TextureBrush texture = new TextureBrush(image1);&nbsp; &nbsp; &nbsp; &nbsp; texture.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;&nbsp; &nbsp; &nbsp; &nbsp; Graphics formGraphics = this.CreateGraphics();&nbsp; &nbsp; &nbsp; &nbsp; formGraphics.FillEllipse(texture,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new RectangleF(90.0F, 110.0F, 100, 100));&nbsp; &nbsp; &nbsp; &nbsp; formGraphics.Dispose();&nbsp; &nbsp; }&nbsp; &nbsp; catch(System.IO.FileNotFoundException)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("There was an error opening the bitmap." +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Please check the path.");&nbsp; &nbsp; }}如果要从流加载图像,也可以使用FromStream 方法。
打开App,查看更多内容
随时随地看视频慕课网APP