I’m trying to figure out how to do something with the WInForm PictureBox control.
I’m going to briefly describe how my application is built and then ask my question.
I’m using C# Visual Studio 2012, however the version of Visual Studio probably doesn’t matter.
Create a new WinForms application named “FunWIthPictureBox”. Bring up the Form’s design view.
Add a PIctureBox control and name it pbxPicture.
Click on the picture box. You will notice that on the top right there is a black arrow.
Click on the black arrow to bring up the PictureBox Tasks dialog box. Click on the “Choose Image” option.
This brings up the “Select Resource” dialog box. Make sure the “Project Resource file” radio button is selected. Click on the “Import” button to import an image. When you are finished click “Import” again and add a second image. Finally click “OK”. My pictures are named “Picture1.jpg” & “Picture2.jpg”.
Now you have loaded two bitmap images into the Project’s resource file (Properties\Resources.resx).
Double click on the Form to bring up the Form’s load event.
You can set the Picture Box’s image to Picture1.jpg with the following code… pbxPicture.Image = FunWIthPictureBox.Properties.Resources.Picture1; Please note that “FunWithPictureBox” is the namespace.
And if you want to use the other picture you can update the line of code like this.. pbxPicture.Image = FunWIthPictureBox.Properties.Resources.Picture2;
Ok so far so good. I’m trying to figure out how to extract all of the images that have been loaded into .Properties.Resources and load those images into an Image list (List).
Using Reflection (and Google) I was able to come up with the following..
(Add Using System.Reflection)
private void PictureBoxForm_Load(object sender, EventArgs e)
{
pbxPicture.Image = FunWithPictureBox.Properties.Resources.Picture1;
var properties = typeof(Properties.Resources).GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
foreach (var propertyinfo in properties)
{
if (propertyinfo.PropertyType.ToString() == "System.Drawing.Bitmap")
{
MessageBox.Show(propertyinfo.Name + " " + propertyinfo.PropertyType.ToString());
}
}
}
This almost gives me what I want. I'm able to identify the images from the Resource file.
How can I add these Images to an Image List? I have the name of the image but I don't know how to access the image and add it to a list.
Something like this...
private void PictureBoxForm_Load(object sender, EventArgs e)
{
List<Image> lstImages = new List<Image>();
pbxPicture.Image = FunWithPictureBox.Properties.Resources.Picture1;
var properties = typeof(Properties.Resources).GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
foreach (var propertyinfo in properties)
{
if (propertyinfo.PropertyType.ToString() == "System.Drawing.Bitmap")
{
lstImages.Add(propertyinfo.Name); //This code does not wwork :-(
}
}
}
Is there a way to do this?
Aucun commentaire:
Enregistrer un commentaire