本文实例为大家分享了C#图片处理类的具体代码,供大家参考,具体内容如下
usingSystem; usingSystem.Collections; usingSystem.IO; usingSystem.Drawing; usingSystem.Drawing.Imaging; usingSystem.Drawing.Drawing2D; namespaceDotNet.Utilities { publicclassImageClass { publicImageClass() {} #region缩略图 ///<summary> ///生成缩略图 ///</summary> ///<paramname="originalImagePath">源图路径(物理路径)</param> ///<paramname="thumbnailPath">缩略图路径(物理路径)</param> ///<paramname="width">缩略图宽度</param> ///<paramname="height">缩略图高度</param> ///<paramname="mode">生成缩略图的方式</param> 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图片水印 ///<summary> ///图片水印处理方法 ///</summary> ///<paramname="path">需要加载水印的图片路径(绝对路径)</param> ///<paramname="waterpath">水印图片(绝对路径)</param> ///<paramname="location">水印位置(传送正确的代码)</param> 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; } ///<summary> ///图片水印位置处理方法 ///</summary> ///<paramname="location">水印位置</param> ///<paramname="img">需要添加水印的图片</param> ///<paramname="waterimg">水印图片</param> 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文字水印 ///<summary> ///文字水印处理方法 ///</summary> ///<paramname="path">图片路径(绝对路径)</param> ///<paramname="size">字体大小</param> ///<paramname="letter">水印文字</param> ///<paramname="color">颜色</param> ///<paramname="location">水印位置</param> 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 } ///<summary> ///文字水印位置的方法 ///</summary> ///<paramname="location">位置代码</param> ///<paramname="img">图片对象</param> ///<paramname="width">宽(当水印类型为文字时,传过来的就是字体的大小)</param> ///<paramname="height">高(当水印类型为文字时,传过来的就是字符的长度)</param> 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调整光暗 ///<summary> ///调整光暗 ///</summary> ///<paramname="mybm">原始图片</param> ///<paramname="width">原始图片的长度</param> ///<paramname="height">原始图片的高度</param> ///<paramname="val">增加或减少的光暗值</param> publicBitmapLDPic(Bitmapmybm,intwidth,intheight,intval) { Bitmapbm=newBitmap(width,height);//初始化一个记录经过处理后的图片对象 intx,y,resultR,resultG,resultB;//x、y是循环次数,后面三个是记录红绿蓝三个值的 Colorpixel; for(x=0;x<width;x++) { for(y=0;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));//绘图 } } returnbm; } #endregion #region反色处理 ///<summary> ///反色处理 ///</summary> ///<paramname="mybm">原始图片</param> ///<paramname="width">原始图片的长度</param> ///<paramname="height">原始图片的高度</param> publicBitmapRePic(Bitmapmybm,intwidth,intheight) { Bitmapbm=newBitmap(width,height);//初始化一个记录处理后的图片的对象 intx,y,resultR,resultG,resultB; Colorpixel; for(x=0;x<width;x++) { for(y=0;y<height;y++) { pixel=mybm.GetPixel(x,y);//获取当前坐标的像素值 resultR=255-pixel.R;//反红 resultG=255-pixel.G;//反绿 resultB=255-pixel.B;//反蓝 bm.SetPixel(x,y,Color.FromArgb(resultR,resultG,resultB));//绘图 } } returnbm; } #endregion #region浮雕处理 ///<summary> ///浮雕处理 ///</summary> ///<paramname="oldBitmap">原始图片</param> ///<paramname="Width">原始图片的长度</param> ///<paramname="Height">原始图片的高度</param> publicBitmapFD(BitmapoldBitmap,intWidth,intHeight) { BitmapnewBitmap=newBitmap(Width,Height); Colorcolor1,color2; for(intx=0;x<Width-1;x++) { for(inty=0;y<Height-1;y++) { intr=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)); } } returnnewBitmap; } #endregion #region拉伸图片 ///<summary> ///拉伸图片 ///</summary> ///<paramname="bmp">原始图片</param> ///<paramname="newW">新的宽度</param> ///<paramname="newH">新的高度</param> 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滤色处理 ///<summary> ///滤色处理 ///</summary> ///<paramname="mybm">原始图片</param> ///<paramname="width">原始图片的长度</param> ///<paramname="height">原始图片的高度</param> publicBitmapFilPic(Bitmapmybm,intwidth,intheight) { Bitmapbm=newBitmap(width,height);//初始化一个记录滤色效果的图片对象 intx,y; Colorpixel; for(x=0;x<width;x++) { for(y=0;y<height;y++) { pixel=mybm.GetPixel(x,y);//获取当前坐标的像素值 bm.SetPixel(x,y,Color.FromArgb(0,pixel.G,pixel.B));//绘图 } } returnbm; } #endregion #region左右翻转 ///<summary> ///左右翻转 ///</summary> ///<paramname="mybm">原始图片</param> ///<paramname="width">原始图片的长度</param> ///<paramname="height">原始图片的高度</param> 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上下翻转 ///<summary> ///上下翻转 ///</summary> ///<paramname="mybm">原始图片</param> ///<paramname="width">原始图片的长度</param> ///<paramname="height">原始图片的高度</param> publicBitmapRevPicUD(Bitmapmybm,intwidth,intheight) { Bitmapbm=newBitmap(width,height); intx,y,z; Colorpixel; for(x=0;x<width;x++) { for(y=height-1,z=0;y>=0;y--) { pixel=mybm.GetPixel(x,y);//获取当前像素的值 bm.SetPixel(x,z++,Color.FromArgb(pixel.R,pixel.G,pixel.B));//绘图 } } returnbm; } #endregion #region压缩图片 ///<summary> ///压缩到指定尺寸 ///</summary> ///<paramname="oldfile">原文件</param> ///<paramname="newfile">新文件</param> 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<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(); returntrue; } catch { returnfalse; } } #endregion #region图片灰度化 publicColorGray(Colorc) { intrgb=Convert.ToInt32((double)(((0.3*c.R)+(0.59*c.G))+(0.11*c.B))); returnColor.FromArgb(rgb,rgb,rgb); } #endregion #region转换为黑白图片 ///<summary> ///转换为黑白图片 ///</summary> ///<paramname="mybt">要进行处理的图片</param> ///<paramname="width">图片的长度</param> ///<paramname="height">图片的高度</param> publicBitmapBWPic(Bitmapmybm,intwidth,intheight) { Bitmapbm=newBitmap(width,height); intx,y,result;//x,y是循环次数,result是记录处理后的像素值 Colorpixel; for(x=0;x<width;x++) { for(y=0;y<height;y++) { pixel=mybm.GetPixel(x,y);//获取当前坐标的像素值 result=(pixel.R+pixel.G+pixel.B)/3;//取红绿蓝三色的平均值 bm.SetPixel(x,y,Color.FromArgb(result,result,result)); } } returnbm; } #endregion #region获取图片中的各帧 ///<summary> ///获取图片中的各帧 ///</summary> ///<paramname="pPath">图片路径</param> ///<paramname="pSavePath">保存路径</param> publicvoidGetFrames(stringpPath,stringpSavedPath) { Imagegif=Image.FromFile(pPath); FrameDimensionfd=newFrameDimension(gif.FrameDimensionsList[0]); intcount=gif.GetFrameCount(fd);//获取帧数(gif图片可能包含多帧,其它格式图片一般仅一帧) for(inti=0;i<count;i++)//以Jpeg格式保存各帧 { gif.SelectActiveFrame(fd,i); gif.Save(pSavedPath+"\frame_"+i+".jpg",ImageFormat.Jpeg); } } #endregion } }
您可能感兴趣的文章:C#实现的图片、string相互转换类分享c#生成图片缩略图的类(2种实现思路)C#添加图片水印类实现代码
C# FTP操作类分享
C# 的析构以及垃圾回收实例分析
上述就是C#学习教程:C#图片处理类分享分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/907901.html