Import and display image one by one - c#

I have a C# window form for importing and displaying multiple images.
I am able to import multiple images and display the first image, but got some problems on displaying images one by one.
The program flow: user click the button, then select multiple images. After that, the first image should be displayed on the picture box. When the user click the "next image button", the next image should be shown.
The first image is able to display on the picturebox but no idea on displaying them one by one.
Is there any configuration for achieving this or how to implement it through coding.
Thanks everyone.
My coding:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
InitializeOpenFileDialog();
}
private void InitializeOpenFileDialog()
{
// Set the file dialog to filter for graphics files.
this.openFileDialog1.Filter =
"Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
"All files (*.*)|*.*";
// Allow the user to select multiple images.
this.openFileDialog1.Multiselect = true;
this.openFileDialog1.Title = "My Image Browser";
}
private void SelectFileButton_Click(object sender, EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
// Read the files
foreach (String file in openFileDialog1.FileNames)
{
// Create a PictureBox.
PictureBox pb = new PictureBox();
Image loadedImage = Image.FromFile(file);
pb.Height = loadedImage.Height;
pb.Width = loadedImage.Width;
pb.Image = loadedImage;
flowLayoutPanel1.Controls.Add(pb);
}
}
}
}

IMHO, there is a better way for achieving that.
You don't have to add PictureBox control for each image, it will overload your form.
My suggestion is to keep a list of all loaded images, and an indexer of the current shown image.
Code:
Add a PictureBox to your form (let's call it pictureBox1), where you want the images to shown.
In addition, add these properties to your class:
private List<Image> loadedImages = new List<Image>();
private int currentImageIndex;
In your "load images" button click event:
private void SelectFileButton_Click(object sender, EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
loadedImages.Clear();
// Read the files
foreach (String file in openFileDialog1.FileNames)
{
// Create a PictureBox.
loadedImages.Add(Image.FromFile(file));
}
if (loadedImages.Count > 0)
{
currentImageIndex = 0;
pictureBox1.Image= loadedImages[currentImageIndex];
}
}
}
And finally, for Next/Previous buttons click event you can add this code:
// Mod function to support negative values (for the back button).
int mod(int a, int b)
{
return (a % b + b) % b;
}
// Show the next picture in the PictureBox.
private void button_next_Click(object sender, EventArgs e)
{
currentImageIndex = mod(currentImageIndex + 1, loadedImages.Count);
pictureBox1.Image = loadedImages[currentImageIndex];
}
// Show the previous picture in the PictureBox.
private void button_prev_Click(object sender, EventArgs e)
{
currentImageIndex = mod(currentImageIndex - 1, loadedImages.Count);
pictureBox1.Image = loadedImages[currentImageIndex];
}

Related

How to change image in picturebox C#

