Saturday, May 14, 2011

Create Thumbnails with the C# Image Class

Here is a quick tutorial on how to create thumbnail images using the System.Image class.

First let’s create a Windows Form project.




Then add a Button control and a PictureBox control.


Now add a click event to the Button control.


private void button1_Click(object sender, EventArgs e)
{
   Image img = Image.FromFile(@"C:\folder\sampleImage.jpg");
   this.pictureBox1.Image = img.GetThumbnailImage(120, 120,
new Image.GetThumbnailImageAbort(ThumbnailCallback),
IntPtr.Zero);

}

public bool ThumbnailCallback()
{
   return true;
}



First, we need an empty method (ThumbnailCallback)  to pass to the GetThumbnailImageAbort delegate. Then, we simply create an Image object (I am using the FromFile method referencing a local file). Finally, assign the image to the PictureBox control to display it.

Let's run it:

Now click "Load Image":

No comments:

Post a Comment