Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6867195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:16:45+00:00 2026-05-27T03:16:45+00:00

I’m still learning the basics and I’m making a Random Character Generator that creates

  • 0

I’m still learning the basics and I’m making a “Random Character Generator” that creates basic stats for the ‘World’s dullest hero’.

I’ve inlcuded an Image box (named charbox) which, on character generation, is supposed to display one of three .jpg files.

I have added all 3 jpgs to my solution (they’re all listed in the images folder of the Solution Explorer) but I can’t get the button to pick one to display.

I’m using a switch (which may very well have a completely wrong syntax) which should use a random integer I’ve declared as “img” with a range of (0,3).

Here’s the code, which I pulled from another site, but it doesn’t work in my program:

switch (img)
        {
            case 1:
                charbox.Source = new BitmapImage(new Uri(@"C:\Users\Otis\documents\visual studio 2010\Projects\WpfApplication2\WpfApplication2\Images\guy1.jpg", UriKind.Relative));
                break;
            case 2:
                charbox.Source = new BitmapImage(new Uri(@"C:\Users\Otis\documents\visual studio 2010\Projects\WpfApplication2\WpfApplication2\Images\guy2.jpg", UriKind.Relative));
                break;
            default:
                charbox.Source = new BitmapImage(new Uri(@"C:\Users\Otis\documents\visual studio 2010\Projects\WpfApplication2\WpfApplication2\Images\guy3.jpg", UriKind.Relative));
                break;
        }

Any idea how I can make this work?

-Edit-

I have done some looking, and changed my image selection code to:

#region Image Selection

        bmguy.BeginInit();


        switch (img)
        {
            case 0:
                bmguy.UriSource = new Uri(@"C:\Users\Otis\Pictures\guy1.jpg");
                break;
            case 1:
                bmguy.UriSource = new Uri(@"C:\Users\Otis\Pictures\guy2.jpg");
                break;
            case 2:
                bmguy.UriSource = new Uri(@"C:\Users\Otis\Pictures\guy3.jpg");
                break;
        }

        bmguy.EndInit();
        charbox.Source = bmguy;
        #endregion

Where bmguy is a bitmapimage declared as a global variable and switch (img) still uses a 1-3 random number.

So, now when I click generate the image loads guy1.jpg. But it’s always guy1.jpg and if I click generate a second time it throws up an error about bitmap initialisation only occuring once.

Should I be using three seperate bitmapimages or can I use the one and change the file path as I attemped in my switch?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:16:46+00:00Added an answer on May 27, 2026 at 3:16 am
            Random r = new Random();
            int rand = r.Next(0, 3);
            switch (rand)
            {
                case 0:
                    charbox.Load("image1");
                    break;
                case 1:
                    charbox.Load("image2");
                    break;
                case 2:
                    charbox.Load("image3");
                    break;
            }
    

    You’re trying to reinitialize the bitmap every time. Just assign it to the picture box the first time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.