using System;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace StudyCommon
{
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 = 0;
int y = 0;
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 = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一個(gè)bmp圖片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一個(gè)畫(huà)板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設(shè)置高質(zhì)量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫(huà)布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小繪制原圖片的指定部分
//第一個(gè):對(duì)哪張圖片進(jìn)行操作。
//二:畫(huà)多么大。
//三:畫(huà)那塊區(qū)域欠母。
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, 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">需要加載水印的圖片路徑(絕對(duì)路徑)</param>
/// <param name="waterpath">水印圖片(絕對(duì)路徑)</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[0].ToString()), int.Parse(loca[1].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 = 0;
int y = 0;
if (location == "LT")
{
x = 10;
y = 10;
}
else if (location == "T")
{
x = img.Width / 2 - waterimg.Width / 2;
y = img.Height - waterimg.Height;
}
else if (location == "RT")
{
x = img.Width - waterimg.Width;
y = 10;
}
else if (location == "LC")
{
x = 10;
y = img.Height / 2 - waterimg.Height / 2;
}
else if (location == "C")
{
x = img.Width / 2 - waterimg.Width / 2;
y = img.Height / 2 - waterimg.Height / 2;
}
else if (location == "RC")
{
x = img.Width - waterimg.Width;
y = img.Height / 2 - waterimg.Height / 2;
}
else if (location == "LB")
{
x = 10;
y = img.Height - waterimg.Height;
}
else if (location == "B")
{
x = img.Width / 2 - waterimg.Width / 2;
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">圖片路徑(絕對(duì)路徑)</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[0].ToString()), float.Parse(loca[1].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">圖片對(duì)象</param>
/// <param name="width">寬(當(dāng)水印類型為文字時(shí),傳過(guò)來(lái)的就是字體的大小)</param>
/// <param name="height">高(當(dāng)水印類型為文字時(shí),傳過(guò)來(lái)的就是字符的長(zhǎng)度)</param>
private static ArrayList GetLocation(string location, Image img, int width, int height)
{
#region
ArrayList loca = new ArrayList(); //定義數(shù)組存儲(chǔ)位置
float x = 10;
float y = 10;
if (location == "LT")
{
loca.Add(x);
loca.Add(y);
}
else if (location == "T")
{
x = img.Width / 2 - (width * height) / 2;
loca.Add(x);
loca.Add(y);
}
else if (location == "RT")
{
x = img.Width - width * height;
}
else if (location == "LC")
{
y = img.Height / 2;
}
else if (location == "C")
{
x = img.Width / 2 - (width * height) / 2;
y = img.Height / 2;
}
else if (location == "RC")
{
x = img.Width - height;
y = img.Height / 2;
}
else if (location == "LB")
{
y = img.Height - width - 5;
}
else if (location == "B")
{
x = img.Width / 2 - (width * height) / 2;
y = img.Height - width - 5;
}
else
{
x = img.Width - width * height;
y = img.Height - width - 5;
}
loca.Add(x);
loca.Add(y);
return loca;
#endregion
}
#endregion
#region 調(diào)整光暗
/// <summary>
/// 調(diào)整光暗
/// </summary>
/// <param name="mybm">原始圖片</param>
/// <param name="width">原始圖片的長(zhǎng)度</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);//初始化一個(gè)記錄經(jīng)過(guò)處理后的圖片對(duì)象
int x, y, resultR, resultG, resultB;//x、y是循環(huán)次數(shù),后面三個(gè)是記錄紅綠藍(lán)三個(gè)值的
Color pixel;
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//獲取當(dāng)前像素的值
resultR = pixel.R + val;//檢查紅色值會(huì)不會(huì)超出[0, 255]
resultG = pixel.G + val;//檢查綠色值會(huì)不會(huì)超出[0, 255]
resultB = pixel.B + val;//檢查藍(lán)色值會(huì)不會(huì)超出[0, 255]
bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//繪圖
}
}
return bm;
}
#endregion
#region 反色處理
/// <summary>
/// 反色處理
/// </summary>
/// <param name="mybm">原始圖片</param>
/// <param name="width">原始圖片的長(zhǎng)度</param>
/// <param name="height">原始圖片的高度</param>
public Bitmap RePic(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);//初始化一個(gè)記錄處理后的圖片的對(duì)象
int x, y, resultR, resultG, resultB;
Color pixel;
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//獲取當(dāng)前坐標(biāo)的像素值
resultR = 255 - pixel.R;//反紅
resultG = 255 - pixel.G;//反綠
resultB = 255 - pixel.B;//反藍(lán)
bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//繪圖
}
}
return bm;
}
#endregion
#region 浮雕處理
/// <summary>
/// 浮雕處理
/// </summary>
/// <param name="oldBitmap">原始圖片</param>
/// <param name="Width">原始圖片的長(zhǎng)度</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 = 0; x < Width - 1; x++)
{
for (int y = 0; y < Height - 1; y++)
{
int r = 0, g = 0, b = 0;
color1 = oldBitmap.GetPixel(x, y);
color2 = oldBitmap.GetPixel(x + 1, y + 1);
r = Math.Abs(color1.R - color2.R + 128);
g = Math.Abs(color1.G - color2.G + 128);
b = Math.Abs(color1.B - color2.B + 128);
if (r > 255) r = 255;
if (r < 0) r = 0;
if (g > 255) g = 255;
if (g < 0) g = 0;
if (b > 255) b = 255;
if (b < 0) b = 0;
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(0, 0, newW, newH), new Rectangle(0, 0, bap.Width, bap.Height), GraphicsUnit.Pixel);
g.Dispose();
return bap;
}
catch
{
return null;
}
}
#endregion
#region 濾色處理
/// <summary>
/// 濾色處理
/// </summary>
/// <param name="mybm">原始圖片</param>
/// <param name="width">原始圖片的長(zhǎng)度</param>
/// <param name="height">原始圖片的高度</param>
public Bitmap FilPic(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);//初始化一個(gè)記錄濾色效果的圖片對(duì)象
int x, y;
Color pixel;
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//獲取當(dāng)前坐標(biāo)的像素值
bm.SetPixel(x, y, Color.FromArgb(0, pixel.G, pixel.B));//繪圖
}
}
return bm;
}
#endregion
#region 左右翻轉(zhuǎn)
/// <summary>
/// 左右翻轉(zhuǎn)
/// </summary>
/// <param name="mybm">原始圖片</param>
/// <param name="width">原始圖片的長(zhǎng)度</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是循環(huán)次數(shù),z是用來(lái)記錄像素點(diǎn)的x坐標(biāo)的變化的
Color pixel;
for (y = height - 1; y >= 0; y--)
{
for (x = width - 1, z = 0; x >= 0; x--)
{
pixel = mybm.GetPixel(x, y);//獲取當(dāng)前像素的值
bm.SetPixel(z++, y, Color.FromArgb(pixel.R, pixel.G, pixel.B));//繪圖
}
}
return bm;
}
#endregion
#region 上下翻轉(zhuǎn)
/// <summary>
/// 上下翻轉(zhuǎn)
/// </summary>
/// <param name="mybm">原始圖片</param>
/// <param name="width">原始圖片的長(zhǎng)度</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 = 0; x < width; x++)
{
for (y = height - 1, z = 0; y >= 0; y--)
{
pixel = mybm.GetPixel(x, y);//獲取當(dāng)前像素的值
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(100, 125);
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(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x]; //設(shè)置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 轉(zhuǎn)換為黑白圖片
/// <summary>
/// 轉(zhuǎn)換為黑白圖片
/// </summary>
/// <param name="mybt">要進(jìn)行處理的圖片</param>
/// <param name="width">圖片的長(zhǎng)度</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是循環(huán)次數(shù)捍掺,result是記錄處理后的像素值
Color pixel;
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//獲取當(dāng)前坐標(biāo)的像素值
result = (pixel.R + pixel.G + pixel.B) / 3;//取紅綠藍(lán)三色的平均值
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[0]);
int count = gif.GetFrameCount(fd); //獲取幀數(shù)(gif圖片可能包含多幀,其它格式圖片一般僅一幀)
for (int i = 0; i < count; i++) //以Jpeg格式保存各幀
{
gif.SelectActiveFrame(fd, i);
gif.Save(pSavedPath + "\\frame_" + i + ".jpg", ImageFormat.Jpeg);
}
}
#endregion
}
}
C#圖片處理類
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén)艳吠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)麦备,“玉大人,你說(shuō)我怎么就攤上這事昭娩∧嗬迹” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵题禀,是天一觀的道長(zhǎng)鞋诗。 經(jīng)常有香客問(wèn)我,道長(zhǎng)迈嘹,這世上最難降的妖魔是什么削彬? 我笑而不...
- 正文 為了忘掉前任全庸,我火速辦了婚禮,結(jié)果婚禮上融痛,老公的妹妹穿的比我還像新娘壶笼。我一直安慰自己,他們只是感情好雁刷,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布覆劈。 她就那樣靜靜地躺著,像睡著了一般沛励。 火紅的嫁衣襯著肌膚如雪责语。 梳的紋絲不亂的頭發(fā)上,一...
- 那天目派,我揣著相機(jī)與錄音坤候,去河邊找鬼。 笑死企蹭,一個(gè)胖子當(dāng)著我的面吹牛白筹,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播谅摄,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼徒河,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了送漠?” 一聲冷哼從身側(cè)響起顽照,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎螺男,沒(méi)想到半個(gè)月后棒厘,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體纵穿,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡下隧,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了谓媒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片淆院。...
- 正文 年R本政府宣布拷淘,位于F島的核電站,受9級(jí)特大地震影響指孤,放射性物質(zhì)發(fā)生泄漏启涯。R本人自食惡果不足惜贬堵,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望结洼。 院中可真熱鬧黎做,春花似錦、人聲如沸松忍。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)鸣峭。三九已至宏所,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間叽掘,已是汗流浹背楣铁。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像浓镜,于是被迫代替她去往敵國(guó)和親溃列。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 自從上了大學(xué)以后膛薛,剛開(kāi)始是對(duì)大學(xué)生活的憧憬和向往听隐,擺脫學(xué)習(xí)壓力的輕松。慢慢地卻變成了一天一天荒廢時(shí)光哄啄,漸漸的失去...
- 我這個(gè)人啊雅任,表達(dá)欲特旺盛。所以只要碰見(jiàn)我想了解的人咨跌,就喜歡掏心窩子沪么。 因?yàn)槲乙恢闭J(rèn)為,當(dāng)每個(gè)人進(jìn)入了人生稍微成熟的...
- 今天早上兒子說(shuō)媽媽老師讓我們每位同學(xué)帶上自己喜歡吃的水果,兒子本想帶溜連刊殉,可是溜連拿到班級(jí)以后會(huì)影響到同學(xué)殉摔,所以兒...