C#图片处理类分享
本文实例为大家分享了C#图片处理类的具体代码,供大家参考,具体内容如下
usingSystem;
usingSystem.Collections;
usingSystem.IO;
usingSystem.Drawing;
usingSystem.Drawing.Imaging;
usingSystem.Drawing.Drawing2D;
namespaceDotNet.Utilities
{
publicclassImageClass
{
publicImageClass()
{}
#region缩略图
///
///生成缩略图
///
///源图路径(物理路径)
///缩略图路径(物理路径)
///缩略图宽度
///缩略图高度
///生成缩略图的方式
publicstaticvoidMakeThumbnail(stringoriginalImagePath,stringthumbnailPath,intwidth,intheight,stringmode)
{
System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(originalImagePath);
inttowidth=width;
inttoheight=height;
intx=0;
inty=0;
intow=originalImage.Width;
intoh=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;
}
//新建一个bmp图片
System.Drawing.Imagebitmap=newSystem.Drawing.Bitmap(towidth,toheight);
//新建一个画板
System.Drawing.Graphicsg=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,newSystem.Drawing.Rectangle(0,0,towidth,toheight),newSystem.Drawing.Rectangle(x,y,ow,oh),System.Drawing.GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exceptione)
{
throwe;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
#endregion
#region图片水印
///
///图片水印处理方法
///
///需要加载水印的图片路径(绝对路径)
///水印图片(绝对路径)
///水印位置(传送正确的代码)
publicstaticstringImageWatermark(stringpath,stringwaterpath,stringlocation)
{
stringkz_name=Path.GetExtension(path);
if(kz_name==".jpg"||kz_name==".bmp"||kz_name==".jpeg")
{
DateTimetime=DateTime.Now;
stringfilename=""+time.Year.ToString()+time.Month.ToString()+time.Day.ToString()+time.Hour.ToString()+time.Minute.ToString()+time.Second.ToString()+time.Millisecond.ToString();
Imageimg=Bitmap.FromFile(path);
Imagewaterimg=Image.FromFile(waterpath);
Graphicsg=Graphics.FromImage(img);
ArrayListloca=GetLocation(location,img,waterimg);
g.DrawImage(waterimg,newRectangle(int.Parse(loca[0].ToString()),int.Parse(loca[1].ToString()),waterimg.Width,waterimg.Height));
waterimg.Dispose();
g.Dispose();
stringnewpath=Path.GetDirectoryName(path)+filename+kz_name;
img.Save(newpath);
img.Dispose();
File.Copy(newpath,path,true);
if(File.Exists(newpath))
{
File.Delete(newpath);
}
}
returnpath;
}
///
///图片水印位置处理方法
///
///水印位置
///需要添加水印的图片
///水印图片
privatestaticArrayListGetLocation(stringlocation,Imageimg,Imagewaterimg)
{
ArrayListloca=newArrayList();
intx=0;
inty=0;
if(location=="LT")
{
x=10;
y=10;
}
elseif(location=="T")
{
x=img.Width/2-waterimg.Width/2;
y=img.Height-waterimg.Height;
}
elseif(location=="RT")
{
x=img.Width-waterimg.Width;
y=10;
}
elseif(location=="LC")
{
x=10;
y=img.Height/2-waterimg.Height/2;
}
elseif(location=="C")
{
x=img.Width/2-waterimg.Width/2;
y=img.Height/2-waterimg.Height/2;
}
elseif(location=="RC")
{
x=img.Width-waterimg.Width;
y=img.Height/2-waterimg.Height/2;
}
elseif(location=="LB")
{
x=10;
y=img.Height-waterimg.Height;
}
elseif(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);
returnloca;
}
#endregion
#region文字水印
///
///文字水印处理方法
///
///图片路径(绝对路径)
///字体大小
///水印文字
///颜色
///水印位置
publicstaticstringLetterWatermark(stringpath,intsize,stringletter,Colorcolor,stringlocation)
{
#region
stringkz_name=Path.GetExtension(path);
if(kz_name==".jpg"||kz_name==".bmp"||kz_name==".jpeg")
{
DateTimetime=DateTime.Now;
stringfilename=""+time.Year.ToString()+time.Month.ToString()+time.Day.ToString()+time.Hour.ToString()+time.Minute.ToString()+time.Second.ToString()+time.Millisecond.ToString();
Imageimg=Bitmap.FromFile(path);
Graphicsgs=Graphics.FromImage(img);
ArrayListloca=GetLocation(location,img,size,letter.Length);
Fontfont=newFont("宋体",size);
Brushbr=newSolidBrush(color);
gs.DrawString(letter,font,br,float.Parse(loca[0].ToString()),float.Parse(loca[1].ToString()));
gs.Dispose();
stringnewpath=Path.GetDirectoryName(path)+filename+kz_name;
img.Save(newpath);
img.Dispose();
File.Copy(newpath,path,true);
if(File.Exists(newpath))
{
File.Delete(newpath);
}
}
returnpath;
#endregion
}
///
///文字水印位置的方法
///
///位置代码
///图片对象
///宽(当水印类型为文字时,传过来的就是字体的大小)
///高(当水印类型为文字时,传过来的就是字符的长度)
privatestaticArrayListGetLocation(stringlocation,Imageimg,intwidth,intheight)
{
#region
ArrayListloca=newArrayList();//定义数组存储位置
floatx=10;
floaty=10;
if(location=="LT")
{
loca.Add(x);
loca.Add(y);
}
elseif(location=="T")
{
x=img.Width/2-(width*height)/2;
loca.Add(x);
loca.Add(y);
}
elseif(location=="RT")
{
x=img.Width-width*height;
}
elseif(location=="LC")
{
y=img.Height/2;
}
elseif(location=="C")
{
x=img.Width/2-(width*height)/2;
y=img.Height/2;
}
elseif(location=="RC")
{
x=img.Width-height;
y=img.Height/2;
}
elseif(location=="LB")
{
y=img.Height-width-5;
}
elseif(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);
returnloca;
#endregion
}
#endregion
#region调整光暗
///
///调整光暗
///
///原始图片
///原始图片的长度
///原始图片的高度
///增加或减少的光暗值
publicBitmapLDPic(Bitmapmybm,intwidth,intheight,intval)
{
Bitmapbm=newBitmap(width,height);//初始化一个记录经过处理后的图片对象
intx,y,resultR,resultG,resultB;//x、y是循环次数,后面三个是记录红绿蓝三个值的
Colorpixel;
for(x=0;x
///反色处理
///
///原始图片
///原始图片的长度
///原始图片的高度
publicBitmapRePic(Bitmapmybm,intwidth,intheight)
{
Bitmapbm=newBitmap(width,height);//初始化一个记录处理后的图片的对象
intx,y,resultR,resultG,resultB;
Colorpixel;
for(x=0;x
///浮雕处理
///
///原始图片
///原始图片的长度
///原始图片的高度
publicBitmapFD(BitmapoldBitmap,intWidth,intHeight)
{
BitmapnewBitmap=newBitmap(Width,Height);
Colorcolor1,color2;
for(intx=0;x255)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));
}
}
returnnewBitmap;
}
#endregion
#region拉伸图片
///
///拉伸图片
///
///原始图片
///新的宽度
///新的高度
publicstaticBitmapResizeImage(Bitmapbmp,intnewW,intnewH)
{
try
{
Bitmapbap=newBitmap(newW,newH);
Graphicsg=Graphics.FromImage(bap);
g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(bap,newRectangle(0,0,newW,newH),newRectangle(0,0,bap.Width,bap.Height),GraphicsUnit.Pixel);
g.Dispose();
returnbap;
}
catch
{
returnnull;
}
}
#endregion
#region滤色处理
///
///滤色处理
///
///原始图片
///原始图片的长度
///原始图片的高度
publicBitmapFilPic(Bitmapmybm,intwidth,intheight)
{
Bitmapbm=newBitmap(width,height);//初始化一个记录滤色效果的图片对象
intx,y;
Colorpixel;
for(x=0;x
///左右翻转
///
///原始图片
///原始图片的长度
///原始图片的高度
publicBitmapRevPicLR(Bitmapmybm,intwidth,intheight)
{
Bitmapbm=newBitmap(width,height);
intx,y,z;//x,y是循环次数,z是用来记录像素点的x坐标的变化的
Colorpixel;
for(y=height-1;y>=0;y--)
{
for(x=width-1,z=0;x>=0;x--)
{
pixel=mybm.GetPixel(x,y);//获取当前像素的值
bm.SetPixel(z++,y,Color.FromArgb(pixel.R,pixel.G,pixel.B));//绘图
}
}
returnbm;
}
#endregion
#region上下翻转
///
///上下翻转
///
///原始图片
///原始图片的长度
///原始图片的高度
publicBitmapRevPicUD(Bitmapmybm,intwidth,intheight)
{
Bitmapbm=newBitmap(width,height);
intx,y,z;
Colorpixel;
for(x=0;x=0;y--)
{
pixel=mybm.GetPixel(x,y);//获取当前像素的值
bm.SetPixel(x,z++,Color.FromArgb(pixel.R,pixel.G,pixel.B));//绘图
}
}
returnbm;
}
#endregion
#region压缩图片
///
///压缩到指定尺寸
///
///原文件
///新文件
publicboolCompress(stringoldfile,stringnewfile)
{
try
{
System.Drawing.Imageimg=System.Drawing.Image.FromFile(oldfile);
System.Drawing.Imaging.ImageFormatthisFormat=img.RawFormat;
SizenewSize=newSize(100,125);
BitmapoutBmp=newBitmap(newSize.Width,newSize.Height);
Graphicsg=Graphics.FromImage(outBmp);
g.CompositingQuality=CompositingQuality.HighQuality;
g.SmoothingMode=SmoothingMode.HighQuality;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.DrawImage(img,newRectangle(0,0,newSize.Width,newSize.Height),0,0,img.Width,img.Height,GraphicsUnit.Pixel);
g.Dispose();
EncoderParametersencoderParams=newEncoderParameters();
long[]quality=newlong[1];
quality[0]=100;
EncoderParameterencoderParam=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,quality);
encoderParams.Param[0]=encoderParam;
ImageCodecInfo[]arrayICI=ImageCodecInfo.GetImageEncoders();
ImageCodecInfojpegICI=null;
for(intx=0;x
///转换为黑白图片
///
///要进行处理的图片
///图片的长度
///图片的高度
publicBitmapBWPic(Bitmapmybm,intwidth,intheight)
{
Bitmapbm=newBitmap(width,height);
intx,y,result;//x,y是循环次数,result是记录处理后的像素值
Colorpixel;
for(x=0;x
///获取图片中的各帧
///
///图片路径
///保存路径
publicvoidGetFrames(stringpPath,stringpSavedPath)
{
Imagegif=Image.FromFile(pPath);
FrameDimensionfd=newFrameDimension(gif.FrameDimensionsList[0]);
intcount=gif.GetFrameCount(fd);//获取帧数(gif图片可能包含多帧,其它格式图片一般仅一帧)
for(inti=0;i
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。