I tried to add picturebox in windowsform on load event-handler but the images didn't appear in the form after loading
attachimage is a picturebox I added it from toolbox ( not by c# )
private void ViewCmap_Load(object sender, EventArgs e)
{
for (int i = 0; i < ConceptProperties.ConceptsMap.Count; i++)
{
conceptattchboxlist.Add(new PictureBox());
conceptattchboxlist[i].Visible = true;
if (ConceptProperties.ConceptsMap[i].Attachments.Count > 0)
{
PictureBox new_attach_box = new PictureBox();
new_attach_box.Image = attachimage.Image;
new_attach_box.Width = attachimage.Width;
new_attach_box.Height = attachimage.Height;
new_attach_box.BackgroundImageLayout = ImageLayout.Stretch;
new_attach_box.SizeMode = PictureBoxSizeMode.StretchImage;
new_attach_box.Location = new Point(ConceptProperties.ConceptsMap[i].Coords[0] + (ConceptProperties.ConceptsMap[i].Coords[2]), ConceptProperties.ConceptsMap[i].Coords[1] + (ConceptProperties.ConceptsMap[i].Coords[3]));
conceptattchboxlist[i] = new_attach_box;
}
}
for (int i = 0; i < ConceptProperties.ConnectionMap.Count; i++)
{
connectionattchboxlist.Add(new PictureBox());
connectionattchboxlist[i].Visible = true;
if (ConceptProperties.ConnectionMap[i].Attachments.Count > 0)
{
PictureBox new_attach_box = new PictureBox();
new_attach_box.Image = attachimage.Image;
new_attach_box.Width = attachimage.Width;
new_attach_box.Height = attachimage.Height;
new_attach_box.BackgroundImageLayout = ImageLayout.Stretch;
new_attach_box.SizeMode = PictureBoxSizeMode.StretchImage;
new_attach_box.Location = new Point(ConceptProperties.ConceptsMap[i].Coords[0] + (ConceptProperties.ConceptsMap[i].Coords[2]), ConceptProperties.ConceptsMap[i].Coords[1] + (ConceptProperties.ConceptsMap[i].Coords[3]));
new_attach_box.Show();
connectionattchboxlist[i] = new_attach_box;
}
}
}
To add a pictureBox or any control use:
PictureBox pic = new Picturebox();
this.Controls.Add(pic);
Related
I have a panel in Visual Studio/windows form app.But I coulnd't add pictureBox on it with code.It is working if I work without panel however I need it.MyCodes:
PictureBox[] pipe = new PictureBox[3];
private void Form1_Load(object sender, EventArgs e)
{
CreatePipes(1);}
private void CreatetopPipes(int Number)
{
for (int i = 0; i <= Number; i++)
{
PictureBox temp = new PictureBox();
this.Controls.Add(temp);
temp.Width = 50;
temp.Height = 350;
temp.BorderStyle = BorderStyle.FixedSingle;
temp.BackColor = Color.Red;
temp.Top = 30;
temp.Left = 300;
topPipe[i] = temp;
topPipe[i].Visible = true;
}
}
You can use panelName.Controls.Add(temp), and i advise you to change a top of PictureBox in for loop to view all PictureBox, like this :
private void CreatetopPipes(int Number)
{
for (int i = 0; i <= Number; i++)
{
PictureBox temp = new PictureBox();
panelName.Controls.Add(temp);
temp.Width = 50;
temp.Height = 350;
temp.BorderStyle = BorderStyle.FixedSingle;
temp.BackColor = Color.Red;
temp.Top = temp.Height * panelName.Controls.Count;
temp.Left = 300;
topPipe[i] = temp;
topPipe[i].Visible = true;
}
}
I want to display image from list to picturebox.
My image is displaying in the picturebox but the problem is it shows the size the of the image in the picturebox that is defined in the list. Can anyone tell me how to enlarge my image size?
here is my piece of code:
private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListViewItem itm in listView.SelectedItems)
{
int imgIndex = itm.ImageIndex;
if (imgIndex >= 0 && imgIndex < this.documents.Images.Count)
{
// this.documents.Images[imgIndex].Width = 417;
pictureBox.Image = this.documents.Images[imgIndex];
}
}
}
and this is how I am getting images from the database:
ImageList documents = new ImageList();
if (documents.Images.Count < 1)
{
MessageBox.Show("No Documents Found.");
}
else
{
// pictureBox.Image = documents.Images[1];
this.listView.View = View.LargeIcon;
documents.ImageSize = new Size(256, 256);
listView.LargeImageList = documents;
listView.Items.Clear();
for (int j = 0; j < documents.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView.Items.Add(item);
}
}
PictureBox has SizeMode property that is used for manipulating image size:
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
More about that on MSDN
-Create a new imagelist (imagelist1)**
-Add images to your imagelist
-Create a new listview (listview1)
-Create a picturebox (picturebox1)
-Create a new button (button1)
-Create another button (button2)**
-Import images from imagelist1 to listview1
private void button1_Click(object sender, EventArgs e)
{
listView1.Scrollable = true;
listView1.View = View.LargeIcon;
imageList1.ImageSize = new Size(100, 100);
listView1.LargeImageList = imagelist1;
for (int i = 0; i < imagelist1.Images.Count; ++i)
{
string s = imagelist1.Images.Keys[i].ToString();
ListViewItem lstItem = new ListViewItem();
lstItem.ImageIndex = i;
lstItem.Text = s;
listView1.Items.Add(lstItem);
}
}
- Set the selected image into your picture box from listview
private void button2_Click(object sender, EventArgs e)
{
if (this != null && listView1.SelectedItems.Count > 0)
{
ListViewItem lvi = listView1.SelectedItems[0];
string imagekeyname = lvi.Text;
if (this.pictureBox1.Image != null)
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
//set the selected image into your picturebox
this.pictureBox1.Image = imagelist1.Images[imagekeyname];
}
}
I am wondering how I could set an Image to "grid_Main", if I have created all my images in a loop (See Code).
Code:
private void MoleImageMaker()
{
NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
String MoleImageFunction = TUtils.GetIniFileString(Moleini, "ImagePath", "PictureFile", Root + "mole2.png");
for (int i = 0; i < NumofImages; i++)
{
Image mole = new Image();
mole.Source = new BitmapImage(new Uri(MoleImageFunction));
mole.Name = "Mole" + i;
}
}
Something like this
for (int i = 0; i < NumofImages; i++)
{
Image mole = new Image();
mole.Source = new BitmapImage(new Uri(MoleImageFunction));
mole.Name = "Mole" + i;
Grid.SetColumn(mole, i);
grid_Main.Children.Add(mole);
}
I have a feeling that Im am missing something obvious but:
I have a single row of pictures in a form, in theory the pictures could go on forever. I need a scroll bar so that the user can look at all of the pictures in the row. I know I need to enable auto scroll but I have no idea how to enable it. Can someone tell me how to enable it or something that I am missing?
If it helps this is the code i am using to generate the pictures:
private void imagePalletToolStripMenuItem_Click(object sender, EventArgs e)
{
MyPalletGui.Show();
Dictionary<string,Bitmap> MyPallet = MyImageCollection.ToDictionary();
int xcor = -50;
int ycor = 0;
foreach (Bitmap curtImage in MyPallet.Values){
PictureBox myPicBox = new PictureBox();
xcor += 50;
myPicBox.Location = new Point(xcor, ycor);
myPicBox.Width = 50;
myPicBox.Height = 50;
myPicBox.Visible = true;
myPicBox.Image = new Bitmap(curtImage);
this.MyPalletGui.Controls.Add(myPicBox);
This code will do exactly what you want, it uses the Form as the ViewPort with AutoScroll:
public Form1()
{
InitializeComponent();
PopulatePictures();
}
private void PopulatePictures()
{
this.AutoScroll = true;
string[] list = Directory.GetFiles(#"C:\\Users\\Public\\Pictures\\Sample Pictures", "*.jpg");
PictureBox[] picturebox= new PictureBox[list.Length];
int y = 100;
for (int index = 0; index < picturebox.Length; index++)
{
picturebox[index] = new PictureBox();
this.Controls.Add(picturebox[index]);
picturebox[index].Location=new Point(index * 120, y);
if(x%12 == 0)
y = y + 150;
picturebox[index].Size = new Size(100,120);
picturebox[index].Image = Image.FromFile(list[index]);
}
}
I'd like to modify this PictureBox Array Project.
i want to put a reset button than will clear all the PictureBox Array it created
more likely the form will be empty again as like from the beginning.
this is some of it's code;
// Function to add PictureBox Controls
private void AddControls(int cNumber)
{
imgArray = new System.Windows.Forms.PictureBox[cNumber]; // assign number array
for (int i = 0; i < cNumber; i++)
{
imgArray[i] = new System.Windows.Forms.PictureBox(); // Initialize one variable
}
// When call this function you determine number of controls
}
private void ImagesInFolder()
{
FileInfo FInfo;
// Fill the array (imgName) with all images in any folder
imgName = Directory.GetFiles(Application.StartupPath + #"\Images");
// How many Picture files in this folder
NumOfFiles = imgName.Length;
imgExtension = new string[NumOfFiles];
for (int i = 0; i < NumOfFiles; i++)
{
FInfo = new FileInfo(imgName[i]);
imgExtension[i] = FInfo.Extension; // We need to know the Extension
//
}
}
private void ShowFolderImages()
{
int Xpos = 8;
int Ypos = 8;
Image img;
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
MyProgress.Visible = true;
MyProgress.Minimum = 1;
MyProgress.Maximum = NumOfFiles;
MyProgress.Value = 1;
MyProgress.Step = 1;
string[] Ext = new string [] {".GIF", ".JPG", ".BMP", ".PNG"};
AddControls(NumOfFiles);
for (int i = 0; i < NumOfFiles; i++)
{
switch (imgExtension[i].ToUpper())
{
case ".JPG":
case ".BMP":
case ".GIF":
case ".PNG":
img = Image.FromFile(imgName[i]); // or img = new Bitmap(imgName[i]);
imgArray[i].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
img = null;
if (Xpos > 432) // six images in a line
{
Xpos = 8; // leave eight pixels at Left
Ypos = Ypos + 72; // height of image + 8
}
imgArray[i].Left = Xpos;
imgArray[i].Top = Ypos;
imgArray[i].Width = 64;
imgArray[i].Height = 64;
imgArray[i].Visible = true;
// Fill the (Tag) with name and full path of image
imgArray[i].Tag = imgName[i];
imgArray[i].Click += new System.EventHandler(ClickImage);
this.BackPanel.Controls.Add(imgArray[i]);
Xpos = Xpos + 72; // width of image + 8
Application.DoEvents();
MyProgress.PerformStep();
break;
}
}
MyProgress.Visible = false;
}
private bool ThumbnailCallback()
{
return false;
}
private void btnLoad_Click(object sender, System.EventArgs e)
{
ImagesInFolder(); // Load images
ShowFolderImages(); // Show images on PictureBox array
this.Text = "Click wanted image";
}
how can i do that?
sorry i don't have any code for the reset button yet.
i don't know what to do, i am new to c#.
You can just set the image to null:
private void Clear()
{
foreach (var pictureBox in imgArray)
{
pictureBox.Image = null;
pictureBox.Invalidate();
}
}
I will follow this steps to be sure everything will be fred :
private void btnReset_Click(object sender, System.EventArgs e)
{
for(int x = this.BackPanel.Controls.Count - 1; x >= 0; x--)
{
if(this.BackPanel.Controls[x].GetType() == typeof(PictureBox))
this.BackPanel.Controls.Remove(x);
}
for(int x = 0; x < imgArray.Length; x++)
{
imgArray[x].Image = null;
imgArray[x] = null;
}
}
Assuming there are no other child controls in this.Backpanel (the container control that is actually displaying your images), this will probably work:
private void ClearImages() {
this.BackPanel.Controls.Clear();
imgArray = null;
}
Good Luck!
If you are drawing on a pictureBox and you want to clear it:
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.Clear(this.pictureBox1.BackColor);
After that you can draw again on the control.
I hope this can help to someone.