【C#公共帮助类】 Image帮助类
大家知道,开发项目除了数据访问层很重要外,就是Common了,这里就提供了强大且实用的工具。
【C#公共帮助类】 Convert帮助类
Image类:
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing.Imaging; using System.Drawing; using System.Web; namespace Common {public static class Image{#region 图片格式转换/// <summary>/// 图片格式转换/// </summary>/// <param name="OriFilename">原始文件相对路径</param>/// <param name="DesiredFilename">生成目标文件相对路径</param>/// <returns></returns>/// JPG采用的是有损压缩所以JPG图像有可能会降低图像清晰度,而像素是不会降低的 /// GIF采用的是无损压缩所以GIF图像是不会降低原图图像清晰度和像素的,但是GIF格式只支持256色图像。public static bool ConvertImage(string OriFilename, string DesiredFilename){string extname = DesiredFilename.Substring(DesiredFilename.LastIndexOf('.')+1).ToLower();ImageFormat DesiredFormat;//根据扩张名,指定ImageFormatswitch (extname){case "bmp":DesiredFormat = ImageFormat.Bmp;break;case "gif":DesiredFormat = ImageFormat.Gif;break;case "jpeg":DesiredFormat = ImageFormat.Jpeg;break;case "ico":DesiredFormat = ImageFormat.Icon;break;case "png":DesiredFormat = ImageFormat.Png;break;default:DesiredFormat = ImageFormat.Jpeg;break;}try{System.Drawing.Image imgFile = System.Drawing.Image.FromFile(WebPathTran(OriFilename));imgFile.Save(WebPathTran(DesiredFilename), DesiredFormat);return true;}catch {return false;}}#endregion#region 图片缩放/// <summary>/// 图片固定大小缩放/// </summary>/// <param name="OriFileName">源文件相对地址</param>/// <param name="DesiredFilename">目标文件相对地址</param>/// <param name="IntWidth">目标文件宽</param>/// <param name="IntHeight">目标文件高</param>/// <param name="imageFormat">图片文件格式</param>public static bool ChangeImageSize(string OriFileName, string DesiredFilename, int IntWidth, int IntHeight, ImageFormat imageFormat){string SourceFileNameStr =WebPathTran(OriFileName); //来源图片名称路径string TransferFileNameStr = WebPathTran(DesiredFilename); //目的图片名称路径FileStream myOutput =null;try{System.Drawing.Image.GetThumbnailImageAbort myAbort = new System.Drawing.Image.GetThumbnailImageAbort(imageAbort);Image SourceImage = System.Drawing.Image.FromFile(OriFileName);//来源图片定义Image TargetImage = SourceImage.GetThumbnailImage(IntWidth, IntHeight, myAbort, IntPtr.Zero); //目的图片定义//将TargetFileNameStr的图片放宽为IntWidth,高为IntHeight myOutput = new FileStream(TransferFileNameStr, FileMode.Create, FileAccess.Write, FileShare.Write);TargetImage.Save(myOutput, imageFormat);myOutput.Close();return true;}catch {myOutput.Close();return false;}}private static bool imageAbort(){return false;}#endregion#region 文字水印/// <summary>/// 文字水印/// </summary>/// <param name="wtext">水印文字</param>/// <param name="source">原图片物理文件名</param>/// <param name="target">生成图片物理文件名</param>public static bool ImageWaterText(string wtext,string source, string target){bool resFlag = false;Image image = Image.FromFile(source);Graphics graphics = Graphics.FromImage(image);try{graphics.DrawImage(image, 0, 0, image.Width, image.Height);Font font = new System.Drawing.Font("Verdana", 60);Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Green);graphics.DrawString(wtext, font, brush, 35, 35);image.Save(target);resFlag = true;}catch (Exception){throw;}finally {graphics.Dispose();image.Dispose();}return resFlag;}#endregion#region 图片水印/// <summary>/// 在图片上生成图片水印/// </summary>/// <param name="Path">原服务器图片路径</param>/// <param name="Path_syp">生成的带图片水印的图片路径</param>/// <param name="Path_sypf">水印图片路径</param>public static bool ImageWaterPic(string source, string target, string waterPicSource){bool resFlag = false;Image sourceimage = Image.FromFile(source);Graphics sourcegraphics = Graphics.FromImage(sourceimage);Image waterPicSourceImage = Image.FromFile(waterPicSource);try{sourcegraphics.DrawImage(waterPicSourceImage, new System.Drawing.Rectangle(sourceimage.Width - waterPicSourceImage.Width, sourceimage.Height - waterPicSourceImage.Height, waterPicSourceImage.Width, waterPicSourceImage.Height), 0, 0, waterPicSourceImage.Width, waterPicSourceImage.Height, GraphicsUnit.Pixel);sourceimage.Save(target);}catch (Exception){throw;}finally{sourcegraphics.Dispose();sourceimage.Dispose();waterPicSourceImage.Dispose();}return resFlag;}#endregion/// <summary>/// 路径转换(转换成绝对路径)/// </summary>/// <param name="path"></param>/// <returns></returns>private static string WebPathTran(string path){try{return HttpContext.Current.Server.MapPath(path);}catch{return path;}}} }
转载于:https://www.cnblogs.com/eadily-dream/p/5525899.html
【C#公共帮助类】 Image帮助类相关推荐
- MFC常用类、成员函数、数组类、Cstring类、CTime类、CPoint类
MFC数组类CByteArray: CDWordArray: CPtrArray: CUIntArray: CWordArray: CStringArray: 常用成员函数 1.int Add( AR ...
- 类5-类的继承、虚函数、纯虚函数、虚析构函数
一.类的继承 就像家谱一样,就是一个继承图.爷爷-父亲-儿子-孙子等.类也一样,上面的类称为基类,也称父类.基类下面的类叫子类也叫派生类. 子类对父类的一些属性等有所继承也有所发展,因此才有了类的继承 ...
- c++ 嵌套私有类_嵌套类和私有方法
c++ 嵌套私有类 当您在另一个类中有一个类时,他们可以看到彼此的private方法. 在Java开发人员中并不为人所知. 面试过程中的许多候选人说, private是一种可见性,它使代码可以查看成员 ...
- UML类图与类间六种关系表示
1.类与类图 类封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性,操作,关系的对象集合的总称. 类图是使用频率最高的UML图之一. 类图用于描述系统中所包含的类以及它们之间的相互关系,帮助 ...
- 【转载】c++之类的基本操作(c++ primer 的读书笔记 ,类对象, 类用户, 类成员的含义)
一前言 看c++ primer有一个地方看的云里雾里的,这么一段话 可以认为 protected 访问标号是 private 和 public 的混合: • 像 private 成员一样,protec ...
- 【JAVA SE】第十章 String类、StringBuffer类和StringBuilder类
第十章 String类.StringBuffer类和StringBuilder类 文章目录 第十章 String类.StringBuffer类和StringBuilder类 一.String类 1.创 ...
- java基类和派生类圆_java – 当基类和派生类都具有相同名称的变量时会发生什么...
在这些类中考虑int变量: class Foo { public int a = 3; public void addFive() { a += 5; System.out.print("f ...
- 超六类与七类等多类网线的比较—Vecloud
七类线有哪些特点?与其他线缆有何区别? 什么是七类网线? 七类标准是一套在100欧姆双绞线上支持最高600MHz带宽传输布线标准.七类线是一种8芯屏蔽线,每对都有一个屏蔽层(一般为金属箔屏蔽 DINT ...
- 设计一个分数类java_Java 有理数类 分数类 Rational类的设计与实现
要实现Rational类的加减乘除,要实现其可比较性,要覆盖toString()方法,要实现不同数据类型的转换等. package chapter14; public class Rational e ...
- C++面向对象程序设计实验题:有理数类(分数类)设计
C++实验题:有理数类(分数类)设计 问题描述: 根据main测试函数设计有理数类CRational,数据成员由分子.分母组成(都是整型,应表示成最简形式),完成分数显示(如用户输入的分子是4,分母是 ...
最新文章
- 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】)...
- QT的QScriptEngineAgent类的使用
- (转)使用DataGridView控件常见问题解答
- 输入输出挂,手动扩栈。
- Spark团队开源新项目MLflow发布0.2版本,内置TensorFlow集成
- 40套各种风格住宿酒店行业网站html5模板大气商务酒店网站模板度假村酒店官方网站模板旅行酒店宾馆整站模板html5网页静态模板Bootstrap扁平化网站源码css3手机seo自适响应
- 使用transmission下载BT
- MIT Molecular Biology 笔记1 DNA的复制,染色体组装
- 【1194】移动路线
- MinIO分布式文件服务器搭建与入门
- cdrx4自动排版步骤_教你怎样在CDR里怎样编号自动排版
- PSGAN——姿态稳健型可感知空间式生成对抗网络论文详细解读与整理
- 使用IR2101半桥驱动电机的案例
- Youtube推荐系统论文-《Deep Neural Networks for YouTube Recommendations》-简单总结
- Python爬虫抓取b站排行榜
- 磁盘、分区及Linux文件系统 [Disk, Partition, Linux File System]
- 工业树莓派应用案例3:印后设备自动化
- JAVA 数字转汉字数字
- JS公式实例一:画圆
- 【学习笔记】认知神经科学