what I intend to do is
PictureBox1.Image = ImageHere
But I am not sure how I would go about this on mass other than then
PictureBox2.Image = ImageHere2
PictureBox3.Image = ImageHere3
PictureBox4.Image = ImageHere4
If I could just do [increment] or something but that gets rejected
If you were to put the picture boxes and the images into arrays or the likes of it, you could loop like this:
PictureBox[] pictureBoxes = { PictureBox1, PictureBox2, PictureBox3, PictureBox4 };
Image[] images = { ImageHere1, ImageHere2, ImageHere3, ImageHere4 };
for (int i = 0; i < pictureBoxes.Length; i++)
{
pictureBoxes[i].Image = images[i];
}
There's still the hassle of creating the arrays though.
You can dynamically create your pictureboxes and assign them corresponding values.
Create a list of images and use foreach iterator:
List<Image> images = //get your list of images
foreach(var img in images)
{
PictureBox pb = new PictureBox();
pb.Image = img;
YourControl.Add(pb);
}
Related
In front end, I have 100 images - Image1, Image2 ... Image100. Now I need to modify some of their source. For example, in back end:
Image1.Source = new BitmapImage(new Uri(dir));
However, I need to click a button to make these 100 images'source to be the same one. The following code is incorrect but shows what I need:
for (int i = 1; i <= 100; i++)
{
("Image"+i.ToString()).Source=new BitmapImage(new Uri(dir));
}
What is the correct way to write it? Thanks in advance.
If you put your images in a grid you could then loop through all the controls in a grid, if its an Image (You'll know because its not null) set the source.
foreach(var img in grid.Children) {
var image = img as Image;
if(image != null) image.Source = new BitmapImage(new Uri(dir));
}
Edit --
Thanks. But I also have another request like randomly choose 50 of
these 100 images and set their source to be another image - this is
why I think I need to indicate each of them in the back end.
You could do something like this...
//Get Images and add them to a list.
List<Image> images = new List<Image>();
foreach(var img in grid.Children) {
var image = img as Image;
images.Add(image);
}
Random random = new Random();
//Loop 50 times and pick a random image to change.
for(int i = 0; i < 50; i++) {
int r = random.Next(99);
images[r].Source = new BitmapImage(new Uri(dir));
}
You'll probably also want to check if the image has already randomly been chosen or not... I'll let you figure that out on your own. It's easy enough.
I would like to display 13 pictureBox, however, it ends up with only the last one visible.
So I was wondering if I did it in a wrong way.
The following code get image from resources folder.
var testP = new PictureBox();
for (int i = 0; i < 13; i++)
{
testP.Width = 65;
testP.Height = 80;
testP.BorderStyle = BorderStyle.None;
testP.SizeMode = PictureBoxSizeMode.StretchImage;
test[i] = getImage(testP, testPTemp[i]);
}
The following code is trying to display 13 pictureBox with shifting location.
These two codes segments should be able to perform the action.
test = new PictureBox[13];
for (var i = 0; i < 13; i++)
{
test[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + testTemp[i]);
test[i].Left = 330;
test[i].Top = 500;
test[i].Location = new Point(test[i].Location.X + 0 * displayShift, test[i].Location.Y);
this.Controls.Add(test[i]);
}
Here is the getImage()
private PictureBox getImage(PictureBox pB, string i) // Get image based on the for loop number (i)
{
pB.Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + i); // Get the embedded image
pB.SizeMode = PictureBoxSizeMode.StretchImage;
return pB;
}
I'm pretty sure there are all PictureBox Controls but they have all the same location so they are lying above each other. That's why only the last one is visible to you.
I think you should replace the 0 with the i variable.
test[i].Location = new Point(test[i].Location.X + i * displayShift, test[i].Location.Y); this.Controls.Add(test[i]);
It's hard to tell the exact problem based off the code you've provided. One possible issue could be that when you are creating the PictureBoxes you only create a single instance before the for loop and then fill the array with references to that instance. Another possibility is that when you're calculating the X position of the controls, you're multiplying by 0 which will always result in 0 (meaning all the controls are at location 330).
Below is code that will achieve basically what you're trying but without all your code I can't give you a more specific example.
In Your Class
const int PICTURE_WIDTH = 65;
const int PICTURE_HEIGHT = 85;
Inside You Function
//Loop through each image
for(int i = 0; i < testTemp[i].length; i++)
{
//Create a picture box
PictureBox pictureBox = new PictureBox();
pictureBox.BorderStyle = BorderStyle.None;
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
//Load the image date
pictureBox.Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + testTemp[i]);
//Set it's size
pictureBox.Size = new Size(PICTURE_WIDTH, PICTURE_HEIGHT);
//Position the picture at (330,500) with a left offset of how many images we've gone through so far
pictureBox.Location = new Point(330 + (i * PICTURE_WIDTH), 500);
//Add the picture box to the list of controls
this.Controls.Add(pictureBox);
}
If you need to keep a list of the picture boxes, just create a new list before the loop and add each pictureBox to the list inside the loop. If the control/window you're adding these PictureBoxes to needs to scroll left or right to see all the images set the AutoScroll property to true.
I was able to add image to my panel with the following code,
But now my problem is I need to retrieve some information from the image etc(key value)
Is that possible to add in a value to every image?
foreach (System.Drawing.Image img in imagelist) {
PictureBox imagePicBox = new PictureBox();
imagePicBox.Name = "image" + imagePanel.Controls.Count.ToString();
imagePicBox.Image = img;
imagePicBox.Size = imagePicBox.Image.Size;
imagePicBox.BackColor = Color.Black;
imagePicBox.SizeMode = PictureBoxSizeMode.StretchImage;
imagePicBox.BorderStyle = BorderStyle.FixedSingle;
imagePanel.Controls.Add(imagePicBox);
}
You can use the Tag property of the System.Drawing.Image to add whatever data you want. this is what it's there for.
I need your help, I'm using this code to generate multiple tabs in a TabControl each containing a PictureBox with a different JPG picture:
foreach (var file in d.GetFiles("*.jpg"))
{
string title = file.Name + (TCFichiers.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
TCFichiers.TabPages.Add(myTabPage);
PictureBox i = new PictureBox();
myTabPage.Controls.Add(i);
}
I want to use a button that will rotate the image in the PictureBox of the selected tab however I can't figure out how to get access to the right PictureBox. How can I access only the picturebox on the selected tab?
Thanks
See this:
PictureBox[] Shapes = new PictureBox[Num_Picbox];
for (int i = 0; i < Num_Picbox; i++)
{
Shapes[i] = new PictureBox();
Shapes[i].Name = "ItemNum_" + i.ToString();
Shapes[i].Location = new Point(label5.Left+1,label5.Top);
Shapes[i].Size = new Size(100, 100);
Shapes[i].BackColor = Color.Black;
Shapes[i].Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));
Shapes[i].Visible = true;
this.Controls.Add(Shapes[i]);
}
In my project I have collection of PictureBoxes and a filled ImageList. I want to populate each pictureBox with the each Image in ImageList using foreach Loop. I know how to do using For loop but I dont know how to do it using foreach loop. I am asking this just for knowledge purpose. I think this can be achieved using Linq inside foreach loop but I am a beginner so I have no Idea how to do it.
I tried the below code in for loop:
for (intimgcount = 0; intimgcount < intMaxPics; intimgcount++)
{
pbxCollection[intimgcount].Image = imglst.Images[intimgcount];
}
the code which I want to use in foreach loop is:
var pbxCollection = new List<PictureBox>(); //PictureBox collection
EDIT: How to set the positions of Picture Box collection in the form?
I tried:
var i = 0;
foreach (var pbx in pbxCollection)
{
pbx.Image = imglst.Images[i++];
//set location:
pbx.Width = 100;
pbx.Height = 100;
pbx.Location = new Point(0, pbx.Height * i);
//add to form:
this.Controls.Add(pbx);
}
You can do it this way.
var pbxCollection = new List<PictureBox>();
foreach (Image img in imglst.Images)
{
PictureBox pb = new PictureBox();
pb.Image = img;
pbxCollection.Add(pb);
}
Use a variable to increment index of collection:
var i = 0;
foreach (var pbx in pbxCollection)
{
pbx.Image = imglst.Images[i++];
//set location:
pbx.Location = new Point(0, pbx.Image.Height * i);
//add to form:
this.Controls.Add(pbx);
}