【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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/374630.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java泛型快速教程

泛型是Java SE 5.0引入的一种Java功能&#xff0c;在其发布几年后&#xff0c;我发誓那里的每个Java程序员不仅听说过它&#xff0c;而且已经使用过它。 关于Java泛型&#xff0c;有很多免费和商业资源&#xff0c;而我使用的最佳资源是&#xff1a; Java教程 Java泛型和集合…

876. 链表的中间结点

给定一个头结点为 head 的非空单链表&#xff0c;返回链表的中间结点。 如果有两个中间结点&#xff0c;则返回第二个中间结点 代码一&#xff1a; 自己想的一个方法 class Solution {public ListNode middleNode(ListNode head) {ListNode p1 head;ListNode p2 head;//i,j…

Hive查询Join

Select a.val,b.val From a [Left|Right|Full Outer] Join b On (a.keyb.key); 现有两张表&#xff1a;sales 列出了人名及其所购商品的 ID&#xff1b;things 列出商品的 ID 和名称&#xff1a; hive> select * from sales; OK Joe 2 Hank 4 Ali 0 Eve 3 Ha…

jquery 获取easyui combobox选中的值

$(#comboboxlist).combobox(getValue);转载于:https://www.cnblogs.com/ftm-datablogs/p/5526857.html

调度Java应用程序中的主体

许多项目需要计划功能&#xff0c;例如我们计划的工作&#xff0c;重复的工作&#xff0c;异步执行等。 我们的首选方法是使用企业工作调度程序&#xff0c;例如OpenSymphony的Quartz。 使用计划任务进行编码时&#xff0c;最棘手的部分之一是执行部分。 这里的主要经验法则是…

继承映射关系 joinedsubclass的查询

会出现下面这样的错一般是配置文件中的mapping和映射文件中的package路径或者class中的name路径不一致 org.hibernate.MappingException: Unknown entity: com.zh.hibernate.joinedsubclass.Student at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(Sessi…

Spark系列—02 Spark程序牛刀小试

一、执行第一个Spark程序 1、执行程序 我们执行一下Spark自带的一个例子&#xff0c;利用蒙特卡罗算法求PI&#xff1a; 启动Spark集群后&#xff0c;可以在集群的任何一台机器上执行一下命令&#xff1a; /home/spark/spark-1.6.1-bin-hadoop2.6/bin/spark-submit \ --class o…

JVM选项:-client vs -server

您是否曾经在运行Java应用程序时想知道-client或-server开关是什么&#xff1f; 例如&#xff1a; javaw.exe -client com.blogspot.sdoulger.LoopTest也显示在java.exe的“帮助”中&#xff0c;例如&#xff0c;其中的选项包括&#xff1a; -client选择“客户端” VM -serv…

网页前台小知识

1.左右布局div块自适应&#xff0c;首先外边套一个div,把宽度固定一个px&#xff0c;然后margin设为&#xff10; atuo&#xff1b;这样他会根据窗口大小自动变换左右距离&#xff0e;就这么简单&#xff1c;/p> 2.多个标签共用一个样式&#xff0c;用&#xff0c;分隔开 p…

统计字符串每个字符出现的次数

//str是个只包含小写字母的字符串&#xff0c;以下是统计每个字符出现的频数 int[] cnt new int[26];//toCharArray() for (char ch : str.toCharArray()) {cnt[ch - a]; }//charAt() for(int i 0;i<str.length;i){char ch str.charAt(i);cnt[ch - a]; }

在Java 7中处理文件

以下是The Well-Grounded Java Developer的草稿的修改后的片段。 它使您快速了解与以前版本相比&#xff0c;在Java 7中操作文件要容易得多。 通过使用新的Files类及其许多实用程序方法&#xff0c;您可以仅用一行代码就可以对文件执行以下操作&#xff1a; 创建 删除 复制 …

3.1存储管理操作系统

存储器管理的对象是主存&#xff08;内存&#xff09;。其主要功能包含分配和回收主存空间、提高主存的利用率、扩充主存、对主存信息实现有效保护。存储器的结构为&#xff1a;寄存去、缓存、主存、外存。逻辑地址&#xff08;对用户角度。程序存放的位置&#xff09;、物理地…

学习教材《构建之法》遇到的问题及思路

在学习中每个人都会遇到各种各样的问题&#xff0c;下面就是我遇到的问题及可能解决问题的思路。 1.如何写好程序的注释&#xff0c;每个人都会写注释&#xff0c;但是&#xff0c;需要注释什么&#xff1f; 思路&#xff1a;注释是为了解释程序做什么&#xff0c;为什么要这样…

了解和扩展Java ClassLoader

Java ClassLoader是项目开发中Java的关键但很少使用的组件之一。 就我个人而言&#xff0c;我从未在任何项目中扩展ClassLoader&#xff0c;但是拥有自己的可以自定义Java类加载的ClassLoader的想法让我感到很兴奋。 本文将概述Java类加载&#xff0c;然后继续创建自定义ClassL…

CAD教程-AL对其命令

AL可以实现不规则的对其功能 1.第一步按下AL&#xff0c;按下Enter 2.选择第一个源点 3.选择第一个目标点 4.选择第二个源点 5.选择第二个目标点 6.按下Enter&#xff0c;完成移位 转载于:https://www.cnblogs.com/weloveshare/p/4739873.html

使用Spring将POJO公开为JMX MBean

这是一个非常不错的教程&#xff0c;介绍了如何通过我们最新的JCG合作伙伴 “ The Holy Java ”博客&#xff08;很酷的名字&#xff09;实现“ 用Spring轻松将POJO作为JMX MBean公开 ”。 &#xff08;注意&#xff1a;对原始帖子进行了少量编辑以提高可读性&#xff09; Jav…

mysql 5.1由于Host为localhost的用户为空,密码为空,导致本地用户无法登陆。

不说了。直接上mysql的用户数据&#xff0c;第四列里面&#xff0c;host为localhost&#xff0c;用户为空&#xff0c;密码为空。 导致在本地登陆的时候除了root的账户外&#xff0c;其他账号不需要密码即可登陆&#xff0c;并且影响host为 %的用户登陆。 这里只需要删除对应的…

scala 88 for替换map,flatmap,filtermap,for,scala,flatmap

王家林亲授《DT大数据梦工厂》大数据实战视频“Scala深入浅出实战经典”视频、音频和PPT下载&#xff01;第88讲&#xff1a;Scala中使用For表达式实现map、flatMap、filter百度云盘&#xff1a;http://pan.baidu.com/s/1mgtgcIG360云盘&#xff1a;http://yunpan.cn/cdXsbctXf…

简单阐述下OC中UIImage三种创建方式~~~

一. 直接使用imageNamed进行创建 1 UIImage * image [UIImage imageNamed:"1.jpg"]; 简单说一下这种方式的优缺点&#xff1a; 优点&#xff1a;代码量少&#xff0c;一行代码就可以搞定。当程序中多次加载这张图片时&#xff0c;系统会指向同一块内存&#xff0c;…

(一二四)tableView的多组数据展示和手动排序

最近在写一个轻量级的网络游戏&#xff0c;遇到了技能优先顺序手动排序的需求&#xff0c;我就想到了iOS自带的tableView编辑功能&#xff0c;对其进行了初步探索&#xff0c;最后做出的效果如下图所示&#xff1a; 点击左边可以删除&#xff0c;拖住右边可以手动排序&#xff…