Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I want to move next and previous when i click next and previous button from the current position of selected image in c# windows application. Say the solution for that.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Collections;
using System.Net;
using System.Web;
using MSaddin;


namespace MSaddin
{
public partial class formDisplay : Form
{
int locX = 15;
int locY = 20;
int sizeWidth = 140;
int sizeHeight = 135;


public formDisplay()
{
InitializeComponent();
DynamciLabel();

}

private void butView_Click(object sender, EventArgs e)
{

DirectoryInfo Folder;
FileInfo[] Images;
Folder = new DirectoryInfo(@"C:\Users\Public\Pictures\Sample Pictures");
Images = Folder.GetFiles();
panelDisplay.Controls.Clear();

int locnewX = locX;
int locnewY = locY;
foreach (FileInfo img in Images)
{

if (img.Extension.ToLower() == ".png" || img.Extension.ToLower() == ".jpg" || img.Extension.ToLower() == ".gif" || img.Extension.ToLower() == ".jpeg" || img.Extension.ToLower() == ".bmp" || img.Extension.ToLower() == ".tif")
{

if (locnewX >= panelDisplay.Width - sizeWidth - 10)
{
locnewX = locX;
locY = locY + sizeHeight + 30;
locnewY = locY;
}
else
{

locnewY = locY;
}

loadImagestoPanel(img.Name, img.FullName, locnewX, locnewY);
locnewY = locY + sizeHeight + 10;
locnewX = locnewX + sizeWidth + 30;
}

}
imagescount(Images.Length - 1);

}

private void butImage_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(panelDisplay.Width, panelDisplay.Height);
panelDisplay.DrawToBitmap(bmp, new Rectangle(0, 0, panelDisplay.Width, panelDisplay.Height));
SaveFileDialog dlg = new SaveFileDialog();
// dlg.Filter = "JPG Files (*.JPG)|*.JPG";
dlg.FileName = "*";
dlg.DefaultExt = "bmp";
dlg.ValidateNames = true;
dlg.Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif |JPEG Image (.jpeg)|*.jpeg |Png Image (.png)|*.png";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureDisplay.Image.Save(dlg.FileName);
} 
}



private void DynamciLabel()
{
Label lab = new Label();
lab.Text = "Photogallery";
lab.Size = new Size(370, 50);
lab.Location = new Point(495, 16);
lab.Font = new Font("Times New Roman", 25);
lab.ForeColor = Color.White;
lab.AutoSize = false;
lab.TextAlign = ContentAlignment.MiddleLeft;
Color clr = Color.FromArgb(3, 186, 217);
lab.BackColor = clr;
Controls.Add(lab);

}



private void loadImagestoPanel(String imageName, String ImageFullName, int newLocX, int newLocY)
{
PictureBox ctrl = new PictureBox();
ctrl.Image = Image.FromFile(ImageFullName);
ctrl.BackColor = Color.White; 
ctrl.Location = new Point(newLocX, newLocY);
ctrl.Size = new System.Drawing.Size(sizeWidth+20, sizeHeight+20);
ctrl.Padding = new System.Windows.Forms.Padding(10,20,10,20);
ctrl.BorderStyle = BorderStyle.None;
ctrl.SizeMode = PictureBoxSizeMode.StretchImage;
ctrl.
Posted
Updated 2-Apr-15 3:48am
v3
Comments
Praveen Kumar Upadhyay 2-Apr-15 9:37am    
You need to show the code and then we will be able to help you.
Shankar Krish 2-Apr-15 9:43am    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Collections;
using System.Net;
using System.Web;
using MSaddin;


namespace MSaddin
{
public partial class formDisplay : Form
{
int locX = 15;
int locY = 20;
int sizeWidth = 140;
int sizeHeight = 135;


public formDisplay()
{
InitializeComponent();
DynamciLabel();

}

private void butView_Click(object sender, EventArgs e)
{

DirectoryInfo Folder;
FileInfo[] Images;
Folder = new DirectoryInfo(@"C:\Users\Public\Pictures\Sample Pictures");
Images = Folder.GetFiles();
panelDisplay.Controls.Clear();

int locnewX = locX;
int locnewY = locY;
foreach (FileInfo img in Images)
{

if (img.Extension.ToLower() == ".png" || img.Extension.ToLower() == ".jpg" || img.Extension.ToLower() == ".gif" || img.Extension.ToLower() == ".jpeg" || img.Extension.ToLower() == ".bmp" || img.Extension.ToLower() == ".tif")
{

if (locnewX >= panelDisplay.Width - sizeWidth - 10)
{
locnewX = locX;
locY = locY + sizeHeight + 30;
locnewY = locY;
}
else
{

locnewY = locY;
}

loadImagestoPanel(img.Name, img.FullName, locnewX, locnewY);
locnewY = locY + sizeHeight + 10;
locnewX = locnewX + sizeWidth + 30;
}

}
imagescount(Images.Length - 1);

}

private void butImage_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(panelDisplay.Width, panelDisplay.Height);
panelDisplay.DrawToBitmap(bmp, new Rectangle(0, 0, panelDisplay.Width, panelDisplay.Height));
SaveFileDialog dlg = new SaveFileDialog();
// dlg.Filter = "JPG Files (*.JPG)|*.JPG";
dlg.FileName = "*";
dlg.DefaultExt = "bmp";
dlg.ValidateNames = true;
dlg.Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif |JPEG Image (.jpeg)|*.jpeg |Png Image (.png)|*.png";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureDisplay.Image.Save(dlg.FileName);
}
}



private void DynamciLabel()
{
Label lab = new Label();
lab.Text = "Photogallery";
lab.Size = new Size(370, 50);
lab.Location = new Point(495, 16);
lab.Font = new Font("Times New Roman", 25);
lab.ForeColor = Color.White;
lab.AutoSize = false;
lab.TextAlign = ContentAlignment.MiddleLeft;
Color clr = Color.FromArgb(3, 186, 217);
lab.BackColor = clr;
Controls.Add(lab);

}



private void loadImagestoPanel(String imageName, String ImageFullName, int newLocX, int newLocY)
{
PictureBox ctrl = new PictureBox();
ctrl.Image = Image.FromFile(ImageFullName);
ctrl.BackColor = Color.White;
ctrl.Location = new Point(newLocX, newLocY);
ctrl.Size = new System.Drawing.Size(sizeWidth+20, sizeHeight+20);
ctrl.Padding = new System.Windows.Forms.Padding(10,20,10,20);
ctrl.BorderStyle = BorderStyle.None;
ctrl.SizeMode = PictureBoxSizeMode.StretchImage;
ctrl.
Praveen Kumar Upadhyay 2-Apr-15 9:49am    
Please edit your comment and remove the code, as I have added it to your solution.
Also add complete definition for loadImagestoPanel() method.

1 solution

First of all, stop using PictureBox. This is a redundant control, all its reasonable use is to avoid writing some trivial code by yourself, in simplest cases of static images. In all other cases, it presents more hassles than help. I'll tell you what to do:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

For further detail on graphics rendering, implementing interactive behavior and related invalidation, please see:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
Zoom image in C# .net mouse wheel[^],
How to speed up my vb.net application?[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900