• How to Create Thumbnail Images in C#


    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 :

    Generate Thumbnail

    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.

  • 相关阅读:
    HIVE 技巧积累之合并重叠日期
    记一次hive版本升级
    【转】elasticsearch中字段类型默认显示{ "foo": { "type": "text", "fields": { "keyword": {"type": "keyword", "ignore_above": 256} }
    【转】深入理解Linux修改hostname
    VMware中 CentOS7挂载windows共享文件夹
    Apache版hadoop编译
    No route info of this topic
    java遍历文件夹及所有子文件
    关闭spring整合kafka时,消费者一直打印kafka日志
    (转)详解shell中>/dev/null 2>&1到底是什么
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/1575101.html
Copyright © 2020-2023  润新知