• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

HowtoCreateThumbnailImagesinC#

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

A thumbnail is a small sized image. Creating a thumbnail using .NET is extremely simple. In this article, we will explore how to create a thumbnail image and display the thumbnail in our application. Follow these steps:

Step 1: Open Visual Studio 2005/2008. File > New > Project > Visual C# or Visual Basic > Windows Application. Enter the name of the application and click Ok.

Step 2: Drag and drop a label, 2 button controls and an OpenFileDialog component to the form. Rename them as follows:

Label1 – lblFile

Button1 – btnOpen

Button2 – btnGenerateThumbnail

TextBox – txtFileNm

OpenFileDialog – Set the Filter to ‘JPG Files|*.jpg’

 

Step 3: On the ‘btnOpen’ click, display the File Open dialog box and accept the selected .jpg file in the txtFileNm textbox.

C#

private void btnOpen_Click(object sender, EventArgs e)

        {

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)

            {

                txtFileNm.Text = openFileDialog1.FileName;

            }

        }

 

Step 4: Next, on the ‘btnGenerateThumbnail’ click, add the following code:

C#

// Declare a class level variable

Image imgThumb = null;

private void btnGenerateThumbnail_Click(object sender, EventArgs e)

        {

try

            {

Image image = null;

// Check if textbox has a value

if (txtFileNm.Text != String.Empty)

                    image = Image.FromFile(txtFileNm.Text);

// Check if image exists

if (image != null)

                {

                    imgThumb = image.GetThumbnailImage(100, 100, null, new IntPtr());

this.Refresh();

                }

            }

catch

            {

MessageBox.Show("An error occured");

            }

        }

The code creates an Image object from the image supplied in the textbox. Using the Image.GetThumbnailImage(), the code then creates a thumbnail image with a size of 100*100.

The Image.GetThumbnailImage() takes in four arguments :

Width, in pixel of the thumbnail image that is to be generated

Height, in pixel of the thumbnail image that is to be generated

Callback, a Image.GetTumbnailImageAbort delegate to prematurely cancel execution

CallbackData, of type IntPtr to represent a pointer or a handle.

Step 5: The final step is to add the Paint event which is called using this.Refresh() in the button click. The thumbnail image is drawn on the form.

C#

private void Form1_Paint(object sender, PaintEventArgs e)

        {

if(imgThumb != null)

            e.Graphics.DrawImage(imgThumb,30, 20, imgThumb.Width, imgThumb.Height);

        }

 

Run the application, select the image and click on the Generate button. The preview will be similar to the image displayed below :

You can actually extend this example to 'save' the generated thumbnails. Something like a ‘Thumbnail Creator’ program. You can loop through a folder containing images and generate thumbnail images for all the images in the folder. I would encourage you to try out these ideas.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[C#]线程调用带参数的方法转发布时间:2022-07-13
下一篇:
A new way to perform parameter validation in C# 3.0发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap