图片处理是C#程序开发中时常会涉及到的一个业务,除了图像的上传、保存以及下载等功能外,根据上传的图片生成一个缩略图也是常见业务,在C#语言中,可以通过Image类提供的相关方法对图片进行操作,如指定宽高对图片进行缩放, 指定高宽裁减裁剪图片、生成图片水印等。

定义一个图片处理工具类PicDeal,该类实现了生成缩略图、在图片上生成图片水印等相应功能。具体的业务代码如下:

    /// <summary>
/// 枚举,生成缩略图模式
/// </summary>
public enum ThumbnailMod : byte
{
/// <summary>
/// HW
/// </summary>
HW,
/// <summary>
/// W
/// </summary>
W,
/// <summary>
/// H
/// </summary>
H,
/// <summary>
/// Cut
/// </summary>
Cut
};
/// <summary>
/// 操作图片类, 生成缩略图,添加水印
/// </summary>
public static class PicDeal
{
private static Hashtable htmimes = new Hashtable();
internal static readonly string AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp";
#region 生成缩略图
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="mode"></param>
/// <returns></returns>
public static bool MakeThumbnail(string originalImagePath, int width, int height, ThumbnailMod mode)
{
string thumbnailPath = originalImagePath.Substring(, originalImagePath.LastIndexOf('.')) + "s.jpg";
Image originalImage = Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = ;
int y = ;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case ThumbnailMod.HW://指定高宽缩放(可能变形)
break;
case ThumbnailMod.W://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case ThumbnailMod.H://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case ThumbnailMod.Cut://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = ;
x = (originalImage.Width - ow) / ;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = ;
y = (originalImage.Height - oh) / ;
}
break;
default:
break;
}
//新建一个bmp图片
Image bitmap = new Bitmap(towidth, toheight);
//新建一个画板
Graphics g = Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(, , towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
bool isok = false;
try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
isok = true;
}
catch (Exception)
{
thumbnailPath = originalImagePath;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
return isok;
}
#endregion
#region 在图片上生成图片水印
///// <summary>
///// 在图片上生成图片水印
///// </summary>
///// <param name="Path">原服务器图片路径</param>
///// <param name="Path_syp">生成的带图片水印的图片路径</param>
///// <param name="Path_sypf">水印图片路径</param>
/// <summary>
/// 在图片上生成图片水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_sypf">水印图片路径</param>
public static void AddWaterPic(string Path, string Path_sypf)
{
try
{
Image image = Image.FromFile(Path);
Image copyImage = Image.FromFile(Path_sypf);
Graphics g = Graphics.FromImage(image);
g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), , , copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
image.Save(Path + ".temp");
image.Dispose();
System.IO.File.Delete(Path);
File.Move(Path + ".temp", Path);
}
catch
{ }
}
#endregion
/// <summary>
/// 公共方法
/// </summary>
private static void GetImgType()
{
htmimes[".jpe"] = "image/jpeg";
htmimes[".jpeg"] = "image/jpeg";
htmimes[".jpg"] = "image/jpeg";
htmimes[".png"] = "image/png";
htmimes[".tif"] = "image/tiff";
htmimes[".tiff"] = "image/tiff";
htmimes[".bmp"] = "image/bmp";
} #region 返回新图片尺寸
/// <summary>
/// 返回新图片尺寸
/// </summary>
/// <param name="width">原始宽</param>
/// <param name="height">原始高</param>
/// <param name="maxWidth">新图片最大宽</param>
/// <param name="maxHeight">新图片最大高</param>
/// <returns></returns>
public static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
{
decimal MAX_WIDTH = (decimal)maxWidth;
decimal MAX_HEIGHT = (decimal)maxHeight;
decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;
int newWidth, newHeight;
decimal originalWidth = (decimal)width;
decimal originalHeight = (decimal)height;
if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT)
{
decimal factor;
// determine the largest factor
if (originalWidth / originalHeight > ASPECT_RATIO)
{
factor = originalWidth / MAX_WIDTH;
newWidth = Convert.ToInt32(originalWidth / factor);
newHeight = Convert.ToInt32(originalHeight / factor);
}
else
{
factor = originalHeight / MAX_HEIGHT;
newWidth = Convert.ToInt32(originalWidth / factor);
newHeight = Convert.ToInt32(originalHeight / factor);
}
}
else
{
newWidth = width;
newHeight = height;
}
return new Size(newWidth, newHeight);
}
#endregion
}

备注:原文转载自C#生成图片的缩略图_IT技术小趣屋

博主个人技术交流群:960640092,博主微信公众号如下:

