csharp

Create Image Gallery In Windows Form using C#

In this article, we will learn how to create image Gallery in Windows Forms using C#.
Complete Demo: we can learn step by step
1.create image Gallery in windows forms using c#
2. select all images using (ctrl+A) from an image gallery
3. Rename selected image from image gallery windows forms using listview c#.

Listview: Listview is a control it is basically used for display item in different views like Large, Small, list, Tile and Details.
ScrrenShot Describe output After Run The Project:
Screenshot
IIn this tutorial we will learn simple way to create Image Gallery/viewer in c# using Listview.
Step 1. Open visual studio clicks on the New project–>”Visual C# Windows”–>”Windows Form Application”.Write the name of a project like “ImagegalleryListView”.

Step 2. Open Form1 Design page drop one listView, one Button and four radio Button List.Design your page like screenshot.

Step 3.Double Click on Open Button “button_click” event generated. After that simply put this code on your form1.cs page.
Complete Demo:Simple way to create image Gallery in windows form using Listview C#
[php]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace ImageGalleryListView
{
public partial class Form1: Form
{

//Dynamically create Image List at run-time
System.Windows.Forms.ImageList myImageList = new ImageList();
System.Windows.Forms.ImageList myImageListSmall = new ImageList();
System.Windows.Forms.ImageList myImageListLarge = new ImageList();
int count = 0;
public string ImageNewName="";
OpenFileDialog ofd = new OpenFileDialog() { Multiselect = true, ValidateNames = true, Filter =
"JPG|*jpg|JPEG|*.jpeg|PNG|*.png" };
FileInfo fi;

public Form1()
{
InitializeComponent();
listViewFile.SmallImageList = myImageListSmall;
listViewFile.SmallImageList = myImageListLarge;
this.KeyPreview = true;
}

private void buttonOpen_Click(object sender, EventArgs e)
{
//set the size of imagelist
myImageList.ImageSize = new Size(50,50);
myImageListSmall.ImageSize = new Size(32, 32);
myImageListLarge.ImageSize = new Size(80, 80);
if (ofd.ShowDialog() == DialogResult.OK)
{
listViewFile.Items.Clear();
foreach (string fileName in ofd.FileNames)
{
fi = new FileInfo(fileName);
FileInfo fileinfo = new FileInfo(fileName);
using (FileStream stream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read))
{
myImageList.Images.Add(Image.FromStream(stream));
myImageListSmall.Images.Add(Image.FromStream(stream));
myImageListLarge.Images.Add(Image.FromStream(stream));

}
listViewFile.LargeImageList = myImageList;

listViewFile.Items.Add(new ListViewItem { ImageIndex = count, Text = fi.Name, Tag = fi.FullName
});
count++;
}
}

}
//Code for large Image icon
//Generate event for CheckedChanged
private void radioButtonLarge_CheckedChanged(object sender, EventArgs e)
{
if(radioButtonLarge.Checked==true)
{
listViewFile.LargeImageList=myImageListLarge;
listViewFile.View=View.LargeIcon;
}
}
//Code for Small Image icon
//Generate event for CheckedChanged
private void radioButtonSmall_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonSmall.Checked == true)
{
listViewFile.SmallImageList = myImageListSmall;

listViewFile.View = View.SmallIcon;
}
}
//Code for List Image icon
//Generate event CheckedChanged
private void radioButtonList_CheckedChanged(object sender, EventArgs e)
{

if (radioButtonList.Checked == true)
{

listViewFile.View = View.List;
}
}

//Select all images using (ctrl+A)
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
foreach (ListViewItem item in listViewFile.Items)
{
item.Selected = true;
listViewFile.Focus();

}
}
}
/// <summary>
/// Here i have create dynamically shortcut menu Contextmenu
/// Add New menuItem "Rename"
/// Create OnMenuItemClick event on MenuItem Click
///
/// </summary>

private void listViewFile_MouseClick(object sender, MouseEventArgs e)
{

ContextMenu cm = new ContextMenu();
listViewFile.ContextMenu = cm;
var mi = new MenuItem("Rename");

mi.MenuItems.Add(mi);
mi.Click += OnMenuItemClick;
cm.MenuItems.Add(mi);

}
//Rename Image Name
//Don’t Forget to listview LabelEdit property allow to true by default labelEdit property false
private void OnMenuItemClick(object sender, EventArgs e)
{
listViewFile.SelectedItems[0].BeginEdit();
}

/// <summary>
/// If you want change system image name write this code
/// Here i have to create event.Like Normal listvView AfterLabelEdit event.
/// you can use this event whenever you have to require some action/operation on selected item
///Here replace old image name from NewImage
/// you have to fired this event and perform some action
///store edit image in ImageNewName string Variable
/// whatever you are select item store in ListViewItem object
/// fileInfo.MoveTo replace old Image of system from New Image Name
/// </summary>

private void listViewFile_AfterLabelEdit(object sender, System.Windows.Forms.LabelEditEventArgs e)
{
if (e.Label == null)
return;
ImageNewName = Convert.ToString(e.Label);

ListViewItem item1 = listViewFile.SelectedItems[0];

FileInfo fileInfo = new FileInfo(item1.Tag.ToString());
fileInfo.MoveTo(fileInfo.Directory.FullName + "\\" + ImageNewName + fileInfo.Extension);
listViewFile.Items[count1].Text = ImageNewName;

}
}
}

[/php]

If This article helped you. Humble request to you please donate small amount only 5 Ruppes or 1 Ruppes so that I can help the needy poor people. Send amount this phonepe upi id : 8800846247@ybl or google pay 8800846247. Thank you from my bottom of heart.

Demo

Leave a Reply

Your email address will not be published.