c#

位置:IT落伍者 >> c# >> 浏览文章

.Net图片处理函数


发布日期:2018年09月28日
 
.Net图片处理函数

///<summary>
///获取一个图片按等比例缩小后的大小
///</summary>
///<paramname="maxWidth">需要缩小到的宽度</param>
///<paramname="maxHeight">需要缩小到的高度</param>
///<paramname="imageOriginalWidth">图片的原始宽度</param>
///<paramname="imageOriginalHeight">图片的原始高度</param>
///<returns>返回图片按等比例缩小后的实际大小</returns>
publicstaticSizeGetNewSize(intmaxWidthintmaxHeightintimageOriginalWidthintimageOriginalHeight)
{
doublew=;
doubleh=;
doublesw=ConvertToDouble(imageOriginalWidth);
doublesh=ConvertToDouble(imageOriginalHeight);
doublemw=ConvertToDouble(maxWidth);
doublemh=ConvertToDouble(maxHeight);

if(sw<mw&&sh<mh)
{
w=sw;
h=sh;
}
elseif((sw/sh)>(mw/mh))
{
w=maxWidth;
h=(w*sh)/sw;
}
else
{
h=maxHeight;
w=(h*sw)/sh;
}

returnnewSize(ConvertToInt(w)ConvertToInt(h));
}

///<summary>
///对给定的一个图片(Image对象)生成一个指定大小的缩略图
///</summary>
///<paramname="originalImage">原始图片</param>
///<paramname="thumMaxWidth">缩略图的宽度</param>
///<paramname="thumMaxHeight">缩略图的高度</param>
///<returns>返回缩略图的Image对象</returns>
publicstaticSystemDrawingImageGetThumbNailImage(SystemDrawingImageoriginalImageintthumMaxWidthintthumMaxHeight)
{
SizethumRealSize=SizeEmpty;
SystemDrawingImagenewImage=originalImage;
Graphicsgraphics=null;

try
{
thumRealSize=GetNewSize(thumMaxWidththumMaxHeightoriginalImageWidthoriginalImageHeight);
newImage=newBitmap(thumRealSizeWidththumRealSizeHeight);
graphics=GraphicsFromImage(newImage);

graphicsCompositingQuality=CompositingQualityHighQuality;
graphicsInterpolationMode=InterpolationModeHighQualityBicubic;
graphicsSmoothingMode=SmoothingModeHighQuality;

graphicsClear(ColorTransparent);

graphicsDrawImage(originalImagenewRectangle(thumRealSizeWidththumRealSizeHeight)newRectangle(originalImageWidthoriginalImageHeight)GraphicsUnitPixel);
}
catch{}
finally
{
if(graphics!=null)
{
graphicsDispose();
graphics=null;
}
}

returnnewImage;
}
///<summary>
///对给定的一个图片文件生成一个指定大小的缩略图
///</summary>
///<paramname="originalImage">图片的物理文件地址</param>
///<paramname="thumMaxWidth">缩略图的宽度</param>
///<paramname="thumMaxHeight">缩略图的高度</param>
///<returns>返回缩略图的Image对象</returns>
publicstaticSystemDrawingImageGetThumbNailImage(stringimageFileintthumMaxWidthintthumMaxHeight)
{
SystemDrawingImageoriginalImage=null;
SystemDrawingImagenewImage=null;

try
{
originalImage=SystemDrawingImageFromFile(imageFile);
newImage=GetThumbNailImage(originalImagethumMaxWidththumMaxHeight);
}
catch{}
finally
{
if(originalImage!=null)
{
originalImageDispose();
originalImage=null;
}
}

returnnewImage;
}
///<summary>
///对给定的一个图片文件生成一个指定大小的缩略图并将缩略图保存到指定位置
///</summary>
///<paramname="originalImageFile">图片的物理文件地址</param>
///<paramname="thumbNailImageFile">缩略图的物理文件地址</param>
///<paramname="thumMaxWidth">缩略图的宽度</param>
///<paramname="thumMaxHeight">缩略图的高度</param>
publicstaticvoidMakeThumbNail(stringoriginalImageFilestringthumbNailImageFileintthumMaxWidthintthumMaxHeight)
{
SystemDrawingImagenewImage=GetThumbNailImage(originalImageFilethumMaxWidththumMaxHeight);
try
{
newImageSave(thumbNailImageFileImageFormatJpeg);
}
catch
{}
finally
{
newImageDispose();
newImage=null;
}
}
///<summary>
///将一个图片的内存流调整为指定大小并返回调整后的内存流
///</summary>
///<paramname="originalImageStream">原始图片的内存流</param>
///<paramname="newWidth">新图片的宽度</param>
///<paramname="newHeight">新图片的高度</param>
///<returns>返回调整后的图片的内存流</returns>
publicstaticMemoryStreamResizeImage(StreamoriginalImageStreamintnewWidthintnewHeight)
{
MemoryStreamnewImageStream=null;

SystemDrawingImagenewImage=GlobalsGetThumbNailImage(SystemDrawingImageFromStream(originalImageStream)newWidthnewHeight);
if(newImage!=null)
{
newImageStream=newMemoryStream();
newImageSave(newImageStreamImageFormatJpeg);
}

returnnewImageStream;
}
///<summary>
///将一个内存流保存为磁盘文件
///</summary>
///<paramname="stream">内存流</param>
///<paramname="newFile">目标磁盘文件地址</param>
publicstaticvoidSaveStreamToFile(StreamstreamstringnewFile)
{
if(stream==null||streamLength==||stringIsNullOrEmpty(newFile))
{
return;
}

byte[]buffer=newbyte[streamLength];
streamPosition=;
streamRead(bufferbufferLength);
FileStreamfileStream=newFileStream(newFileFileModeOpenOrCreateFileAccessWrite);
fileStreamWrite(bufferbufferLength);
fileStreamFlush();
fileStreamClose();
fileStreamDispose();
}
///<summary>
///对一个指定的图片加上图片水印效果
///</summary>
///<paramname="imageFile">图片文件地址</param>
///<paramname="waterImage">水印图片(Image对象)</param>
publicstaticvoidCreateImageWaterMark(stringimageFileSystemDrawingImagewaterImage)
{
if(stringIsNullOrEmpty(imageFile)||!FileExists(imageFile)||waterImage==null)
{
return;
}

SystemDrawingImageoriginalImage=SystemDrawingImageFromFile(imageFile);

if(originalImageWidth<waterImageWidth||originalImageHeight<waterImageHeight)
{
return;
}

Graphicsgraphics=GraphicsFromImage(originalImage);

intx=originalImageWidthwaterImageWidth;
inty=originalImageHeightwaterImageHeight;
intwidth=waterImageWidth;
intheight=waterImageHeight;

graphicsDrawImage(waterImagenewRectangle(xywidthheight)widthheightGraphicsUnitPixel);
graphicsDispose();

MemoryStreamstream=newMemoryStream();
originalImageSave(streamImageFormatJpeg);
originalImageDispose();

SystemDrawingImageimageWithWater=SystemDrawingImageFromStream(stream);

imageWithWaterSave(imageFile);
imageWithWaterDispose();
}
///<summary>
///对一个指定的图片加上文字水印效果
///</summary>
///<paramname="imageFile">图片文件地址</param>
///<paramname="waterText">水印文字内容</param>
publicstaticvoidCreateTextWaterMark(stringimageFilestringwaterText)
{
if(stringIsNullOrEmpty(imageFile)||stringIsNullOrEmpty(waterText)||!FileExists(imageFile))
{
return;
}

SystemDrawingImageoriginalImage=SystemDrawingImageFromFile(imageFile);

Graphicsgraphics=GraphicsFromImage(originalImage);

graphicsSmoothingMode=SmoothingModeHighQuality;
graphicsTextRenderingHint=TextRenderingHintClearTypeGridFit;
graphicsCompositingQuality=CompositingQualityHighQuality;
graphicsInterpolationMode=InterpolationModeHighQualityBicubic;

SolidBrushbrush=newSolidBrush(ColorFromArgb());
FontwaterTextFont=newFont("Arial"FontStyleRegular);
SizeFwaterTextSize=graphicsMeasureString(waterTextwaterTextFont);

floatx=(float)originalImageWidthwaterTextSizeWidthF;
floaty=(float)originalImageHeightwaterTextSizeHeightF;

graphicsDrawString(waterTextwaterTextFontbrushxy);

graphicsDispose();
brushDispose();

MemoryStreamstream=newMemoryStream();
originalImageSave(streamImageFormatJpeg);
originalImageDispose();

SystemDrawingImageimageWithWater=SystemDrawingImageFromStream(stream);

imageWithWaterSave(imageFile);
imageWithWaterDispose();
}

///<summary>
///判断上传组件是否包含内容
///</summary>
///<paramname="fileUpload">ASPNET标准上传组件</param>
///<returns>如果数据有效则返回True否则返回False</returns>
publicstaticboolIsAttachmentValid(FileUploadfileUpload)
{
if(fileUpload!=null&&
fileUploadPostedFile!=null&&
!stringIsNullOrEmpty(fileUploadPostedFileFileName)&&
fileUploadPostedFileContentLength>)
{
returntrue;
}
returnfalse;
}

               

上一篇:.NET Framework 4.5新特性介绍

下一篇:.net基础收集汇总