I have a form application in which I can input a name of an image and view the image in a PictureBox after clicking a button.
private void button1_Click(object sender, EventArgs e)
{
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
pictureBox1.ImageLocation = image;
}
This is my code but it doesn't do anything, the image I tried to search didn't appear in the PictureBox. What could possibly go wrong? Answers will be appreciated.
Because you've created a new instance of PictureBox but you didn't add it to your form. You should add it to your form's controls like this:
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
//Set pictureBox1's location on the form
pictureBox1.Location = new Point(10 , 10);
//Add pictureBox1 to your form
Controls.Add(pictureBox1);
Now if your image variable contains a valid image path your PictureBox should show it.
Edit: To write a valid image path in your TextBox try write the full picture's path in your TextBox like below:
D:\Pics\yourPic
Or if you've added it to your project it should be like this:
D:\New folder (1)\WindowsFormsApplication1\WindowsFormsApplication1\yourPic
Just don't forget if you have already placed a PictureBox in your form you just need to call it in the code. You don't need to create a new one. And you should remove this line PictureBox pictureBox1 = new PictureBox();.
Related
I have a Winforms application where I programmatically add a number of pictureBox controls and load each one with an image.
How can I link each pictureBox control with a video file so I can run that video by clicking on that pictureBox control?
This is how I add my pictureBox controls:
if (ofd.ShowDialog() == DialogResult.OK)
{
file_address = ofd.FileName;
}
var picture1 = new PictureBox
{
Name = "pictureBox",
Size = new Size(160, 200),
Location = new Point(x, y),
SizeMode = PictureBoxSizeMode.Zoom,
Image = Image.FromFile(file_address),
};
this.Controls.Add(picture1);
I've managed to add click event handler:
picture1.Click += delegate
{
// Do something
}
You can use the Tag property to store the file_address.
this.picture1.tag = file_address;
when you want to play the video by the picture box file name, get the file name by
string file_name = (string)this.picture1.tag;
Solved: Using design toolbox.
I'm using the following method to add a image
private void cobertura1vertente_Load(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.Image = Image.FromFile("C:/cobertura1vertente.png");
pb1.Location = new Point(3, 3);
pb1.Size = new Size(250, 250);
//doubt right below
groupBox81.Controls.Add(pb1);
}
I usually would add a image to a entire form, but now i want to add it to a specific groupbox, who lies inside a tab.
If the code is correct, my guess is that the location that i've set is relative to the whole form, and not to the groupBox, why would that happen?
I have a image showing in
picShowPicture.Image = Image.FromFile(textbox1.Text)
At the moment the image is showing on the main screen what I want is when a user selects a image from the database, it then open up in a new window?
how is this done?
Create a new Form in Designer and pick a PictureBox in it. And create a special method for example
public void SetPicture(Image image)
which will set image to PictureBox.
On selecting picture call:
YourForm form = new YourForm();
form.SetPicture(Image.FromFile(textbox1.Text));
form.ShowDialog();
Or you can dynamically create new form:
Form form = new Form();
PictureBox pictureBox = new PictureBox();
pictureBox.Dock = DockStyle.Fill;
pictureBox.Image = Image.FromFile(textbox1.Text);
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
form.Controls.Add(pictureBox);
form.ShowDialog();
I'm going to make the assumption that by "selects a image from the database" does not mean that you're (de)serializing BLOBs and just want the code to make the image display in a new window. I will further assume that you have a second form created in your project called "Form2" with an image viewer called "picImageViewerOnForm2".
var newImage = Image.FromFile(textbox1.Text);
var newForm = new Form2();
newForm.picImageViewerOnForm2.Image = newImage;
newForm.Show();
Add Form to application and put PictureBox on it, let's say that ID of PictureBox is "pictureBox1", then create public proterty on that form to access picutre box, like this :
public partial class ShowPictureForm : Form
{
public PictureBox ImagePictureBox { get { return this.pictureBox1; } }
public ShowPictureForm()
{
InitializeComponent();
}
}
then show that new form like this :
ShowPictureForm spf = new ShowPictureForm();
spf.ImagePictureBox.Image.FromFile(textbox1.Text)
spf.ShowDialog();
Add a new Windows Form to your project, named "ShowImageWindow".
Add a picturebox to the window, and the following code:
public Image ImageToShow { get; set; }
public ShowImageWindow()
{
InitializeComponent();
}
private void ShowImageWindow_Load(object sender, EventArgs e)
{
pictureBox1.Image = ImageToShow;
}
Then create and show the window as follows:
Image img = Image.FromFile(textBox1.Text);
ShowImageWindow frm = new ShowImageWindow();
frm.ImageToShow = img;
frm.ShowDialog();
frm.Dispose();
Create a new Form (new type derived from System.Windows.Forms.Form) that accept image path as constructor.
Say new form is ImageForm. Create PictureBox inside this form.
in the function ShowImageWindow (or similar in main form), call like following
ImageForm imageForm = new ImageForm(textbox1.Text)
imageForm.ShowDialog()
In the ctor of ImageForm, set the Image into picturebox control inside ImageForm
public ImageForm(String imagePath)
{
pictureBox1.Image = Image.FromFile(imagePath);
}
Create a new form with just picture box in it.
Then when forming that forms object pass image as parameter(of course you have to create a parameterised constructor of your form) and pass that image in a global image variable.
In Form_Load set that global image variable as your image controls image.
I have an mdichild, and an eventhandler for draganddrop so when I drop an image file in my form, a picturebox ( name = dpic ) is created with that image.
I have another eventhandler which is for dpic_Click, so when I click on the image, my form's text is the name of that image.
After the first time I drop an image, another dpic is created, because you can't have two cotrols with the same name. C# automatically changes the name and that makes my event handlers only work for the last image I dropped in. I think if I could make an event handler for my mdichild that gets the name of the control that is under the mouse pointer I could simply change back the image I am pointing at.
UPDATE
Here is my code , I made an event handler for droping in my mdichild :
void mdiChild_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
dpic = new PictureBox() ;
string[] filepath = (string[])e.Data.GetData(DataFormats.FileDrop);
Image image = Image.FromFile(filepath[0]);
dpic.Image = image;
dpic.Tag = Path.GetFileName(filepath[0]);
this.ActiveMdiChild.Controls.Add(dpic);
dpic.ContextMenuStrip = this.contextMenuStrip1;
this.ActiveMdiChild.Refresh();
dpic.BringToFront();
this.ActiveMdiChild.ActiveControl = dpic;
dpic.Click += new EventHandler(dpic_Click);
// _____this helped me do it_________________________________________________
foreach (Control c in this.ActiveMdiChild.Controls)
{
c.Click += new EventHandler(c_Click);
}
// ________________________________________________________________________
}
}
What you are looking for is a sender. The sender will tell which image was clicked and will allow you to get its name.
PictureBox picSender = (PictureBox)sender;
label1.Text = picSender.Name;
EDIT : You put that in the pic_Click event
I don't understand exactly what you want to do here, possibly layering new pictureboxes with dropped images on the form?
If that is so you can use a
List<PictureBox> picboxes = new List<PictureBox>();
and where you do:
dpic = new PictureBox() ;
change to
PictureBox dpic = new PictureBox() ;
picboxes.Add(dpic);
this.Controls.Add(dpic);
But please note that you cannot have unlimited controls declared.
How to navigate the image using Keyboard arrow keys in C#.
My 1st form contains listView. Listview contains 10 images in thumbnail format.Images are from perticular folder. When i double click the image in the list view, its opening in a new window as large image. I want to navigate the image in the new window as per listview order.
If i click the image randomly, want to navigate the image from that selected image.
Its like a Microsoft picture manager.
Plz give me an Idea.
Set your form's KeyPreview property to True. Then add this line of code to the top of your CS file:
using System.IO;
Next, inside the scope of your form, add these two lines:
private FileInfo[] _files;
private int _currentFile;
In your form's Load event, put this code:
DirectoryInfo dirinfo = new DirectoryInfo(
Path.Combine(Application.StartupPath, "images"));
_files = dirinfo.GetFiles();
_currentFile = 0;
Bitmap bmp = (Bitmap)Bitmap.FromFile(_files[_currentFile].FullName);
pictureBox1.Image = bmp;
Finally, in your form's KeyDown event, put this code:
if (e.KeyCode == Keys.Down)
{
_currentFile--;
if (_currentFile < 0)
{
_currentFile = _files.Length - 1;
}
}
else if (e.KeyCode == Keys.Up)
{
_currentFile++;
if (_currentFile >= _files.Length)
{
_currentFile = 0;
}
}
Bitmap bmp = (Bitmap)Bitmap.FromFile(_files[_currentFile].FullName);
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
}
pictureBox1.Image = bmp;
This code assumes that you have a PictureBox on your form named "pictureBox1", and it assumes that you have a folder named "\images" in your application folder that contains the image files you wish to display.
As o.k.w. mentioned in a comment, you may want to enhance this code by resizing the PictureBox to fit the dimensions of the image file. You can do that by setting the SizeMode property of your PictureBox to AutoSize (or set it to CenterImage, if you wish to keep the overall image centered on your form).