Reputation: 117
I'm unsure about the right method to define some rules for a specific image in one of my user resource folders. I allow one image in this folder with a specific name, but this single image can be a PNG / JPEG / BMP.
What could be a suitable route for loading the image as BackgroundImage, when the current code setup for loading the image would be something like this:
private void Form1_DragDrop(object sender, DragEventArgs e)
{
var data = e.Data.GetData(DataFormats.FileDrop);
if (data != null)
{
var fileNames = data as string[];
if (fileNames.Length > 0)
BackgroundImage = Image.FromFile(fileNames[0]);
BackgroundImage = Image.FromFile(@"image_default\image_default.jpg");
}
Upvotes: 0
Views: 67
Reputation: 55
If I understand the question correctly you will have an array of filenames without extensions and you want to find that file in the directory? You could get an Array of the files in that directory, then find the matching filename (without extension) and load from that.
var dir = System.IO.Directory.GetFiles(@"image_default\");
var imageFile = string.Empty;
foreach (string file in dir) {
if (System.IO.Path.GetFileName(file).Equals(fileNames[0]))
imageFile = file;
}
if (imageFile != null)
BackgroundImage = Image.FromFile(imageFile);
else
//some error or handle
There might be some more clever way of searching the directory without iterating through the array, but you get the idea...
Upvotes: 0