using System;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D; namespace DotNet.Utilities
{
public class ImageClass
{
public ImageClass()
{ } #region 缩略图
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width;
int toheight = height; int x = ;
int y = ;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case "HW": //指定高宽缩放(可能变形)
break;
case "W": //指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H": //指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "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图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.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 System.Drawing.Rectangle(, , towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
#endregion #region 图片水印
/// <summary>
/// 图片水印处理方法
/// </summary>
/// <param name="path">需要加载水印的图片路径(绝对路径)</param>
/// <param name="waterpath">水印图片(绝对路径)</param>
/// <param name="location">水印位置(传送正确的代码)</param>
public static string ImageWatermark(string path, string waterpath, string location)
{
string kz_name = Path.GetExtension(path);
if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
{
DateTime time = DateTime.Now;
string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
Image img = Bitmap.FromFile(path);
Image waterimg = Image.FromFile(waterpath);
Graphics g = Graphics.FromImage(img);
ArrayList loca = GetLocation(location, img, waterimg);
g.DrawImage(waterimg, new Rectangle(int.Parse(loca[].ToString()), int.Parse(loca[].ToString()), waterimg.Width, waterimg.Height));
waterimg.Dispose();
g.Dispose();
string newpath = Path.GetDirectoryName(path) + filename + kz_name;
img.Save(newpath);
img.Dispose();
File.Copy(newpath, path, true);
if (File.Exists(newpath))
{
File.Delete(newpath);
}
}
return path;
} /// <summary>
/// 图片水印位置处理方法
/// </summary>
/// <param name="location">水印位置</param>
/// <param name="img">需要添加水印的图片</param>
/// <param name="waterimg">水印图片</param>
private static ArrayList GetLocation(string location, Image img, Image waterimg)
{
ArrayList loca = new ArrayList();
int x = ;
int y = ; if (location == "LT")
{
x = ;
y = ;
}
else if (location == "T")
{
x = img.Width / - waterimg.Width / ;
y = img.Height - waterimg.Height;
}
else if (location == "RT")
{
x = img.Width - waterimg.Width;
y = ;
}
else if (location == "LC")
{
x = ;
y = img.Height / - waterimg.Height / ;
}
else if (location == "C")
{
x = img.Width / - waterimg.Width / ;
y = img.Height / - waterimg.Height / ;
}
else if (location == "RC")
{
x = img.Width - waterimg.Width;
y = img.Height / - waterimg.Height / ;
}
else if (location == "LB")
{
x = ;
y = img.Height - waterimg.Height;
}
else if (location == "B")
{
x = img.Width / - waterimg.Width / ;
y = img.Height - waterimg.Height;
}
else
{
x = img.Width - waterimg.Width;
y = img.Height - waterimg.Height;
}
loca.Add(x);
loca.Add(y);
return loca;
}
#endregion #region 文字水印
/// <summary>
/// 文字水印处理方法
/// </summary>
/// <param name="path">图片路径(绝对路径)</param>
/// <param name="size">字体大小</param>
/// <param name="letter">水印文字</param>
/// <param name="color">颜色</param>
/// <param name="location">水印位置</param>
public static string LetterWatermark(string path, int size, string letter, Color color, string location)
{
#region string kz_name = Path.GetExtension(path);
if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
{
DateTime time = DateTime.Now;
string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
Image img = Bitmap.FromFile(path);
Graphics gs = Graphics.FromImage(img);
ArrayList loca = GetLocation(location, img, size, letter.Length);
Font font = new Font("宋体", size);
Brush br = new SolidBrush(color);
gs.DrawString(letter, font, br, float.Parse(loca[].ToString()), float.Parse(loca[].ToString()));
gs.Dispose();
string newpath = Path.GetDirectoryName(path) + filename + kz_name;
img.Save(newpath);
img.Dispose();
File.Copy(newpath, path, true);
if (File.Exists(newpath))
{
File.Delete(newpath);
}
}
return path; #endregion
} /// <summary>
/// 文字水印位置的方法
/// </summary>
/// <param name="location">位置代码</param>
/// <param name="img">图片对象</param>
/// <param name="width">宽(当水印类型为文字时,传过来的就是字体的大小)</param>
/// <param name="height">高(当水印类型为文字时,传过来的就是字符的长度)</param>
private static ArrayList GetLocation(string location, Image img, int width, int height)
{
#region ArrayList loca = new ArrayList(); //定义数组存储位置
float x = ;
float y = ; if (location == "LT")
{
loca.Add(x);
loca.Add(y);
}
else if (location == "T")
{
x = img.Width / - (width * height) / ;
loca.Add(x);
loca.Add(y);
}
else if (location == "RT")
{
x = img.Width - width * height;
}
else if (location == "LC")
{
y = img.Height / ;
}
else if (location == "C")
{
x = img.Width / - (width * height) / ;
y = img.Height / ;
}
else if (location == "RC")
{
x = img.Width - height;
y = img.Height / ;
}
else if (location == "LB")
{
y = img.Height - width - ;
}
else if (location == "B")
{
x = img.Width / - (width * height) / ;
y = img.Height - width - ;
}
else
{
x = img.Width - width * height;
y = img.Height - width - ;
}
loca.Add(x);
loca.Add(y);
return loca; #endregion
}
#endregion #region 调整光暗
/// <summary>
/// 调整光暗
/// </summary>
/// <param name="mybm">原始图片</param>
/// <param name="width">原始图片的长度</param>
/// <param name="height">原始图片的高度</param>
/// <param name="val">增加或减少的光暗值</param>
public Bitmap LDPic(Bitmap mybm, int width, int height, int val)
{
Bitmap bm = new Bitmap(width, height);//初始化一个记录经过处理后的图片对象
int x, y, resultR, resultG, resultB;//x、y是循环次数,后面三个是记录红绿蓝三个值的
Color pixel;
for (x = ; x < width; x++)
{
for (y = ; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//获取当前像素的值
resultR = pixel.R + val;//检查红色值会不会超出[0, 255]
resultG = pixel.G + val;//检查绿色值会不会超出[0, 255]
resultB = pixel.B + val;//检查蓝色值会不会超出[0, 255]
bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//绘图
}
}
return bm;
}
#endregion #region 反色处理
/// <summary>
/// 反色处理
/// </summary>
/// <param name="mybm">原始图片</param>
/// <param name="width">原始图片的长度</param>
/// <param name="height">原始图片的高度</param>
public Bitmap RePic(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);//初始化一个记录处理后的图片的对象
int x, y, resultR, resultG, resultB;
Color pixel;
for (x = ; x < width; x++)
{
for (y = ; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
resultR = - pixel.R;//反红
resultG = - pixel.G;//反绿
resultB = - pixel.B;//反蓝
bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//绘图
}
}
return bm;
}
#endregion #region 浮雕处理
/// <summary>
/// 浮雕处理
/// </summary>
/// <param name="oldBitmap">原始图片</param>
/// <param name="Width">原始图片的长度</param>
/// <param name="Height">原始图片的高度</param>
public Bitmap FD(Bitmap oldBitmap, int Width, int Height)
{
Bitmap newBitmap = new Bitmap(Width, Height);
Color color1, color2;
for (int x = ; x < Width - ; x++)
{
for (int y = ; y < Height - ; y++)
{
int r = , g = , b = ;
color1 = oldBitmap.GetPixel(x, y);
color2 = oldBitmap.GetPixel(x + , y + );
r = Math.Abs(color1.R - color2.R + );
g = Math.Abs(color1.G - color2.G + );
b = Math.Abs(color1.B - color2.B + );
if (r > ) r = ;
if (r < ) r = ;
if (g > ) g = ;
if (g < ) g = ;
if (b > ) b = ;
if (b < ) b = ;
newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
return newBitmap;
}
#endregion #region 拉伸图片
/// <summary>
/// 拉伸图片
/// </summary>
/// <param name="bmp">原始图片</param>
/// <param name="newW">新的宽度</param>
/// <param name="newH">新的高度</param>
public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
{
try
{
Bitmap bap = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(bap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(bap, new Rectangle(, , newW, newH), new Rectangle(, , bap.Width, bap.Height), GraphicsUnit.Pixel);
g.Dispose();
return bap;
}
catch
{
return null;
}
}
#endregion #region 滤色处理
/// <summary>
/// 滤色处理
/// </summary>
/// <param name="mybm">原始图片</param>
/// <param name="width">原始图片的长度</param>
/// <param name="height">原始图片的高度</param>
public Bitmap FilPic(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);//初始化一个记录滤色效果的图片对象
int x, y;
Color pixel; for (x = ; x < width; x++)
{
for (y = ; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
bm.SetPixel(x, y, Color.FromArgb(, pixel.G, pixel.B));//绘图
}
}
return bm;
}
#endregion #region 左右翻转
/// <summary>
/// 左右翻转
/// </summary>
/// <param name="mybm">原始图片</param>
/// <param name="width">原始图片的长度</param>
/// <param name="height">原始图片的高度</param>
public Bitmap RevPicLR(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);
int x, y, z; //x,y是循环次数,z是用来记录像素点的x坐标的变化的
Color pixel;
for (y = height - ; y >= ; y--)
{
for (x = width - , z = ; x >= ; x--)
{
pixel = mybm.GetPixel(x, y);//获取当前像素的值
bm.SetPixel(z++, y, Color.FromArgb(pixel.R, pixel.G, pixel.B));//绘图
}
}
return bm;
}
#endregion #region 上下翻转
/// <summary>
/// 上下翻转
/// </summary>
/// <param name="mybm">原始图片</param>
/// <param name="width">原始图片的长度</param>
/// <param name="height">原始图片的高度</param>
public Bitmap RevPicUD(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);
int x, y, z;
Color pixel;
for (x = ; x < width; x++)
{
for (y = height - , z = ; y >= ; y--)
{
pixel = mybm.GetPixel(x, y);//获取当前像素的值
bm.SetPixel(x, z++, Color.FromArgb(pixel.R, pixel.G, pixel.B));//绘图
}
}
return bm;
}
#endregion #region 压缩图片
/// <summary>
/// 压缩到指定尺寸
/// </summary>
/// <param name="oldfile">原文件</param>
/// <param name="newfile">新文件</param>
public bool Compress(string oldfile, string newfile)
{
try
{
System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
Size newSize = new Size(, );
Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(outBmp);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(, , newSize.Width, newSize.Height), , , img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[];
quality[] = ;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[] = encoderParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = ; x < arrayICI.Length; x++)
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x]; //设置JPEG编码
break;
}
img.Dispose();
if (jpegICI != null) outBmp.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
outBmp.Dispose();
return true;
}
catch
{
return false;
}
}
#endregion #region 图片灰度化
public Color Gray(Color c)
{
int rgb = Convert.ToInt32((double)(((0.3 * c.R) + (0.59 * c.G)) + (0.11 * c.B)));
return Color.FromArgb(rgb, rgb, rgb);
}
#endregion #region 转换为黑白图片
/// <summary>
/// 转换为黑白图片
/// </summary>
/// <param name="mybt">要进行处理的图片</param>
/// <param name="width">图片的长度</param>
/// <param name="height">图片的高度</param>
public Bitmap BWPic(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);
int x, y, result; //x,y是循环次数,result是记录处理后的像素值
Color pixel;
for (x = ; x < width; x++)
{
for (y = ; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
result = (pixel.R + pixel.G + pixel.B) / ;//取红绿蓝三色的平均值
bm.SetPixel(x, y, Color.FromArgb(result, result, result));
}
}
return bm;
}
#endregion #region 获取图片中的各帧
/// <summary>
/// 获取图片中的各帧
/// </summary>
/// <param name="pPath">图片路径</param>
/// <param name="pSavePath">保存路径</param>
public void GetFrames(string pPath, string pSavedPath)
{
Image gif = Image.FromFile(pPath);
FrameDimension fd = new FrameDimension(gif.FrameDimensionsList[]);
int count = gif.GetFrameCount(fd); //获取帧数(gif图片可能包含多帧,其它格式图片一般仅一帧)
for (int i = ; i < count; i++) //以Jpeg格式保存各帧
{
gif.SelectActiveFrame(fd, i);
gif.Save(pSavedPath + "\\frame_" + i + ".jpg", ImageFormat.Jpeg);
}
}
#endregion
}
}

操作Image,封装的方法的更多相关文章

  1. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  2. Visual Studio快速封装字段方法

    在面向对象的编程中我们常常要将各个字段封装为属性,但是当字段多的时候往往这个重复的操作会大大降低我们的开发效率,那么如何才能快速的封装字段呢?下面就给大家2个解决方法: 1.使用封装字段方法: 选中字 ...

  3. C# .NET更智能的数据库操作的封装

    前述: 对数据库操作的封装,相信网络上已经有一大堆,ORM框架,或者是.NET本身的EF,都很好的支持数据库操作.这篇文章是分享自己所思考的,对数据库操作的简单封装.我对于这篇文章,认为被浏览者所关注 ...

  4. 手把手封装数据层之DataUtil数据库操作的封装

    上一篇我们写完了数据库连接的封装 没有看的请移步上一篇关于数据库连接的内容 这次我们讲数据库操作的封装.数据库的操作就是增删改查:心再大一点就可以直接分为查询和其他. 因为查询是有返回对象的,而其他都 ...

  5. 基于 Aspose.Cells与XML导入excel 数据----操作类封装

    前言 导入excel数据, 在每个项目中基本上都会遇到,第三方插件或者基于微软office,用的最多的就是npoi,aspose.cells和c#基于office这三种方式,其中各有各的优缺点,在这也 ...

  6. Asp.Net Core 2.0 项目实战(4)ADO.NET操作数据库封装、 EF Core操作及实例

    Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...

  7. 数据操作的封装--sqlhelper

    为了提高软件的灵活性和可维护性,软件的代码须要科学的管理.我们引入了架构这个词.设计模式提醒我们,软件中反复性的代码须要封装起来. 近期在做收费系统时.须要和数据库进行频繁的联系.既然是反复的使用,就 ...

  8. Vue之优化封装请求方法

    Vue之优化封装请求方法 对于代码中的请求操作 1.接口请求可能需要重用 2.实际工作中,接口非常容易变动, 改起来很麻烦! 我们建议的做法是把所有的请求都封装成函数然后统一的>###组织到模块 ...

  9. Android网络操作的几种方法

    安卓开发软件:AndroidStudio 服务器软件:Myeclipse+Tomcat 首先无论是哪种方式,安卓手机软件要想联网,必须要申请联网权限(android.permission.INTERN ...

  10. JQuery中操作Css样式的方法

    JQuery中操作Css样式的方法//1.获取和设置样式 $("#tow").attr("class")获取ID为tow的class属性 $("#tw ...

随机推荐

  1. input只改变光标的颜色 不改变字的颜色

    color: red; text-shadow: 0px 0px 0px #000; -webkit-text-fill-color: transparent;把这些放到input里文字通过阴影实现 ...

  2. 【CF Round 434 B. Which floor?】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  3. 模拟赛 yjqa

    考场上怕是石乐志. 状态设计还是很自然的,求什么设什么. f[i]表示前i个人安排好,电梯最早回到0层的时间 转移的话,枚举上一次最后一个带走的是谁 f[i]=min(max(f[j],t[i])+2 ...

  4. nginx,docker反向代理

    1. [root@javanginx ~]# cat /etc/nginx/nginx.conf user root root;worker_processes 4;error_log /var/lo ...

  5. apply()和call()

    每个函数都包含俩个非继承而来的方法:apply() 和 call(),这俩个方法的用途都是在特定的作用域中调用函数,实际上等于设置函数体内this对象的值,以扩充函数赖以运行的作用域.一般来讲,thi ...

  6. Eclipse+Tomcat实现热部署/热加载配置,修改java代码无需重启tomcat

    一.Tomcat热加载配置 Eclipse Package Explorer中找到Servers,点击你所需要运行的tomcat的config配置文件,例如 demo-config,双击该文件夹下的s ...

  7. 【BZOJ4774】修路 [斯坦纳树]

    修路 Time Limit: 20 Sec  Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 5 5 2 ...

  8. hdu 2817 A sequence of numbers(快速幂取余)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 题目大意:给出三个数,来判断是等差还是等比数列,再输入一个n,来计算第n个数的值. #inclu ...

  9. HDU 4344 大数分解大素数判定

    这里贴个模板吧.反正是不太理解 看原题就可以理解用法!! #include <cstdio> #include <iostream> #include <algorith ...

  10. Jmeter接口测试常见的乱码问题三种解决方法

    使用Jmeter时经常遇到中文乱码问题,下面总结三种常用的解决方式. 1. 2.在Jmeter安装文件bin中找到jmeter.properties,打开jmeter.properties,搜索“IS ...