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).
Related
I am trying to check if a PictureBox contains a certain image, the way I am trying to do it seems like it would work in my head, however, does not, i am not sure if there is any other way to check if the picturebox on the form contains a certain image.
private void user_btn_Click(object sender, EventArgs e)
{
//If statement to check if the forms picture box contains a certain image
if (pictureBox1.Image == Resources.user_male_white_red_brown)
{
this.Hide();
UserProfile User = new UserProfile();
User.ShowDialog();
User.pictureBox1.Image = Resources.user_male_white_red_brown;
this.Close();
}
else if (pictureBox1.Image == Resources.user_female_olive_orange)
{
this.Hide();
UserProfile User = new UserProfile();
User.ShowDialog();
User.pictureBox1.Image = Resources.user_female_olive_orange;
this.Close();
}
}
Although PictureBox Image might be identical as the one in the resources but they are not the same as reference. (Imagine like you have 2 copy of the same photo although their image is the same but they are 2 separate photos).
There are a few ways you can do it, one of them (a simple one) is to set the picturebox Tag to a relevent value whenever setting its image and compare that value instead of comparing images:
User.pictureBox1.Image = Resources.user_male_white_red_brown;
User.pictureBox1.Tag = "user_male_white_red_brown";
And then:
if((string)User.pictureBox1.Tag == "user_male_white_red_brown")
{
// your logic
}
This way you need to set PictureBox tag whenever you set its image.
Another way would be loading all images from resources to an array and setting the PictureBox image from the array, this way as the reference of both images (picturebox.Image and the image item in the array) are the same comparison would work for images but I think the first solution is easier.
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;
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();.
In my windows application I am using the ImageListView.dll displaying selected images.
I used the following code for Adding the selected images to ImageListView
string folder = Photo_Care.Properties.Settings.Default.LastFolder;
if (Directory.Exists(folder))
openFileDialog1.InitialDirectory = folder;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
folder = Path.GetDirectoryName(openFileDialog1.FileName);
Properties.Settings.Default.LastFolder = folder;
Properties.Settings.Default.Save();
imageListView1.Items.AddRange(openFileDialog1.FileNames);
}
And I can able to remove the selected Image from Image List using the below code.
imageListView1.SuspendLayout();
// Remove selected items
foreach (var item in imageListView1.SelectedItems)
imageListView1.Items.Remove(item);
// Resume layout logic.
imageListView1.ResumeLayout(true);
But Now I want to add the close button right top of the added image instantly for removing the image. Like Below Image
I'm doing an imageviewer. I've already done importing the image in the picture box.
Which code should be used to autosize the image in the picture box? Here's my code in Viewing the image in picture box.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.Filter = "JPEG|*.jpg|Bitmaps|*.bmp";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
pFileNames = openFileDialog.FileNames;
pCurrentImage = 0;
ImageView();
}
}
protected void ImageView()
{
if (pCurrentImage >= 0 && pCurrentImage <= pFileNames.Length - 1)
{
pictureBox1.Image = Bitmap.FromFile(pFileNames[pCurrentImage]);
}
}
Take a look at the SizeMode property of the PictureBox: http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.sizemode.aspx
Set this to AutoSize and you're ready to go.
Check here what you can set it to
Check PictureBox.SizeMode Property and set it by PictureBoxSizeMode Enumeration as you want PictureBox control to do while displaying the image.
AutoSize means that the PictureBox is going to fit to the image.
If you want image fit to the pictureBox then set the sizemode to StretchImage
// Set the SizeMode property to the StretchImage value.
// This will enlarge the image as needed to fit into
// the PictureBox.
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
You could try setting the SizeMode property on the PictureBox to AutoSize.