No property name mods were done, because the defaults were sufficient for this example. The mainPictureBox member is a PictureBox component. And there are 3 Buttons. Two of the buttons advance the Picture being viewed forward and backward using a fileIndex. Basically, progressing through the pics 1 at a time. The other button exits the application. The directory info member and the array of files use is obvious in the example. Quick and simple, and you have a photo viewer that you can scroll through. It is so fast to create with .Net.
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;
namespace formsStuff
{
public partial class Form2 : Form
{
private int fileIndex;
private DirectoryInfo dir;
private FileInfo[] files;
private string pictureDir;
public Form2()
{
InitializeComponent();
fileIndex = 0;
pictureDir =
@"C:\Users\Public\Pictures\Sample Pictures";
dir = new DirectoryInfo(pictureDir);
files = dir.GetFiles("*.jpg");
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
fileIndex++;
if (fileIndex >= files.Length)
fileIndex = files.Length - 1;
ShowPicture(fileIndex);
}
private void button3_Click(object sender, EventArgs e)
{
fileIndex--;
if (fileIndex <= 0) fileIndex = 0;
ShowPicture(fileIndex);
}
private void ShowPicture(int index)
{
mainPictureBox.Load(files[index].FullName);
}
}
}
Wednesday, June 10, 2009
C# Picture Viewer
It still amazes me how fast you can create a working example, and how little code it takes to get something going with .Net. I created a simple jpg picture viewer while I was watching "Everybody Loves Raymond" repeats on the TV. Heres the code I had to add for the application to work: (Ok, what's the trick for getting the C# style/formatting to show up in a blog? Anybody?)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment