[GDI+] C# ImageClass帮助类教程与源码下载 (转载)
功能如下图片
主要功能有:
缩略图片,图片水印,文字水印,调整光暗,反色处理,浮雕处理,拉伸处理,
左右翻转,上下翻转,压缩图片,图片灰度化,转换为黑白图片,获取图片中的各帧
看代码吧
/// <summary>
/// 类说明:Assistant
/// 编 码 人:苏飞
/// 联系方式:361983679
/// 更新网站:[url=http://www.sufeinet.com/thread-655-1-1.html]http://www.sufeinet.com/thread-655-1-1.html[/url]
/// </summary>
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)
{
return Compress(oldfile, newfile, , );
}
/// <summary>
/// 压缩指定尺寸,如果写的和图片大家一样表示大小不变,只是把图片压缩下一些
/// </summary>
/// <param name="oldfile">原文件</param>
/// <param name="newfile">新文件</param>
/// <param name="width">长</param>
/// <param name="height">高</param>
public bool Compress(string oldfile, string newfile, int width, int height)
{
try
{
System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
Size newSize = new Size(width, height);
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
}
}
[GDI+] C# ImageClass帮助类教程与源码下载 (转载)的更多相关文章
- [IO] C# DirFileHelper文件与文件夹操作类教程与源码下载 (转载)
点击下载 DirFileHelper.zip 主要功能如下所示 // 摘要: // 向文本文件的尾部追加内容 // // 参数: // filePath: // 文件的绝对路径 // // conte ...
- [GDI+] C# ImageDown帮助类教程与源码下载 (转载)
点击下载 ImageDown.zip 1.下载图片到本地代码如下 /// <summary> /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url=h ...
- [网络] C# NetHelper网络通信编程类教程与源码下载
点击下载 NetHelper.zip 主要功能如下所示 检查设置的IP地址是否正确,返回正确的IP地址 检查设置的端口号是否正确,返回正确的端口号 将字符串形式的IP地址转换成IPAddress对象 ...
- [上传下载] C# ImageUpload图片上传类教程与源码下载 (转载)
点击下载 ImageUpload.zip 功能如下图片1.设置属性后上传图片,用法如下 /// <summary> /// 图片上传类 /// </summary> //--- ...
- ExcelReport.cs Excel操作类、实例源码下载
标题一.告别ASP.NET操作EXCEL的烦恼标题二.ASP.NET操作EXCEL 合并单元格 大全... cnblogs/hanzhaoxin/韩兆新的博客园ExcelReport第一篇:使用Exc ...
- 50余本中外Python电子教程及源码下载地址
链接:http://pan.baidu.com/s/1c0VTwsC 密码:hapu
- 【绝对给力】Android开发免豆资料(教程+工具+源码)地址汇总
教程下载: [免费]android界面效果全汇总.pdf http://down.51cto.com/data/209179 Android终极开发教程[pdf高清版] http://down.51c ...
- LIRE教程之源码分析 | LIRE Tutorial of Analysis of the Source Code
LIRE教程之源码分析 |LIRE Tutorial of Analysis of the Source Code 最近在做地理图像识别和检索的研究,发现了一个很好用的框架LIRE,遂研究了一通.网上 ...
- 自定义View系列教程04--Draw源码分析及其实践
深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...
随机推荐
- 【CF】110 Div.1 B. Suspects
这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...
- JIRA Cannot Start Due to 'unable to clean the cache directory: /opt/jira/plugins/.osgi-plugins/felix'
Symptoms After restarting JIRA, the following error appeared: JIRA Startup Failed You cannot access ...
- List 三种遍历的方法
学习java语言list遍历的三种方法,顺便测试各种遍历方法的性能,测试方法为在ArrayList中插入1千万条记录,然后遍历ArrayList,发现了一个奇怪的现象,测试代码如下: package ...
- Ruby跳出多层循环 catch...throw
在编码的时候,有时候会遇到嵌套循环的情况,最内部的循环结束的时候,想跳出所有循环,这个时候我们往往采用通过内部循环设置一个flag来控制外部跳出循环条件,比如: #encoding:utf-8 for ...
- [置顶]VC2013的一个bug
[置顶]VC2013的一个bug 前段时间在尝试使用一个C++的GUI库nana.这个库最大的特点在于使用现代C++风格去编写GUI程序,而不需要使用大量的比较丑陋的代码(如MFC中的各种宏),或者其 ...
- Red5 配置RTMPT
在网上找了挺长时间,终于把RTMPT的问题解决了. 我要做的项目需要实现这样的流程: 流服务器输出RTMP码流 Red5接收码流该RTMP码流 Red5把RTMP码流转成RTMPT流,并向客户端广播 ...
- JavaScript高级程序设计61.pdf
JSON对象 早期的JSON解析器就是使用JavaScript的eval()函数,ECMAScript5对解析JSON的行为做出了规定,定义了全局对象JSON. JSON对象有2个方法:stringi ...
- WCF、.Net Remoting、Web Service概念及区别
此文章主要参考http://www.cnblogs.com/weiweibtm/archive/2013/06/21/3148583.html 参考书籍<WCF全面解析上册>.<WC ...
- 【转】Flask快速入门
迫不及待要开始了吗?本页提供了一个很好的 Flask 介绍,并假定你已经安装好了 Flask.如果没有,请跳转到 安装 章节. 一个最小的应用 一个最小的 Flask 应用看起来会是这样: from ...
- JOptionPanel类的解析
JOptionPane类提示框的一些常用的方法 XMLOracleSwing 最近在做swing程序中遇到使用消息提示框的,JOptionPane类其中封装了很多的方法. 很方便的,于是就简单的整理 ...