C#实现为一张大尺寸图片创建缩略图的方法

作者:红薯 时间:2021-08-03 21:07:15 

本文实例讲述了C#实现为一张大尺寸图片创建缩略图的方法。分享给大家供大家参考。具体实现方法如下:


public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
 System.Drawing.Bitmap bmpOut = null;
 try
 {
   Bitmap loBMP = new Bitmap(lcFilename);
   ImageFormat loFormat = loBMP.RawFormat;
   decimal lnRatio;
   int lnNewWidth = 0;
   int lnNewHeight = 0;
   //*** If the image is smaller than a thumbnail just return it
   if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
     return loBMP;
   if (loBMP.Width > loBMP.Height)
   {
     lnRatio = (decimal)lnWidth / loBMP.Width;
     lnNewWidth = lnWidth;
     decimal lnTemp = loBMP.Height * lnRatio;
     lnNewHeight = (int)lnTemp;
   }
   else
   {
     lnRatio = (decimal)lnHeight / loBMP.Height;
     lnNewHeight = lnHeight;
     decimal lnTemp = loBMP.Width * lnRatio;
     lnNewWidth = (int)lnTemp;
   }
   bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
   Graphics g = Graphics.FromImage(bmpOut);
   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
   g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
   g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
   loBMP.Dispose();
 }
 catch
 {
   return null;
 }
 return bmpOut;
}

希望本文所述对大家的C#程序设计有所帮助。

标签:C#,缩略图
0
投稿

猜你喜欢

  • java 安全 ysoserial CommonsCollections6 分析

    2021-06-04 01:58:14
  • C#获取上个月第一天和最后一天日期的方法

    2023-02-22 07:33:44
  • JAVA JNI函数的注册过程详细介绍

    2023-02-07 18:41:17
  • Java实现五子棋(附详细源码)

    2023-10-19 15:20:54
  • 详解Mybatis中常用的约束文件

    2023-11-28 08:02:17
  • java实现简单年龄计算器

    2022-01-28 02:23:34
  • 解决Swagger2返回map复杂结构不能解析的问题

    2022-07-15 09:17:04
  • C#中的try catch finally用法分析

    2021-06-20 18:31:28
  • C# WPF 自定义按钮的方法

    2021-08-30 23:42:11
  • java语言基础之标识符和命名规则详解

    2023-04-21 16:50:18
  • 使用Filter过滤器中访问getSession()要转化

    2022-10-01 16:20:04
  • Java多线程编程实战之模拟大量数据同步

    2023-09-02 21:15:59
  • Java源码解析之平衡二叉树

    2023-11-29 11:16:40
  • 详解如何实现SpringBoot的底层注解

    2023-11-23 06:21:04
  • C#通用邮件发送类分享

    2022-05-03 01:35:36
  • C#实现创建标签PDF文件的示例代码

    2023-09-15 07:04:58
  • 基于Scala和Java方法的相互调用

    2021-07-05 11:53:04
  • C#实现EPL II格式打印与打印测试

    2021-10-24 06:40:50
  • java中staticclass静态类详解

    2021-10-12 19:47:35
  • java Semaphore共享锁实现原理解析

    2021-11-02 23:12:38
  • asp之家 软件编程 m.aspxhome.com