mercredi 31 juillet 2019

How can I display an image embedded in my program using the value from a random number generator?

I'm trying to build a Russian Roulette style program where you click a button and it randomly selects one of 25 images to display on screen but I can't seem to figure out how to call the images using the generator.

It works fine when I select an image manually, as seen in my code below, but anything else seems to return an error.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label_click(object sender, EventArgs e)
        {
            Close();
        }

        int mouseX = 0, mouseY = 0;
        bool mouseDown;



        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            mouseDown = false;
        }

        private void GOBUTN_Paint(object sender, PaintEventArgs e)
        {



        }
        //Sharing is caring: Communism goes here
        private System.Windows.Forms.Timer timtim;
        int rand0;
        PictureBox Rooskie = new PictureBox();
        Label test = new Label();
        Random rando = new Random();
        List<int> duplicheck = new List<int>();


        private void boopthesnoot(object sender, EventArgs e)
        {
        dingding:
            //Hell yeah, random numbers here
            rand0 = rando.Next(1, 26);



            /*string combowombo = string.Join(", ", duplicheck.ToArray());
            test.Text = combowombo;
            test.Font = new Font("Calibri", 20);
            Controls.Add(test);
            test.Location = new Point(0, 200);
            test.Height = 1000;
            test.Width = 1000;*/
            if(duplicheck.Contains(rand0))
            {
                goto dingding;
            }
            else
            {
                GOBUTTON.Hide();
                pictureBox1.Hide();
                pictureBox2.Hide();

                //Fuckin image code goes here my dood
                Rooskie.Width = 1160;
                Rooskie.Height = 620;
                Bitmap image = new Bitmap(WindowsFormsApp1.Properties.Resources._1);
                Rooskie.Dock = DockStyle.Fill;
                Rooskie.Image = (Image)image;
                Controls.Add(Rooskie);
                Rooskie.SizeMode = PictureBoxSizeMode.CenterImage;


                //Aww shit, it's that timer time
                timtim = new System.Windows.Forms.Timer();
                timtim.Tick += new EventHandler(clockfinish);
                timtim.Interval = 3000;
                timtim.Start();
                duplicheck.Add(rand0);
                if (duplicheck.Count == 25)
                {
                    duplicheck = new List<int>();
                }
            }


        }

        private void clockfinish(object sender, EventArgs e)
        {
            //CEASE THE TIMER AND GIVE ME BACK MY BUTTON
            Rooskie.Image = null;
            timtim.Stop();
            GOBUTTON.Show();
            pictureBox1.Show();
            pictureBox2.Show();
        }

The expected result is when the user presses the button it calls up the image without having to load it from a folder.




Aucun commentaire:

Enregistrer un commentaire