Reputation: 967
This is how I open a multiple pictures and set to flowLyaoutPanel
DialogResult dr = this.openFileDialog.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
// Read the files
foreach (String file in openFileDialog.FileNames)
{
// Create a PictureBox.
try
{
PictureBox pb = new PictureBox();
Image loadedImage = Image.FromFile(file);
pb.Height = loadedImage.Height;
pb.Width = loadedImage.Width;
pb.Image = loadedImage;
flowLayoutPanel1.Controls.Add(pb);
}
catch (SecurityException ex)
{
MessageBox.Show("Security error. Please contact your administrator for details.\n\n" +
"Error message: " + ex.Message + "\n\n" +
"Details (send to Support):\n\n" + ex.StackTrace
);
}
catch (Exception ex)
{
MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
+ ". You may not have permission to read the file, or " +
"it may be corrupt.\n\nReported error: " + ex.Message);
}
}
}
This is where Im stuck, I need to get the Image in every picturebox by use of looping
foreach (Control ctrl in flowLayoutPanel1.Controls) {
if (ctrl is PictureBox) {
//problem goes here
Image img = (PictureBox)ctrl.Image;
byte[] arr;
ImageConverter converter = new ImageConverter();
arr = (byte[])converter.ConvertTo(img, typeof(byte[]));
}
}
Is there a way to cast the control to picturebox?
Upvotes: 0
Views: 372
Reputation: 2988
Use OfType<T>
to filter a collection down by the type.
using System.Linq;
...
foreach (PictureBox pictureBox in flowLayoutPanel1.Controls.OfType<PictureBox>())
{
Image img = pictureBox.Image;
byte[] arr;
ImageConverter converter = new ImageConverter();
arr = (byte[])converter.ConvertTo(img, typeof(byte[]));
}
Upvotes: 2