Results 1 to 5 of 5

Thread: Trouble displaying an image in a picturebox, c#

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    19

    Trouble displaying an image in a picturebox, c#

    Good afternoon. This code is very short and simple, but I cannot get it to do what I want.

    Brief description of project:
    This code is for a "smart" picture frame which will rotate between 841 images periodically, at random, on its own.

    Problem description:
    I have 841 images, labeled "Image_1" through "Image_841" in a folder called "Images." This folder is a resource in a c# project. I also have a picturebox (picturebox1) which can display these images. If I execute the following code:

    pictureBox1.Image = Properties.Resources.Image_358;

    Image_358 does indeed appear in the picturebox. So far so good.

    The problem I am having is that I want these images to be selected at random, so I tried some random generator code:

    int random_number = new Random().Next(1, 841); // Generates a number between 1 to 841
    string fileappend = random_number.ToString();
    string newimagefile = "Properties.Resources." + "Image_" + fileappend;
    pictureBox1.Image = newimagefile;

    This code fails, because it cannot convert a string (newimagefile) to an image (I believe). Clearly, what I am trying to do here is use the successful operator
    pictureBox1.Image =Properties.Resources.Image_358; (although 358 could be any number 1-841), and just use random numbers to re-build the successful operator, but c# is not having it. Put another way, I can hardcode

    pictureBox1.Image = Properties.Resources.Image_358;

    and it will work. But if 358 happened to be the randomly generated number random_number, I cannot append this number to the previous string "Properties.Resources.Image_", store in a variable (say filepath), and then execute pictureBox1.Image = filepath;

    Can anyone tell me how to get around this little difficulty?

  2. #2
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    557

    Re: Trouble displaying an image in a picturebox, c#

    Load image using Image.FromFile:
    C# Code:
    1. pictureBox1.Image = Image.FromFile(filename);

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Trouble displaying an image in a picturebox, c#

    Quote Originally Posted by peterst View Post
    Load image using Image.FromFile:
    C# Code:
    1. pictureBox1.Image = Image.FromFile(filename);
    Nope, don't do that. These images are being loaded from resources, so there is no file.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Trouble displaying an image in a picturebox, c#

    You can load a resource by name using the ResourceManager. What you should do is create a Dictionary to store Image objects by number. Each time you generate a random number, go to the Dictionary and see if there is an Image for that number. If there is, load it into the PictureBox. If there isn't, get it from resources, add it to the Dictionary and load it into the PictureBox. The point of the Dictionary is to prevent you getting the same Image from resources multiple times. The code might look like this:
    csharp Code:
    1. private readonly Random rng = new Random();
    2. private readonly Dictionary<int, Image> imagesByNumber = new Dictionary<int, Image>();
    3.  
    4. private void LoadRandomResourceImage()
    5. {
    6.     var number = rng.Next(1, 841 + 1);
    7.  
    8.     pictureBox1.Image = GetResourceImageByNumber(number);
    9. }
    10.  
    11. private Image GetResourceImageByNumber(int number)
    12. {
    13.     if (imagesByNumber.TryGetValue(number, out var img))
    14.     {
    15.         return img;
    16.     }
    17.  
    18.     img = (Image)Properties.Resources.ResourceManager.GetObject($"Image_{number}");
    19.     imagesByNumber.Add(number, img);
    20.  
    21.     return img;
    22. }

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    19

    Re: Trouble displaying an image in a picturebox, c#

    I'm intrigued by this solution, will try it tomorrow. I should also note it will be executed by an RPi4, though hopefully that won't be too difficult--they support visual studio code and c# in particular

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width