`
- 浏览:
1109010 次
-
-
usingSystem;
-
usingSystem.Drawing;
-
usingSystem.Drawing.Imaging;
-
usingSystem.IO;
-
usingSystem.Web;
-
-
namespaceXXXX.Common
-
{
-
-
-
-
publicclassImagesHelper
-
{
-
-
-
-
-
-
-
-
-
publicstaticintgetImgWidth(Streamstream)
-
{
-
Imageimg=Image.FromStream(stream);
-
intresult=img.Width;
-
img.Dispose();
-
stream.Dispose();
-
returnresult;
-
}
-
-
-
-
-
-
-
publicstaticintgetImgWidth(stringfilePath)
-
{
-
Imageimg=Image.FromFile(filePath);
-
intresult=img.Width;
-
img.Dispose();
-
returnresult;
-
}
-
-
-
#region从文件流生成缩略图
-
-
-
-
-
-
-
-
-
-
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;
-
}
-
-
-
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
-
{
-
-
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从文件路径生成缩略图
-
-
-
-
-
-
-
-
-
-
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;
-
}
-
-
-
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
-
{
-
-
bitmap.Save(savePath,ImageFormat.Jpeg);
-
}
-
catch(Exceptione)
-
{
-
throwe;
-
}
-
finally
-
{
-
img.Dispose();
-
bitmap.Dispose();
-
g.Dispose();
-
}
-
-
returntrue;
-
-
}
-
#endregion
-
-
#region获取图片格式
-
-
-
-
-
-
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
-
-
-
-
-
-
-
-
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())
-
{
-
-
-
-
-
-
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;
-
-
}
-
-
-
-
-
-
-
-
-
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())
-
{
-
-
-
-
-
-
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从图片中截取一张指定大小的图片
-
-
-
-
-
-
-
-
-
-
publicstaticvoidCaptureImage(stringsFromFilePath,stringsaveFilePath,intwidth,intheight,intspaceX,intspaceY)
-
{
-
-
ImagefromImage=Image.FromFile(sFromFilePath);
-
intx=0;
-
inty=0;
-
-
-
-
-
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
-
{
-
-
-
-
Center,
-
-
-
-
TopLeft,
-
-
-
-
BottomLeft,
-
-
-
-
BottomRight,
-
-
-
-
TopRigth
-
}
-
-
-
-
-
publicenumThumbNailScale
-
{
-
-
-
-
Appointed,
-
-
-
-
ScaleWidth,
-
-
-
-
ScaleHeight,
-
-
-
-
Cut,
-
-
-
-
ScaleDown
-
}
-
}
-
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
在网站建设过程中,需要处理图片的地方多不胜数,用PHP的图片函数处理图片,十分繁琐。而且对新手来讲十分...现在我们可以用PHPThumb类库来处理图片,包括,图片尺寸调整,图片截取,图片加水印,图片旋转等等功能。
/// 缩略图存放地址 /// 指定的边长(正方型) /// 质量(范围0-100) public static void CutForSquare(string filePath, string fileSaveUrl, int side, int quality) { FileStream fileStream = new ...
她能轻易的生成缩略图,调整尺寸、转换格式、添加水印一步到位!向导式的操作方式,轻易上手,而功能丝毫不减。所有的批处理操作都可以应用图像脚本处理命令,再复杂的操作也可以一步到位。而且所有这些功能,您都...
能轻易的生成缩略图,调整尺寸、转换格式、添加水印一步到位!向导式的操作方式,轻易上手。所有的批处理操作都可以应用图像脚本处理命令,再复杂的操作也可以一步到位。 淘淘图片批处理之星能为您做什么? ...
PHP生成缩略图网上代码很多,大部分感觉写的太死,而且不能自适应大小。下面放出一个老外的版本
3.其他更丰富的裁剪功能,请参见主页说明或程序,比如:提取图片上的文字并保存先裁剪后加水印一步到位忽略处理过的文件夹手动指定裁剪区域多裁剪区域裁剪打印二维码图片转Pdf 过滤小图或缩略图 AB文件夹配对拼合 ...
自动生成缩略图清晰度设置3. 是否开启防盗链功能4. 长文件名自动截取(暂无)5. 支持文字水印,可自定义字体、阴影、颜色、旋转角度等6. 支持图片水印7. 手动生成/更新所有html图片页(暂无)8. 可以根据需要设定目录...
一个很好用的php图片上传类,包含了缩略图的一些功能,存放路径,水印图片的存放,大小限制,设定当前时间为图片名称,截取的数量,生成新的文件名,并附上use.php文件注明类的使用方法,供朋友们学习参考和使用这个...
主要介绍了PHPThumb图片处理实例,例如生成缩略图、图片尺寸调整、图片截取、图片加水印、图片旋转等,需要的朋友可以参考下
例如产品图片,往往要提供多个缩略图,例如在列表页为小图,在产品详情页为中图,当鼠标放到中图上再显示大图等。为此上传服务器需要提供图片的适当裁剪成大中小各种尺寸的图片,以适应多种情况。 其实,不只是图片...
13、修复了上传图片时如果图片是动画为该图片生成缩略图和水印图片 14、修复了在gbk项目下无法保存ftp密码的问题 15、修复了合并用户时合并短消息异常 16、修复了主题自动关闭后仍可以回复的问题 17、修复了编辑...
jsp编程的一些小技巧总结,绝对实用。包括JSP编程中常用的js技术。 1.JSP编程中常用的js技术 2. 在下拉列表框里选择一个值后跳出新窗口? 3. 在JSP中启动execl? 4. 两级下拉列表框联动菜单...121.上传图片并生成缩略图?
13、修复了上传图片时如果图片是动画为该图片生成缩略图和水印图片 14、修复了在gbk项目下无法保存ftp密码的问题 15、修复了合并用户时合并短消息异常 16、修复了主题自动关闭后仍可以回复的问题 17、修复了编辑...
2.[改进]改进后台文章管理 缩略图示意图,图片图标代表本地缩略图,电脑图标代表远程缩略图 3.[纠正]纠正外部调用代码会显示出未审核状态和隐藏状态的文章 4.[纠正]纠正外部调用代码对采用外部链接的文章路径没法...
上传图片并生成缩略图? 122.JS实现图形菜单中点击当前图片变另一幅图片? 123.无刷新页面自动刷新? 124.写文件? 125.怎么得到鼠标点击在线图片的位置坐标? 126.页面单线程? 127.数字转中文 ...
书名:《PHP开发实战1200例(第I卷)》(清华大学出版社.潘凯华.刘中华) PDF格式扫描版,全书分为5篇15章,共899页。2011年1月出版。...实例278 缩略图艺术库 371 实例279 提取图像的EXIF信息 374 6.2 控制、显示...
书名:《PHP开发实战1200例(第I卷)》(清华大学出版社.潘凯华.刘中华) PDF格式扫描版,全书分为5篇15章,共899页。2011年1月出版。...实例278 缩略图艺术库 371 实例279 提取图像的EXIF信息 374 6.2 控制、显示...
增加了图片水印及缩略图功能; 6.增加了信息管理的类别转移功能; 7.增加了模板文件化管理功能; 8.增加了蜘蛛来访记录功能,可以随时了解蜘蛛的来访时间; 9.增加了信息来源功能和对信息自动截取描述内容长度的管理...
5.增加了图片水印及缩略图功能; 6.增加了信息管理的类别转移功能; 7.增加了模板文件化管理功能; 8.增加了蜘蛛来访记录功能,可以随时了解蜘蛛的来访时间; 9.增加了信息来源功能和对信息自动截取描述内容长度的...