C#,生成图片的指定尺寸缩略图的源代码

C#,生成图片的指定尺寸缩略图的源代码_第1张图片

编程的时候经常用到图像的缩略图。

本文发布一个用于生成指定尺寸的缩略图的简单方法。

1 文本格式

private void button1_Click(object sender, EventArgs e)
{
    CreateThumbnail(@"demo.jpg", @"demo_thumb.jpg", 128, 128);
}

private void CreateThumbnail(string imgFile, string thumbnailFile, int w, int h)
{
    try
    {
    using (Image img = Image.FromFile(imgFile))
    {
        Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
        img.GetThumbnailImage(w, h, callback, IntPtr.Zero).Save(thumbnailFile);
    }
    }
    catch (Exception ex)
    {
    throw new Exception("Image to Thumbnail error!");
    }
}

public bool ThumbnailCallback()
{
    return false;
}

2 代码格式

private void button1_Click(object sender, EventArgs e)
{
    CreateThumbnail(@"demo.jpg", @"demo_thumb.jpg", 128, 128);
}

private void CreateThumbnail(string imgFile, string thumbnailFile, int w, int h)
{
    try
    {
    using (Image img = Image.FromFile(imgFile))
    {
        Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
        img.GetThumbnailImage(w, h, callback, IntPtr.Zero).Save(thumbnailFile);
    }
    }
    catch (Exception ex)
    {
    throw new Exception("Image to Thumbnail error!");
    }
}

public bool ThumbnailCallback()
{
    return false;
}


 

你可能感兴趣的:(C#实用代码,Coding,Recipes,c#,入门,缩略图)