Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A single image can be initially loaded from an application subfolder into a Windows form to appear as the BackgroundImage/PictureBox Image by specifying name and image format by file extension.
However I can't figure out a way to support different image formats with identical names and effectively handle multiple images in my subfolder which have the same name, but different image file extensions.
I want that any supported Bitmap image called bg_default(*.*) will be loaded initially as BackgroundImage if there is a single image with this name in the subfolder and that one image will be picked randomly if there are multiple images in the subfolder like this:

app1.exe
default_01(folder):
bg_default.png
bg_default.gif
bg_default.bmp
bg_default.tiff
bg_default.jpeg

I saw many methods to modify path expressions and break things apart, but unfortunately there seems no way to define the name of an image and specify different file extensions for it in one path expression.
On the other side it seems that I must specify an extension for an image.
How could I possibly solve this?

What I have tried:

I tried everything I can imagine with my basic c# skills.
Posted
Updated 26-Oct-19 8:23am
v2

1 solution

Directory.GetFiles Method (System.IO) | Microsoft Docs[^]
With Directory.GetFiles (string, string) you get all the files in the specified directory matching the specified pattern, the return is a string array.
C#
string[] Images = System.IO.Directory.GetFiles (Path, @"bg_default*");
Checking if one or more images in the array and picking a "random" one of the array, should be easy.

Then you might set that as background.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900