无法隐式将位图转换为图像或将图像转换为图像

时间:2019-05-12 12:57:11

标签: c# visual-studio

我刚刚编写了此代码以访问图像

private Bitmap[] hi = { HangmanUrdu.Properties.Resources._4, HangmanUrdu.Properties.Resources._5, HangmanUrdu.Properties.Resources._6, HangmanUrdu.Properties.Resources._7, HangmanUrdu.Properties.Resources._8, HangmanUrdu.Properties.Resources._9, HangmanUrdu.Properties.Resources._10 };

但是当我想增加索引并在图片框中获取这些图像

// wg is just a counter;
pictureBox1.Image = hi { wg}; i

t让我说一个错误

  

无法将位图隐式转换为图像

我还尝试将数组从位图更改为图像,但随后显示了无法将图像转换为图像的错误。

1 个答案:

答案 0 :(得分:0)

在此处创建一个 Cannot deserialize value of type `java.util.Date` from String "2001-01": not a valid representation (error: Failed to parse Date value '2001-01': Cannot parse date "2001-01": while it seems to fit format 'yyyy-MM-dd', parsing fails (leniency? null)) -作为字段-或适合设计的任何其他类型(例如,类属性)。
用该上下文中需要的Bitmap对象填充List<Bitmap>在表单的构造函数中,从资源对象创建一个新的Bitmap:

List<Bitmap>

需要时,将位图分配给控件的Image属性:

private List<Bitmap> hi = null;

public Form1()
{
    InitializeComponent();

    this.hi = new List<Bitmap>()
    {
        new Bitmap(Properties.Resources._4),
        new Bitmap(Properties.Resources._5)
    };
}

您还可以构建一个专门的类来保存这些引用,以便可以使用不同的命名约定访问它们。
例如:

pictureBox1.Image = hi[1];

然后,在需要时:

按索引:

private List<BitmapResource> BitmapResources = null;

public Form1()
{
    InitializeComponent();

    this.BitmapResources = new List<BitmapResource>()
    {
        new BitmapResource(new Bitmap(Properties.Resources._4), "Logo"),
        new BitmapResource(new Bitmap(Properties.Resources._5), "Watermark")
    };
}

internal class BitmapResource
{
    public BitmapResource(Bitmap bitmap, string imageName)
    {
        this.Image = bitmap;
        this.Name = imageName;
    }
    public Bitmap Image { get; private set; }
    public string Name { get; private set; }
}

按名称(简体):

pictureBox1.Image = BitmapResources[0].Image;
相关问题