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

发布时间:2024年01月23日

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

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

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;
}


?

文章来源:https://blog.csdn.net/beijinghorn/article/details/124266890
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。