Add image and value to a panel? - c#

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.

Related

I am trying to add a PictureBox dynamically to a TableLayoutPanel but it's not filling the selected cell

I use this part of code to add the picture but it's half the cell...and still it's not working.
PictureBox pB = new PictureBox {
Size = MaximumSize,
Dock = DockStyle.Fill,
BackgroundImageLayout = ImageLayout.Stretch
};
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK) {
string path = ofd.FileName;
pB.Image = new Bitmap(path);
}
tableLayoutPanel1.Controls.Add(pB, x-1, y-1);
Control control = tableLayoutPanel1.GetControlFromPosition(x - 1, y - 1);
control.Dock = DockStyle.Fill;
control.BackgroundImageLayout = ImageLayout.Stretch;
You are confusing the two images a PictureBox can have:
The Image is the main one with all sorts of capabilties
The BackgroundImage is below it and can be use for just that: A background.
You want to set the layout for the Image; it is called SizeMode
PictureBox pB = new PictureBox
{
Size = MaximumSize,
Dock = DockStyle.Fill,
SizeMode = PictureBoxSizeMode.StretchImage
};
Not sure where and how you set x and y but you should see the full image now, albeit more or less stretched.
And you don't need to set the SizeMode or Docking again at the end..

C# Runtime Generated PictureBox Control

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]);
}

Set multiple images to multiple PictureBoxs

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);
}

Centered and scrolled PictureBox in WinForms

I'm developing a WinForms application and can't figure out how to resolve an issue.
I need to show an image in a Form. Because the image can be arbitrarily large, I need scrollbars on the picturebox containing the image so the user can see it entirely.
Googling around I found out the best way to achieve this is to add the PictureBox as a Child Control of a Panel, and making the Panel autosizable and autoscrollable.
I did that programatically since using the designer I was unable to insert the picturebox as a child control of the panel.
The problem I'm now facing is that I can't seem to be able to center and scroll the picturebox at the same time.
If I put the anchor of the picturebox to top,left,bottom,right, the scrollbars are not shown and the image displayed is strange, if I put back the anchor to only top-left, the image is not centered.
Is there any way to do both at the same time?
Here's the code for my Panel and Picturebox:
this.panelCapturedImage = new System.Windows.Forms.Panel();
this.panelCapturedImage.SuspendLayout();
this.panelCapturedImage.AutoScroll = true;
this.panelCapturedImage.AutoSize = true;
this.panelCapturedImage.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.panelCapturedImage.Controls.Add(this.pictureBoxCapturedImage);
this.panelCapturedImage.Location = new System.Drawing.Point(0, 49);
this.panelCapturedImage.Name = "panelCapturedImage";
this.panelCapturedImage.Size = new System.Drawing.Size(3, 3);
this.panelCapturedImage.TabIndex = 4;
this.pictureBoxCapturedImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBoxCapturedImage.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCapturedImage.Name = "pictureBoxCapturedImage";
this.pictureBoxCapturedImage.Size = new System.Drawing.Size(0, 0);
this.pictureBoxCapturedImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBoxCapturedImage.TabIndex = 0;
this.pictureBoxCapturedImage.TabStop = false;
this.panelCapturedImage.Controls.Add(this.pictureBoxCapturedImage);
And here's where I set the image:
public Image CapturedImage
{
set
{
pictureBoxCapturedImage.Image = value;
pictureBoxCapturedImage.Size = value.Size;
}
}
For the PictureBox, set SizeMode = AutoSize, Anchor it Top, Left, and set its Location to 0, 0.
Set Panel.AutSize to False and Panel.AutoScroll to True.
When you set the PictureBox.Image property, it will auto-size to the size of the image. You can then use that size to set the panel's AutoScrollPosition property:
public Image CapturedImage
{
set
{
pictureBoxCapturedImage.Image = value;
panelCapturedImage.AutoScrollPosition =
new Point {
X = (pictureBoxCapturedImage.Width - panelCapturedImage.Width) / 2,
Y = (pictureBoxCapturedImage.Height - panelCapturedImage.Height) / 2
};
}
}
If the image is smaller then then panel's size, it will remain in the upper left corner. If you want it centered within the panel, you'll have to add logic to set its Location appropriately.
Based on earlier answers I was able to create this full example:
private void testShowPictureBox()
{
/* format form */
Form frmShowPic = new Form();
frmShowPic.Width = 234;
frmShowPic.Height = 332;
frmShowPic.MinimizeBox = false;
frmShowPic.MaximizeBox = false;
frmShowPic.ShowIcon = false;
frmShowPic.StartPosition = FormStartPosition.CenterScreen;
frmShowPic.Text = "Show Picture";
/* add panel */
Panel panPic = new Panel();
panPic.AutoSize = false;
panPic.AutoScroll = true;
panPic.Dock = DockStyle.Fill;
/* add picture box */
PictureBox pbPic = new PictureBox();
pbPic.SizeMode = PictureBoxSizeMode.AutoSize;
pbPic.Location = new Point(0, 0);
panPic.Controls.Add(pbPic);
frmShowPic.Controls.Add(panPic);
/* define image */
pbPic.ImageLocation = #"c:\temp\pic.png";
frmShowPic.ShowDialog();
}
The picturebox has to be set to autosize. anchored at the center (or a border).
You could manage all of this in the designer, don't undertand your problem with that.
The panel has to be set to autoscroll to true.

I can't get ActualSize of an UIElement

I have an Image control, generated in runtime. It renders correct size when displaying. But I can't get size of that element. Width and Height is NaN. ActualWidth and ActualHeight is always 0.0 and control never fires SizeChanged event.
I'm also generating TextBoxes in the runtime and I can't get size of them too.
The code that generates image is bellow.
ExtendedImage image = new ExtendedImage();
image.Name = "element" + item.Id;
if (item.Text.ToUpperInvariant().EndsWith(".GIF"))
{
var gif = BookReader.Imaging.GIFDecoder.Decode(Application.GetResourceStream(new Uri("Files/Books/" + item.BookId + "/" + item.Text, UriKind.Relative)).Stream);
image.Source = gif.Frames[0].Image;
}
else
{
var img = new BitmapImage(new Uri("/Files/Books/" + item.BookId + "/" + item.Text, UriKind.Relative));
image.Source = img;
}
image.SetValue(Canvas.LeftProperty, (double)item.LocationX);
image.SetValue(Canvas.TopProperty, (double)(Application.Current.Host.Content.ActualHeight - item.LocationY));
image.SizeChanged += (o, e) =>
{
var sender = o as ExtendedImage;
image.SetValue(UIElement.RenderTransformOriginProperty, new Point(0.5, 0.5));
};
image.InvalidateMeasure();
if (!item.Visible)
{
image.Visibility = System.Windows.Visibility.Collapsed;
}
ContentPanel.Children.Add(image);
You can't access the size of the image before it's been rendered. And you can't render a item in a Canvas, without it having a explicit size.
So your problem is that you're attempting to get the size of the image before it's actually been rendered.
If the container of the Image has Height and Width, the image should have it as well. Try to put your Image into a Grid or StackPanel and set the size explicitly.

Categories

Resources