【问题标题】:getting a value from an image in C#从 C# 中的图像中获取值
【发布时间】:2018-02-13 00:07:09
【问题描述】:

我正在创建一个项目,当我单击按钮时会显示随机图像,并且必须猜测图像(骰子)的总数。我已经记录了随机图像生成并跟踪人员的滚动数量。然而;我不知道如何使骰子(图像)具有一定的价值。就像骰子 5 显示的一样,它的值为 4。这个人将猜测放在guessBx 中,然后单击guessBtn,如果它们正确或不正确,它就会弹出。

这是我现在的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PetalsAroundTheRose
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
    }
    int a = 0;
    int diceRoll;
    int diceImage;
    int diceValue;

    private void btnRoll_Click_1(object sender, EventArgs e)
    {
        Random random = new Random();
        picBx1.Image = imageList1.Images[random.Next(1, 6)];
        picBx2.Image = imageList1.Images[random.Next(1, 6)];
        picBx3.Image = imageList1.Images[random.Next(1, 6)];
        picBx4.Image = imageList1.Images[random.Next(1, 6)];

        a++;
        txtBxRolls.Text = a.ToString();

        //each dice seperate
        //dice 1
        diceRoll = 1;
        diceImage = imageList1.Images[(1)];
        diceValue = 0;

        //dice 2
        diceRoll = 1;
        diceImage = imageList1.Images[(2)];
        diceValue = 0;

        //dice 3
        diceRoll = 1;
        diceImage = imageList1.Images[(3)];
        diceValue = 2;

        //dice 4
        diceRoll = 1;
        diceImage = imageList1.Images[(4)];
        diceValue = 0;

        //dice 5
        diceRoll = 1;
        diceImage = imageList1.Images[(5)];
        diceValue = 4;
    }

    private void guessBx_TextChanged_1(object sender, EventArgs e)
    {
}
}
}

在我的设计中,我使用 btnRoll 来掷骰子。 guessBx 用于输入猜测,btnGuess,txtBxCorrect 用于输入正确的数量,txtBxResult 用于说明它们是否正确

图像在 imageList 中

【问题讨论】:

  • 你能解释一下吗。图像是骰子每一面的图像吗?并且您要检查输入的图像和数字是否相同?
  • 是的,每个图像都是模具的一面。图像“滚动”后,可以检查显示的图像值是否正确。类似于花瓣的是玫瑰游戏。
  • 从 (1-7] 中选择一个随机数。这将告诉您掷骰子的值。根据它们的列出方式将其用作 ImageList 的索引
  • ...对整个应用程序也使用一个随机实例 - 不要在每次点击时创建一个新实例
  • 不要使用 ImageList,而是使用 Image 和 Int32 作为属性创建一个自定义类,以将图像及其值放入其中,然后将它们的 List 作为随机选择的源。然后你就可以得到所选对象的图像和值。

标签: c# image int


【解决方案1】:

首先我用这两个属性创建一个类

public class Dice
{
    public int Indexer { get; set; }
    public string PhotoPath { get; set; }

    public Dice(int indexer, string photoPath)
    {
        Indexer = indexer;
        PhotoPath = photoPath;
    }
}

之后,我将图片框放入表单中,然后创建了两种方法 第一个:

public void SetUp()
    {         
        for (int i = 1; i <= 6; i++)
        {
            temp = random.Next(1, 6);
            dice = new Die(temp, "C:\\Users\\giorg\\Desktop\\dice\\dice" + temp + ".PNG");
            dices.Add(dice); // list<Dice> dices = new list<Dice>();
        }
    }

第二个:

public  void RollDices()
    {
        //this is not necessary but if you want to keep a sum of dices keep 
        //it
        var count = 0;
        foreach (var dice in dices)
        {
            count += dice.Indexer;
        }
        //pictureboxes 2-7 are guess boxes
        if(textBox2.Text.Equals(dices[0].Indexer.ToString()))
            pictureBox1.Image = Image.FromFile(dices[0].PhotoPath);

        if (textBox3.Text.Equals(dices[1].Indexer.ToString()))
            pictureBox2.Image = Image.FromFile(dices[1].PhotoPath);

        if (textBox4.Text.Equals(dices[2].Indexer.ToString()))
            pictureBox3.Image = Image.FromFile(dices[2].PhotoPath);

        if (textBox5.Text.Equals(dices[3].Indexer.ToString()))
            pictureBox4.Image = Image.FromFile(dices[3].PhotoPath);

        if (textBox6.Text.Equals(dices[4].Indexer.ToString()))
            pictureBox5.Image = Image.FromFile(dices[4].PhotoPath);

        if (textBox7.Text.Equals(dices[5].Indexer.ToString()))
            pictureBox6.Image = Image.FromFile(dices[5].PhotoPath);    
    }

把这两个方法放到你的按钮点击事件中就OK了…… 如果您同意这种方法,请告诉我。

【讨论】:

  • 因为图像不在我的电脑上,它们在图像列表中,所以我不应该为新的骰子... (imageList1.Images)??
  • 为什么你不使用具有值(我的示例中的索引器)和 photopath 的类骰子,然后将“骰子”添加到列表中而不是使用 imageList?有什么原因吗?
猜你喜欢
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 2014-02-05
  • 2019-06-25
  • 1970-01-01
相关资源
最近更新 更多