【转载】C#生成图片的缩略图的更多相关文章

  1. 【转载】ASP.NET生成图片的缩略图

    图片处理是C#程序开发中时常会涉及到的一个业务,除了图像的上传.保存以及下载等功能外,根据上传的图片生成一个缩略图也是常见业务,在C#语言中,可以通过Image类提供的相关方法对图片进行操作,如指定宽 ...

  2. ASP组件AspJpeg(加水印)生成缩略图等使用方法

    ASP组件AspJpeg(加水印)生成缩略图等使用方法 作者: 字体:[增加 减小] 类型:转载 时间:2012-12-17我要评论 ASPJPEG是一款功能相当强大的图象处理组件,用它可以轻松地做出 ...

  3. 共有31款PHP 图形/图像处理开源软件(转)

    详情点击:http://www.oschina.net/project/lang/22/php?tag=141&os=0&sort=view PHP 图像处理库 Grafika Gra ...

  4. [转]非常实用的15款开源PHP类库

    源文件:http://www.csdn.net/article/2013-10-09/2817123-PHP-Libraries 英文原文:https://codegeekz.com/useful-p ...

  5. 8 个最佳 PHP 库

    PHP标准库 (SPL)的目的就是提供一组接口,让开发者在PHP5中充分利用面向对象编程.因此本文我们搜集了8个最好的,能辅助开发者简化他们的工作,为他们的开发任务服务的PHP库. 如果你喜欢本文,也 ...

  6. 15款开源PHP类库

    PHP库给开发者提供了一个标准接口,它帮助开发者在PHP里充分利用面向对象编程.这些库为特定类型的内置功能提供了一个标准的API,允许类可以与PHP引擎进行无缝的交互.此外,开发者使用这些类库还可以简 ...

  7. 24个有用的PHP类库分享

    目前,PHP是用于Web开发的最流行的脚本语言.你可以在互联网上随手找到关于PHP大量资料,包括文档.教程.工具等等.PHP不仅是一种功能丰富的语言,它还能帮助开发人员轻松地创建更好的网络环境.为了进 ...

  8. 非常实用的15款开源PHP类库

    PHP库给开发者提供了一个标准接口,它帮助开发者在PHP里充分利用面向对象编程.这些库为特定类型的内置功能提供了一个标准的API,允许类可以与PHP引擎进行无缝的交互.此外,开发者使用这些类库还可以简 ...

  9. 项目源码--Android高质量图片浏览器源码

      下载源码   技术要点: 1. 浏览所有格式的图片 2. 图片缓存到数据库 3. Sqlite数据库的高级应用 4. 文件夹缩图显示 5. 多点触控技术 6. 动画技术 7. 支持超高清图片 8. ...

随机推荐

  1. sqoop错误集锦2

    1.使用sqoop技术将mysql的数据导入到Hive出现的错误如下所示: 第一次使用命令如下所示: 1 [hadoop@slaver1 sqoop-1.4.5-cdh5.3.6]$ bin/sqoo ...

  2. redis & memcache 性能比较

    redis和memcache非常像的,都是key,value的方式,将数据存放内存中.最近在学习redis,在网上看了一些这方面的资料,有三种观点: redis读写内存比memcache快 memca ...

  3. 背水一战 Windows 10 (100) - 应用间通信: 分享

    [源码下载] 背水一战 Windows 10 (100) - 应用间通信: 分享 作者:webabcd 介绍背水一战 Windows 10 之 应用间通信 分享 示例1.本例用于演示如何开发一个分享的 ...

  4. 【ISC安全训练营】挑战价格极限第三天!!![北京]

    每到周三都觉得离周末不远啦,人生都充满的了希望,同样的,今天的优惠福利依旧超级给力,错过了可就没有了哦! 周三福利 名额 周四福利 名额 周五福利 名额 3折购买任意课程资格 3名 4折购买任意课程资 ...

  5. 图片处理服务 ImageMagick 的安装和使用

    简介 该文章使用目前官方最新版本7.0.8,这里只记录下Windows系统下的安装. 官方网站:http://www.imagemagick.org/script/index.php. ImageMa ...

  6. 前端切图神器-cutterman

    之前我写过一篇关于前端切图的博客:https://www.cnblogs.com/tu-0718/p/9741058.html 但上面的方法在切图量大时依然很费时间,下面向大家推荐这款免费切图神器 c ...

  7. redis5.0新特性

    1. redis5.0新特性 1.1. 新的Stream类型 1.1.1. 什么是Stream数据类型 抽象数据日志 数据流 1.2. 新的Redis模块API:Timers and Cluster ...

  8. 基于.NET框架的消息通信组件ZMQ资料汇编-总目录

    ZMQ是一个比较轻量级的消息通信组件,引用官方的说法: “ZMQ (以下 ZeroMQ 简称 ZMQ)是一个简单好用的传输层,像框架一样的一个 socket library,他使得 Socket 编程 ...

  9. Tools - 速查表与备忘单(Cheat Sheet)

    Cheat Sheets Rico's cheatsheets Cheat-Sheets.org Python Python Cheat sheet Python Programming Cheat ...

  10. ORM基本操作回顾

    连接数据库 默认是MySQLdb 指定引擎 dialect[+driver]: //user:password@host/dbname[?key=value..]: from sqlalchemy i ...