- usingSystem;
- usingSystem.Drawing;
- usingSystem.Drawing.Imaging;
- usingSystem.IO;
- usingSystem.Web;
- namespaceXXXX.Common
- {
- ///<summary>
- ///图片操作:生成缩略图、添加水印、截取图片等
- ///</summary>
- publicclassImagesHelper
- {
- ///<summary>
- ///根据文件流获得图片宽度
- ///</summary>
- ///<paramname="file"></param>
- ///<returns></returns>
- publicstaticintgetImgWidth(Streamstream)
- {
- Imageimg=Image.FromStream(stream);
- intresult=img.Width;
- img.Dispose();
- stream.Dispose();
- returnresult;
- }
- ///<summary>
- ///根据图片路径获得图片宽度
- ///</summary>
- ///<paramname="filePath"></param>
- ///<returns></returns>
- publicstaticintgetImgWidth(stringfilePath)
- {
- Imageimg=Image.FromFile(filePath);
- intresult=img.Width;
- img.Dispose();
- returnresult;
- }
- #region从文件流生成缩略图
- ///<summary>
- ///从文件流生成缩略图
- ///</summary>
- ///<paramname="stream">数据IO流</param>
- ///<paramname="savePath"></param>
- ///<paramname="width"></param>
- ///<paramname="height"></param>
- ///<paramname="scale"></param>
- ///<returns></returns>
- publicstaticboolGetThumbNail(Streamstream,stringsavePath,intwidth,intheight,ThumbNailScalescale)
- {
- //缩略图
- Imageimg=Image.FromStream(stream);
- stream.Dispose();
- inttowidth=width;
- inttoheight=height;
- intx=0;
- inty=0;
- intow=img.Width;
- intoh=img.Height;
- //如果图片小于指定宽度
- if(ow<width)
- width=ow;
- if(oh<height)
- height=oh;
- switch(scale)
- {
- caseThumbNailScale.Appointed:
- break;
- caseThumbNailScale.ScaleWidth:
- toheight=img.Height*width/img.Width;
- break;
- caseThumbNailScale.ScaleHeight:
- towidth=img.Width*height/img.Height;
- break;
- caseThumbNailScale.Cut:
- if((double)img.Width/(double)img.Height>(double)towidth/(double)toheight)
- {
- oh=img.Height;
- ow=img.Height*towidth/toheight;
- y=0;
- x=(img.Width-ow)/2;
- }
- else
- {
- ow=img.Width;
- oh=img.Width*height/towidth;
- x=0;
- y=(img.Height-oh)/2;
- }
- break;
- caseThumbNailScale.ScaleDown:
- doubleTw,Th;
- Tw=width;
- Th=height*(Convert.ToDouble(oh)/Convert.ToDouble(ow));
- if(Th>height)
- {
- Th=height;
- Tw=width*(Convert.ToDouble(ow)/Convert.ToDouble(oh));
- }
- towidth=Convert.ToInt32(Tw);
- toheight=Convert.ToInt32(Th);
- break;
- default:
- break;
- }
- //新建一个bmp图片
- Imagebitmap=newBitmap(towidth,toheight);
- //新建一个画板
- Graphicsg=Graphics.FromImage(bitmap);
- //设置高质量插值法
- g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;
- //设置高质量,低速度呈现平滑程度
- g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- //清空画布并以透明背景色填充
- g.Clear(Color.Transparent);
- //在指定位置并且按指定大小绘制原图片的指定部分
- g.DrawImage(img,newRectangle(0,0,towidth,toheight),
- newRectangle(x,y,ow,oh),
- GraphicsUnit.Pixel);
- try
- {
- //以jpg格式保存缩略图
- bitmap.Save(savePath,ImageFormat.Jpeg);
- }
- catch(Exceptionex)
- {
- Logger.Write(string.Format("从文件流生成缩略图:{0}",ex.Message),Logger.MsgType.Error);
- returnfalse;
- }
- finally
- {
- img.Dispose();
- bitmap.Dispose();
- g.Dispose();
- }
- returntrue;
- }
- #endregion
- #region从文件路径生成缩略图
- ///<summary>
- ///从图片路径生成缩略图
- ///</summary>
- ///<paramname="originalImagePath">图片路径</param>
- ///<paramname="savePath">保存路径</param>
- ///<paramname="width">缩略图宽度</param>
- ///<paramname="height">缩略图高度</param>
- ///<paramname="mode">HW:指定高宽缩放(可能变形)W://指定宽,高按比例H://指定高,宽按比例Cut://指定高宽裁减(不变形)</param>
- ///<returns></returns>
- publicstaticboolGetThumbNail(stringoriginalImagePath,stringsavePath,intwidth,intheight,ThumbNailScalescale)
- {
- //缩略图
- Imageimg=Image.FromFile(originalImagePath);
- inttowidth=width;
- inttoheight=height;
- intx=0;
- inty=0;
- intow=img.Width;
- intoh=img.Height;
- //如果图片小于指定宽度
- if(ow<width)
- width=ow;
- switch(scale)
- {
- caseThumbNailScale.Appointed:
- break;
- caseThumbNailScale.ScaleWidth:
- toheight=img.Height*width/img.Width;
- break;
- caseThumbNailScale.ScaleHeight:
- towidth=img.Width*height/img.Height;
- break;
- caseThumbNailScale.Cut:
- if((double)img.Width/(double)img.Height>(double)towidth/(double)toheight)
- {
- oh=img.Height;
- ow=img.Height*towidth/toheight;
- y=0;
- x=(img.Width-ow)/2;
- }
- else
- {
- ow=img.Width;
- oh=img.Width*height/towidth;
- x=0;
- y=(img.Height-oh)/2;
- }
- break;
- caseThumbNailScale.ScaleDown:
- doubleTw,Th;
- Tw=width;
- Th=height*(Convert.ToDouble(oh)/Convert.ToDouble(ow));
- if(Th>height)
- {
- Th=height;
- Tw=width*(Convert.ToDouble(ow)/Convert.ToDouble(oh));
- }
- towidth=Convert.ToInt32(Tw);
- toheight=Convert.ToInt32(Th);
- break;
- default:
- break;
- }
- //新建一个bmp图片
- Imagebitmap=newBitmap(towidth,toheight);
- //新建一个画板
- Graphicsg=Graphics.FromImage(bitmap);
- //设置高质量插值法
- g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;
- //设置高质量,低速度呈现平滑程度
- g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- //清空画布并以透明背景色填充
- g.Clear(Color.White);
- //在指定位置并且按指定大小绘制原图片的指定部分
- g.DrawImage(img,newRectangle(0,0,towidth,toheight),
- newRectangle(x,y,ow,oh),
- GraphicsUnit.Pixel);
- try
- {
- //以jpg格式保存缩略图
- bitmap.Save(savePath,ImageFormat.Jpeg);
- }
- catch(Exceptione)
- {
- throwe;
- }
- finally
- {
- img.Dispose();
- bitmap.Dispose();
- g.Dispose();
- }
- returntrue;
- }
- #endregion
- #region获取图片格式
- ///<summary>
- ///获取图片格式
- ///</summary>
- ///<paramname="strContentType"></param>
- ///<returns>返回图片格式</returns>
- publicstaticImageFormatGetImageType(objectstrContentType)
- {
- if((strContentType.ToString().ToLower())=="image/pjpeg")
- {
- returnImageFormat.Jpeg;
- }
- elseif((strContentType.ToString().ToLower())=="image/gif")
- {
- returnImageFormat.Gif;
- }
- elseif((strContentType.ToString().ToLower())=="image/bmp")
- {
- returnImageFormat.Bmp;
- }
- elseif((strContentType.ToString().ToLower())=="image/tiff")
- {
- returnImageFormat.Tiff;
- }
- elseif((strContentType.ToString().ToLower())=="image/x-icon")
- {
- returnImageFormat.Icon;
- }
- elseif((strContentType.ToString().ToLower())=="image/x-png")
- {
- returnImageFormat.Png;
- }
- elseif((strContentType.ToString().ToLower())=="image/x-emf")
- {
- returnImageFormat.Emf;
- }
- elseif((strContentType.ToString().ToLower())=="image/x-exif")
- {
- returnImageFormat.Exif;
- }
- elseif((strContentType.ToString().ToLower())=="image/x-wmf")
- {
- returnImageFormat.Wmf;
- }
- else
- {
- returnImageFormat.MemoryBmp;
- }
- }
- #endregion
- ///<summary>
- ///生成水印图片
- ///</summary>
- ///<paramname="sourceFile"></param>
- ///<paramname="saveFile">保存文件路径</param>
- ///<returns></returns>
- publicstaticboolMakeWaterImage(StreamsourceFile,stringsaveFile)
- {
- boolresult=false;
- //水印图片
- try
- {
- ImageimgPhoto=Image.FromStream(sourceFile);
- sourceFile.Close();
- sourceFile.Dispose();
- intphWidth=imgPhoto.Width;
- intphHeight=imgPhoto.Height;
- BitmapbmPhoto=newBitmap(phWidth,phHeight,PixelFormat.Format24bppRgb);
- bmPhoto.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution);
- ImageimgWatermark=newBitmap(System.Web.HttpContext.Current.Server.MapPath("/images/watermark.png"));
- intwmWidth=imgWatermark.Width;
- intwmHeight=imgWatermark.Height;
- if(phWidth>(wmWidth+100)&&phHeight>(wmHeight+100))
- {
- GraphicsgrPhoto=Graphics.FromImage(bmPhoto);
- grPhoto.Clear(Color.White);
- grPhoto.DrawImage(imgPhoto,newRectangle(0,0,phWidth,phHeight),0,0,phWidth,phHeight,GraphicsUnit.Pixel);
- grPhoto.Dispose();
- //添加水印图片
- using(BitmapbmWatermark=newBitmap(bmPhoto))
- {
- bmWatermark.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution);
- GraphicsgrWatermark=Graphics.FromImage(bmWatermark);
- using(ImageAttributesimageAttributes=newImageAttributes())
- {
- //ColorMapcolorMap=newColorMap();
- //colorMap.OldColor=Color.FromArgb(255,255,255,255);
- //colorMap.NewColor=Color.FromArgb(0,0,0,0);
- //ColorMap[]remapTable={colorMap};
- //imageAttributes.SetRemapTable(remapTable,ColorAdjustType.Bitmap);
- float[][]colorMatrixElements={newfloat[]{1.0f,0.0f,0.0f,0.0f,0.0f},newfloat[]{0.0f,1.0f,0.0f,0.0f,0.0f},newfloat[]{0.0f,0.0f,1.0f,0.0f,0.0f},newfloat[]{0.0f,0.0f,0.0f,1.0f,0.0f},newfloat[]{0.0f,0.0f,0.0f,0.0f,1.0f}};
- ColorMatrixwmColorMatrix=newColorMatrix(colorMatrixElements);
- imageAttributes.SetColorMatrix(wmColorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
- intxPosOfWm=((phWidth-wmWidth)-2);
- intyPosOfWm=((phHeight-wmHeight)-2);
- grWatermark.DrawImage(imgWatermark,newRectangle(xPosOfWm,yPosOfWm,wmWidth,wmHeight),0,0,wmWidth,wmHeight,GraphicsUnit.Pixel,imageAttributes);
- }
- imgPhoto=bmWatermark;
- grWatermark.Dispose();
- imgPhoto.Save(saveFile,ImageFormat.Jpeg);
- }
- result=true;
- }
- else
- {
- result=false;
- }
- imgWatermark.Dispose();
- bmPhoto.Dispose();
- imgPhoto.Dispose();
- }
- catch(Exceptionex)
- {
- Logger.Write(string.Format("生成水印图片错误:{0}",ex.Message),Logger.MsgType.Information);
- try
- {
- ImageimgPhoto2=Image.FromStream(sourceFile);
- imgPhoto2.Save(saveFile,ImageFormat.Jpeg);
- imgPhoto2.Dispose();
- result=true;
- }
- catch
- {
- result=false;
- }
- }
- returnresult;
- }
- ///<summary>
- ///生成水印图片
- ///</summary>
- ///<paramname="sourceFile"></param>
- ///<paramname="saveFile">保存文件路径</param>
- ///<paramname="Location">位置0-右下角1-居中2-右上角3-左下角</param>
- ///<returns></returns>
- publicstaticboolMakeWaterImage(StreamsourceFile,stringsaveFile,ImagePositionPosition)
- {
- boolresult=false;
- //水印图片
- try
- {
- ImageimgPhoto=Image.FromStream(sourceFile);
- intphWidth=imgPhoto.Width;
- intphHeight=imgPhoto.Height;
- BitmapbmPhoto=newBitmap(phWidth,phHeight,PixelFormat.Format24bppRgb);
- bmPhoto.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution);
- ImageimgWatermark=newBitmap(System.Web.HttpContext.Current.Server.MapPath("/images/watermark.png"));
- intwmWidth=imgWatermark.Width;
- intwmHeight=imgWatermark.Height;
- if(phWidth>(wmWidth+100)&&phHeight>(wmHeight+100))
- {
- GraphicsgrPhoto=Graphics.FromImage(bmPhoto);
- grPhoto.Clear(Color.White);
- grPhoto.DrawImage(imgPhoto,newRectangle(0,0,phWidth,phHeight),0,0,phWidth,phHeight,GraphicsUnit.Pixel);
- grPhoto.Dispose();
- //添加水印图片
- using(BitmapbmWatermark=newBitmap(bmPhoto))
- {
- bmWatermark.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution);
- GraphicsgrWatermark=Graphics.FromImage(bmWatermark);
- using(ImageAttributesimageAttributes=newImageAttributes())
- {
- //ColorMapcolorMap=newColorMap();
- //colorMap.OldColor=Color.FromArgb(255,255,255,255);
- //colorMap.NewColor=Color.FromArgb(0,0,0,0);
- //ColorMap[]remapTable={colorMap};
- //imageAttributes.SetRemapTable(remapTable,ColorAdjustType.Bitmap);
- float[][]colorMatrixElements={newfloat[]{1.0f,0.0f,0.0f,0.0f,0.0f},newfloat[]{0.0f,1.0f,0.0f,0.0f,0.0f},newfloat[]{0.0f,0.0f,1.0f,0.0f,0.0f},newfloat[]{0.0f,0.0f,0.0f,1.0f,0.0f},newfloat[]{0.0f,0.0f,0.0f,0.0f,1.0f}};
- ColorMatrixwmColorMatrix=newColorMatrix(colorMatrixElements);
- imageAttributes.SetColorMatrix(wmColorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
- intxPosOfWm=0;
- intyPosOfWm=0;
- switch(Position)
- {
- caseImagePosition.BottomRight:
- xPosOfWm=((phWidth-wmWidth)-2);
- yPosOfWm=((phHeight-wmHeight)-2);
- break;
- caseImagePosition.TopLeft:
- xPosOfWm=2;
- yPosOfWm=2;
- break;
- caseImagePosition.TopRigth:
- xPosOfWm=((phWidth-wmWidth)-2);
- yPosOfWm=2;
- break;
- caseImagePosition.BottomLeft:
- xPosOfWm=2;
- yPosOfWm=((phHeight-wmHeight)-2);
- break;
- caseImagePosition.Center:
- xPosOfWm=((phWidth/2)-(wmWidth/2));
- yPosOfWm=((phHeight/2)-(wmHeight/2));
- break;
- }
- grWatermark.DrawImage(imgWatermark,newRectangle(xPosOfWm,yPosOfWm,wmWidth,wmHeight),0,0,wmWidth,wmHeight,GraphicsUnit.Pixel,imageAttributes);
- }
- imgPhoto=bmWatermark;
- grWatermark.Dispose();
- imgPhoto.Save(saveFile,ImageFormat.Jpeg);
- }
- result=true;
- }
- else
- {
- ImageimgPhoto2=Image.FromStream(sourceFile);
- imgPhoto2.Save(saveFile,ImageFormat.Jpeg);
- imgPhoto2.Dispose();
- result=true;
- }
- imgWatermark.Dispose();
- bmPhoto.Dispose();
- imgPhoto.Dispose();
- }
- catch
- {
- try
- {
- ImageimgPhoto2=Image.FromStream(sourceFile);
- imgPhoto2.Save(saveFile,ImageFormat.Jpeg);
- imgPhoto2.Dispose();
- result=true;
- }
- catch
- {
- result=false;
- }
- }
- sourceFile.Close();
- sourceFile.Dispose();
- returnresult;
- }
- #region从图片中截取一张指定大小的图片
- ///<summary>
- ///从图片中截取部分生成新图
- ///</summary>
- ///<paramname="sFromFilePath">原始图片</param>
- ///<paramname="saveFilePath">生成新图</param>
- ///<paramname="width">截取图片宽度</param>
- ///<paramname="height">截取图片高度</param>
- ///<paramname="spaceX">截图图片X坐标</param>
- ///<paramname="spaceY">截取图片Y坐标</param>
- publicstaticvoidCaptureImage(stringsFromFilePath,stringsaveFilePath,intwidth,intheight,intspaceX,intspaceY)
- {
- //载入底图
- ImagefromImage=Image.FromFile(sFromFilePath);
- intx=0;//截取X坐标
- inty=0;//截取Y坐标
- //原图宽与生成图片宽之差
- //当小于0(即原图宽小于要生成的图)时,新图宽度为较小者即原图宽度X坐标则为0
- //当大于0(即原图宽大于要生成的图)时,新图宽度为设置值即widthX坐标则为sX与spaceX之间较小者
- //Y方向同理
- intsX=fromImage.Width-width;
- intsY=fromImage.Height-height;
- if(sX>0)
- {
- x=sX>spaceX?spaceX:sX;
- }
- else
- {
- width=fromImage.Width;
- }
- if(sY>0)
- {
- y=sY>spaceY?spaceY:sY;
- }
- else
- {
- height=fromImage.Height;
- }
- //创建新图位图
- Bitmapbitmap=newBitmap(width,height);
- //创建作图区域
- Graphicsgraphic=Graphics.FromImage(bitmap);
- //截取原图相应区域写入作图区
- graphic.DrawImage(fromImage,0,0,newRectangle(x,y,width,height),GraphicsUnit.Pixel);
- //从作图区生成新图
- ImagesaveImage=Image.FromHbitmap(bitmap.GetHbitmap());
- //保存图象
- saveImage.Save(saveFilePath,ImageFormat.Jpeg);
- //释放资源
- saveImage.Dispose();
- bitmap.Dispose();
- graphic.Dispose();
- }
- #endregion
- publicenumImagePosition
- {
- ///<summary>
- ///居中
- ///</summary>
- Center,
- ///<summary>
- ///左上角
- ///</summary>
- TopLeft,
- ///<summary>
- ///左下角
- ///</summary>
- BottomLeft,
- ///<summary>
- ///右下角
- ///</summary>
- BottomRight,
- ///<summary>
- ///右上角
- ///</summary>
- TopRigth
- }
- ///<summary>
- ///图片
- ///</summary>
- publicenumThumbNailScale
- {
- ///<summary>
- ///指定高宽缩放,图片长宽不一致会变形
- ///</summary>
- Appointed,
- ///<summary>
- ///指定宽,高按比例
- ///</summary>
- ScaleWidth,
- ///<summary>
- ///指定高,宽按比例
- ///</summary>
- ScaleHeight,
- ///<summary>
- ///指定高宽裁减,可能只显示部分图片
- ///</summary>
- Cut,
- ///<summary>
- ///按图片比例缩放,不变形,显示全部图片(推荐)
- ///</summary>
- ScaleDown
- }
- }
- }