I need to change the image by clicking on pictureBox, but when I click it again I can't return former picture
Here is my code:
private void PictureBox_Click(object sender, EventArgs e)
{
if (pictureBox.Image == Properties.Resources.openeye)
pictureBox.Image = Properties.Resources.closeeye;
else
pictureBox.Image = Properties.Resources.openeye;
}
How can I fix it?
Here's an example that demonstrates it with two images. One of an "x" another of an "o".
As you can see, the form has two instance variables x and o to store each of the Image objects. There is another flag field called isX which the ClickHandle event handler uses to check which image is currently displayed and switch to the other image, the event handler then toggles the isX field so the next click responds properly.
public void Main(string[] args)
{
var f1 = new Form1(); // form instance that holds the PictureBox
Task.Run(() => Application.Run(f1)); //I'm running this from LINQPad, but this would also work in a console application.
}
public class Form1 : Form // Derives from the Form class
{
private bool isX; // private instance variable to indicate which image is diplayed
private Image x; // private instance variable storing the x image
private Image o; // private instance variable storing the o image
// the picture box this form uses
private PictureBox p;
public Form1()
{
// load the images from wherever they are stored.
// I do this at construction time to avoid doing disk IO when clicking
x = Image.FromFile(#"C:\image\path\x.png");
o = Image.FromFile(#"C:\image\path\o.png");
// Initialize the picture box
p = new PictureBox {
Name = "p1",
Size = new Size(100,100),
Location = new Point(100,100),
Image = o //Initialize with the o image
};
// register the click event handler
p.Click += this.ClickHandle;
// set the flag to false, since the o image is what we start with
this.isX = false;
// add PictureBox p to the form
this.Controls.Add(p);
}
// handles the click action, registered to the PictureBox.Click event
private void ClickHandle(object sender, EventArgs e)
{
// use the flag to check which image is shown, and display the other image
if(this.isX) // this might work with your image == check, I didn't test it
{
p.Image = this.o;
}
else
{
p.Image = this.x;
}
// set the flag to the opposite of whatever the flag currently is
this.isX = ! isX;
}
}
You can set value of picturebox image, but you cant get it that way.
U can use global variable out of method and go this way.
int i = 0;
private void PictureBox1_Click(object sender, EventArgs e)
{
if (i == 0)
{
pictureBox1.Image = Properties.Resources.close;
i++;
}
else
{
pictureBox1.Image = Properties.Resources.open;
i--;
}
}
You can use this.
private void pictureBox1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image!=null&& getSignatureLen( pictureBox1.Image) == getSignatureLen(Properties.Resources.openeye))
{
pictureBox1.Image = Properties.Resources.closeeye;
}
else
{
pictureBox1.Image = PProperties.Resources.openeye;
}
}
public long getSignatureLen(Image img)
{
using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
{
img.Save(mStream, img.RawFormat);
return mStream.Length;
}
}
You can save the value in the un-used Tag property, of the PictureBox object.
pictureBox1.Tag = 1; //Set it to whatever
if (pictureBox1.Tag == "1") { } //Do your check

How to dynamically add events to a large number of controls?

