Reputation: 147
Hi i'm doing a simple image viewer in C#. It has 3 button first button contain "Open" and the last two button is "Back and Next" and the picturebox. I'm done with the open button using OpenFileDialog Here's my code in OpenFiledialog:
private void openButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JPEG|*.jpg|Bitmaps|*.bmp";
if(openFileDialog.ShowDialog()== DialogResult.OK)
{
pictureBox1.Image = Bitmap.FromFile(openFileDialog.FileName);
}
}
Now this is the problem i don't have idea what code can i use the in "next and back" button. i know it's looping. Need you're help guys thank you..
Upvotes: 1
Views: 11914
Reputation: 59
Here what I did for "btnPrevImage" is some how easier:
*// this button is forward button*
private void btnForward_Click(object sender, EventArgs e)
{
if(currentIndex!=imageList1.Images.Count-1 && imageList1.Images.Count > 0)
{
pictureBox1.Image = imageList1.Images[currentIndex++];
}
}
*// this button is for Previews button*
private void btnPrevImage_Click(object sender, EventArgs e)
{
if (currentIndex!=0)
{
pictureBox1.Image = imageList1.Images[--currentIndex];
}
do not forget to declare currentIndex.
Upvotes: 0
Reputation: 945
Here's working user control code. You'll need to create new Window Forms Application, create user control called MyPictureViewer. Add pictureBox, and 3 buttons to this control. Paste below code to this usercontrol codebehind, hook click events, and add this control to main form. Hope this helps
public partial class MyPictureViewer : UserControl
{
protected string[] pFileNames;
protected int pCurrentImage = -1;
public MyPictureViewer()
{
InitializeComponent();
}
void btnPrevImage_Click(object sender, EventArgs e)
{
if (pFileNames.Length > 0)
{
pCurrentImage = pCurrentImage == 0 ? pFileNames.Length - 1 : --pCurrentImage;
ShowCurrentImage();
}
}
void btnNextImage_Click(object sender, EventArgs e)
{
if (pFileNames.Length > 0)
{
pCurrentImage = pCurrentImage == pFileNames.Length - 1 ? 0 : ++pCurrentImage;
ShowCurrentImage();
}
}
private void openButton_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;
ShowCurrentImage();
}
}
protected void ShowCurrentImage()
{
if (pCurrentImage >= 0 && pCurrentImage <= pFileNames.Length - 1)
{
pictureBox1.Image = Bitmap.FromFile(pFileNames[pCurrentImage]);
}
}
}
Upvotes: 2
Reputation: 1738
You need some kind of enumeration of files. One way is to allow the user to select multiple files in the OpenFileDialog.Multiselect
property. OpenFileDialog
exposes a property Files that contains all the selected file names.
class MyPictureViewer
{
protected string[] pFileNames;
protected int pCurrentImage = -1;
private void openButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JPEG|*.jpg|Bitmaps|*.bmp";
if(openFileDialog.ShowDialog()== DialogResult.OK)
{
pFileNames = openFileDialog.FileNames;
pCurrentImage=0;
ShowCurrentImage();
}
}
protected void ShowCurrentImage()
{
if(pCurrentImage >= 0 && pCurrentImage < pFileNames.Length-1)
{
pictureBox1.Image = Bitmap.FromFile(pFileNames[pCurrentImage]);
}
}
}
You can implement event handlers for the click event of you next and previos either checking borders (so once you reach the last image you don't go beyond it) or cycling (if the user clicks Next on the last image, jump to the first)
void btnNextImage_Click(object sender, EventArgs e)
{
++pCurrentImage;
//check if this was last image in list
if(pCurrentImage >= pFileNames.Length)
pCurrentImage = pFileNames.Length == 0? -1 : 0;//if this was last image, go to first image
ShowCurrentImage();
}
void btnPrevImage_Click(object sender, EventArgs e)
{
--pCurrentImage;
//check if this was first image in list
if (pCurrentImage < 0)
pCurrentImage = pFileNames.Length == 0 ? -1 : pFileNames.Length -1;//if this was first image, go to last image
ShowCurrentImage();
}
Upvotes: 3