图片处理是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,博主微信公众号如下:

【转载】ASP.NET生成图片的缩略图的更多相关文章

  1. 【转载】C#生成图片的缩略图

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

  2. 【转载】Asp.Net生成图片验证码工具类

    在Asp.Net应用程序中,很多时候登陆页面以及其他安全重要操作的页面需要输入验证码,本文提供一个生成验证码图片的工具类,该工具类通过随机数生成验证码文本后,再通过C#中的图片处理类位图类,字体类,一 ...

  3. asp.net中生成缩略图并添加版权实例代码

    这篇文章介绍了asp.net中生成缩略图并添加版权实例代码,有需要的朋友可以参考一下 复制代码代码如下: //定义image类的对象 Drawing.Image image,newimage; //图 ...

  4. 转载 ASP.NET Web API 学习

    转载关于ASP.NET Web API 的学习网址 http://www.cnblogs.com/aehyok/p/3432158.html http://www.mashangpiao.net/Ar ...

  5. 转载ASP.NET MVC 和ASP.NET Web Form简单区别

    转载原地址 http://www.cnblogs.com/lei2007/p/3315431.html 概论: Asp.net  微软 提供web开发框架或者技术.分Web Form和ASP.NET  ...

  6. 转载ASP.NET 状态管理Application,Session,Cookie和ViewState用法

    转载原地址 http://www.cnblogs.com/cuishao1985/archive/2009/09/24/1573403.html ASP.NET状态管理 APPlication,Ses ...

  7. 转载 asp.net中ViewState的用法详解

    转载原地址: http://www.jb51.net/article/73662.htm 在web窗体控件设置为runat = "server",这个控件会被附加一个隐藏的属性_V ...

  8. 转载 ASP.NET MVC中使用ASP.NET Identity

    转载原地址: http://blog.jobbole.com/90695/ 在之前的文章中,我为大家介绍了OWIN和Katana,有了对它们的基本了解后,才能更好的去学习ASP.NET Identit ...

  9. 转载ASP.NET MVC中Session的处理机制

    本文章转载自 http://www.cnblogs.com/darrenji/p/3951065.html ASP.NET MVC中的Session以及处理方式   最近在ASP.NET MVC项目中 ...

随机推荐

  1. github install

    1.安装git依赖包 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUti ...

  2. git无法同步

    出现问题: fatal: destination path 'test' already exists and is not an empty directory. 解决方法如下: git init ...

  3. 吴恩达机器学习笔记16-决策边界(decision boundary)

    现在讲下决策边界(decision boundary)的概念.这个概念能更好地帮助我们理解逻辑回归的假设函数在计算什么. 在逻辑回归中,我们预测:当ℎ

  4. Python面向对象1:类与对象

    Python的面向对象- 面向对象编程 - 基础 - 公有私有 - 继承 - 组合,Mixin- 魔法函数 - 魔法函数概述 - 构造类魔法函数 - 运算类魔法函数 # 1. 面向对象概述(Objec ...

  5. DOS窗口查看端口占用

    背景:最近用tomcat,一直访问不了,要账号密码登录,最后发现问题原因根本是tomcat的默认端口号8080被占用了,下面介绍如何通过dos窗口找到占用端口的进程. 方法: 打开DOS窗口,输入ne ...

  6. mysql原生sql盘点

    select*from (select*from test1 union all select*from test2 ) //两个查询的数据行数需要对应一致,且名字as 一致. 1.如果直接用如下sq ...

  7. Java 11 已发布,String 还能这样玩!

    在文章<Java 11 正式发布,这 8 个逆天新特性教你写出更牛逼的代码>中,我有介绍到 Java 11 的八个新特性,其中关于 String 加强部分,我觉得有点意思,这里单独再拉出来 ...

  8. vue的Vuex

    网上也很多文章,但解释起来的确玄乎,小白们很难理解到位. 自问文笔没大神们好只是自己了解了掌握了Vuex用法以及主要思路 但要我解释起来也只能参考大神们的说法 Vuex就是一个全局变量,而这个全局变量 ...

  9. ModelState 错误信息输出

    在MVC的项目中,我们通常情况下,为了方便(偷懒),会直接使用 !ModelState.IsValid 来判断实体的验证是否正确,但是这样对于用户的体验是不好的,当填写的内容比较多的时候,用户需要自己 ...

  10. DataCleaner(4.5)第二章

    Chapter 2. Getting started with DataCleaner desktop Table of Contents |--Installing the desktop appl ...