I'm trying to add 1152 small pictureBoxes to a Winform dynamically then I want to add to each of those pictureBoxes a Click event so that when I click on them, the image change. I have no idea how to add event handler!?
private void Form1_Load(object sender, EventArgs e)
{
Image image1= Image.FromFile(#"C:\Users\image1.png");
Image image2= Image.FromFile(#"C:\Users\image2.png");
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
var pictures= new PictureBox
{
Name = "pic" + i + j,
Size = new Size(14, 14),
Location = new Point(j * 14, i * 14),
Image = image1,
};
this.Controls.Add(pictures);
}
}
}
So you have a sequence of PictureBoxes and you want two things:
You want to subscribe to a Click event
If the Click event occurs you want to change the image, probably depending on the picture box that was clicked.
First you need to have a sequence of the PictureBoxes you want to have this behaviour. If you don't have a sequence yet, and you want all PictureBoxes on a certain Control to have this behaviour you could use this:
IEnumerable<PictureBox> GetPictureBoxes(Control control)
{
return control.Controls.OfType<PictureControl>();
}
See Enumerable.OfType
Subscribe to event:
IEnumerable<PictureBox> myPictureBoxes = ...
foreach (PictureBox pictureBox in myPictureBoxes)
{
pictureBox.Click += new System.EventHandler(this.pictureBox_Click);
}
Event handler:
private void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox pictureBox = (PictureBox)sender;
Image imageToShow = DecideWhichImageToShow(pictureBox); // TODO
// change the image:
pictureBox.Image = imageToShow
}
On event handler's method, the sender argument will be your PictureBox object, so you can write something like this:
private void Pb_Click(object sender, EventArgs e)
{
PictureBox pb = sender as PictureBox;
try
{
if (pb != null)
pb.Image = Image.FromFile(#"NewImagePath");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

How to retain existing images in a panel while redrawing in c#?

I have a panel where I am drawing images on button click. But on the second button click, the previously drawn image is being replaced by the new Image.
void panel_Image_Paint(object sender, PaintEventArgs e)
{
if (Clipboard.ContainsImage())
{
Point p1 = new Point(i, 0);
e.Graphics.DrawImage(Clipboard.GetImage(), p1);
i += img.Width;
}
}
I want to retain the previously drawn image in the panel, when the new image is being drawn. The clipboard is being refreshed on each button click with the new image. Any help would be highly appreciated!!!!
Thanks..
I'm not sure how the paint event actually works and what causes your images to disappear , but what you can try is adding your images to a list and then looping through the list to get all of your images displayed on the panel.
Try this code:
int i = 0;
List<Image> Images = new List<Image>();
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (Clipboard.ContainsImage())
{
Images.Add(Clipboard.GetImage());
foreach (Image item in Images)
{
e.Graphics.DrawImage(item, new Point(i,0));
i += Clipboard.GetImage().Width;
}
}
i = 0;
}
Don't forget to call the Invalidate function.
private void button1_Click(object sender, EventArgs e)
{
panel1.Invalidate();
}

Which pictureBox was selected onClick? C#

I am working on a image viewer and have encountered a small problem.
I load the images into the program using a dialog box then put them into individual picture boxes.
I do not know how to do the onclick for each picture box as when its clicked I want that image to display in a bigger picture box.
public partial class Form1 : Form
{
List<Bitmap> images = new List<Bitmap>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int x = 0;
openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach(String fileName in openFileDialog1.FileNames)
{
images.Add(new Bitmap(fileName));
PictureBox pb = new PictureBox();
Image loadedImage = Image.FromFile(fileName);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Width = flowLayoutPanel1.Width;
pb.Height = 200;
pb.Image = loadedImage;
pb.MouseClick += pb_MouseClick;
flowLayoutPanel1.Controls.Add(pb);
pb.Name = "" + x;
x++;
}
}
MessageBox.Show(String.Format("{0} images loaded",images.Count),"",MessageBoxButtons.OK);
}
private void Form1_Load(object sender, EventArgs e)
{
}
void pb_MouseClick(object sender, MouseEventArgs e)
{
// handle click event
if (e.Button == MouseButtons.Left)
MessageBox.Show("Image clicked");
pbMain.Image = images[0];
pbMain.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
If, in your Click handler for the picture boxes, cast sender to a PictureBox like so:
PictureBox pb = (PictureBox)sender;
Then you don't need to know which one specifically was clicked. Just use that reference to grab the image and put it in the larger box.

How to hide/show the background image of pictureBox?

I have a pictureBox with an image inside.
I want, when i click on a button the image should hide and click again to show the image.
In the pictureBox, using paint event i am drawing some lines.
So if im doing pictureBox1.Refresh(); it will draw the lines. I want that if i click on a button the image will not display on/off.
pictureBox1 = null; or pictureBox1.Image.Dispose(); doesn't work it's showing me big red x with white background.
To hide it:
pictureBox.Visible = false;
To hide/show it in a click event:
void SomeButton_Click(Object sender, EventArgs e)
{
pictureBox.Visible = !pictureBox.Visible;
}
For toggling the image in your PictureBox you can create a 1 pixel bitmap and assign it to the picture box when you want hide your image, then assign your image back again. I am a little unclear of what the second part of your question is asking, any drawing in the picturebox's Paint Event will remain unless you exclude it in the Paint Event based on some Condition. If you want to draw a line in the box an toggle it on/off from a button see my second example.
i.e.
public partial class Form1 : Form
{
Bitmap nullBitmap = new Bitmap(1, 1); // create a 1 pixel bitmap
Bitmap myImage = new Bitmap("Load your Image Here"); // Load your image
bool showImage; // boolean variable so we know what image is assigned
public Form1()
{
InitializeComponent();
pictureBox1.Image = myImage;
showImage = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (showImage)
{
pictureBox1.Image = nullBitmap;
showImage = false;
}
else
{
pictureBox1.Image = myImage;
showImage = true;
}
}
}
Second Example
public partial class Form1 : Form
{
bool showLines;
public Form1()
{
InitializeComponent();
showLines = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (showLines)
{
showLines = false;
pictureBox1.Invalidate();
}
else
{
showLines = true;
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if(showLines)
e.Graphics.DrawLine(Pens.Purple, 0, 0, 100, 100);
}
}
picturebox1.BackgroundImage = null

Categories

Resources