zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#图片截取压缩(百分比压缩/大小压缩)实现代码

c#代码 实现 图片 大小 压缩 截取 百分比
2023-06-13 09:14:44 时间
前端时间朋友要传一些图片给我,全是大图,考虑到网速的限制,让他处理下图片大小再给我,这厮居然不知道用什么工具.

为了娱乐写了个截取图片和压缩图片你的小工具
1.按照百分比截图
复制代码代码如下:

ViewCode
///<summary>
///按照比例缩小图片
///</summary>
///<paramname="srcImage">要缩小的图片</param>
///<paramname="percent">缩小比例</param>
///<returns>缩小后的结果</returns>
publicstaticBitmapPercentImage(ImagesrcImage,doublepercent)
{
//缩小后的高度
intnewH=int.Parse(Math.Round(srcImage.Height*percent).ToString());
//缩小后的宽度
intnewW=int.Parse(Math.Round(srcImage.Width*percent).ToString());
try
{
//要保存到的图片
Bitmapb=newBitmap(newW,newH);
Graphicsg=Graphics.FromImage(b);
//插值算法的质量
g.InterpolationMode=InterpolationMode.Default;
g.DrawImage(srcImage,newRectangle(0,0,newW,newH),newRectangle(0,0,srcImage.Width,srcImage.Height),GraphicsUnit.Pixel);
g.Dispose();
returnb;
}
catch(Exception)
{
returnnull;
}
}

2.按照指定像素大小截图
复制代码代码如下:

ViewCode
///<summary>
///按照指定大小缩放图片
///</summary>
///<paramname="srcImage"></param>
///<paramname="iWidth"></param>
///<paramname="iHeight"></param>
///<returns></returns>
publicstaticBitmapSizeImage(ImagesrcImage,intiWidth,intiHeight)
{
try
{
//要保存到的图片
Bitmapb=newBitmap(iWidth,iHeight);
Graphicsg=Graphics.FromImage(b);
//插值算法的质量
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.DrawImage(srcImage,newRectangle(0,0,iWidth,iHeight),newRectangle(0,0,srcImage.Width,srcImage.Height),GraphicsUnit.Pixel);
g.Dispose();
returnb;
}
catch(Exception)
{
returnnull;
}
}

3.按照指定像素大小截图(但为了保证图片的原始比例,将对图片从中心进行截取,达到图片不被拉伸的效果)
复制代码代码如下:
ViewCode
///<summary>
///按照指定大小缩放图片,但是为了保证图片宽高比自动截取
///</summary>
///<paramname="srcImage"></param>
///<paramname="iWidth"></param>
///<paramname="iHeight"></param>
///<returns></returns>
publicstaticBitmapSizeImageWithOldPercent(ImagesrcImage,intiWidth,intiHeight)
{
try
{
//要截取图片的宽度(临时图片)
intnewW=srcImage.Width;
//要截取图片的高度(临时图片)
intnewH=srcImage.Height;
//截取开始横坐标(临时图片)
intnewX=0;
//截取开始纵坐标(临时图片)
intnewY=0;
//截取比例(临时图片)
doublewhPercent=1;
whPercent=((double)iWidth/(double)iHeight)*((double)srcImage.Height/(double)srcImage.Width);
if(whPercent>1)
{
//当前图片宽度对于要截取比例过大时
newW=int.Parse(Math.Round(srcImage.Width/whPercent).ToString());
}
elseif(whPercent<1)
{
//当前图片高度对于要截取比例过大时
newH=int.Parse(Math.Round(srcImage.Height*whPercent).ToString());
}
if(newW!=srcImage.Width)
{
//宽度有变化时,调整开始截取的横坐标
newX=Math.Abs(int.Parse(Math.Round(((double)srcImage.Width-newW)/2).ToString()));
}
elseif(newH==srcImage.Height)
{
//高度有变化时,调整开始截取的纵坐标
newY=Math.Abs(int.Parse(Math.Round(((double)srcImage.Height-(double)newH)/2).ToString()));
}
//取得符合比例的临时文件
BitmapcutedImage=CutImage(srcImage,newX,newY,newW,newH);
//保存到的文件
Bitmapb=newBitmap(iWidth,iHeight);
Graphicsg=Graphics.FromImage(b);
//插值算法的质量
g.InterpolationMode=InterpolationMode.Default;
g.DrawImage(cutedImage,newRectangle(0,0,iWidth,iHeight),newRectangle(0,0,cutedImage.Width,cutedImage.Height),GraphicsUnit.Pixel);
g.Dispose();
returnb;
}
catch(Exception)
{
returnnull;
}
}

4.jpeg图片质量压缩,压缩的比例参数在1-100之间。(适量的压缩对于肉眼来说没有什么明显的区别,但是能够大大的减小图片的占用大小)
复制代码代码如下:
ViewCode
///<summary>
///jpeg图片压缩
///</summary>
///<paramname="sFile"></param>
///<paramname="outPath"></param>
///<paramname="flag"></param>
///<returns></returns>
publicstaticboolGetPicThumbnail(stringsFile,stringoutPath,intflag)
{
System.Drawing.ImageiSource=System.Drawing.Image.FromFile(sFile);
ImageFormattFormat=iSource.RawFormat;
//以下代码为保存图片时,设置压缩质量
EncoderParametersep=newEncoderParameters();
long[]qy=newlong[1];
qy[0]=flag;//设置压缩的比例1-100
EncoderParametereParam=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,qy);
ep.Param[0]=eParam;
try
{
ImageCodecInfo[]arrayICI=ImageCodecInfo.GetImageEncoders();
ImageCodecInfojpegICIinfo=null;
for(intx=0;x<arrayICI.Length;x++)
{
if(arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo=arrayICI[x];
break;
}
}
if(jpegICIinfo!=null)
{
iSource.Save(outPath,jpegICIinfo,ep);//dFile是压缩后的新路径
}
else
{
iSource.Save(outPath,tFormat);
}
returntrue;
}
catch
{
returnfalse;
}
finally
{
iSource.Dispose();
iSource.Dispose();
}
}

PS:之上用的CutImage方法的补充
复制代码代码如下:
ViewCode
///<summary>
///剪裁--用GDI+
///</summary>
///<paramname="b">原始Bitmap</param>
///<paramname="StartX">开始坐标X</param>
///<paramname="StartY">开始坐标Y</param>
///<paramname="iWidth">宽度</param>
///<paramname="iHeight">高度</param>
///<returns>剪裁后的Bitmap</returns>
publicstaticBitmapCutImage(Imageb,intStartX,intStartY,intiWidth,intiHeight)
{
if(b==null)
{
returnnull;
}
intw=b.Width;
inth=b.Height;
if(StartX>=w||StartY>=h)
{
//开始截取坐标过大时,结束处理
returnnull;
}
if(StartX+iWidth>w)
{
//宽度过大时只截取到最大大小
iWidth=w-StartX;
}
if(StartY+iHeight>h)
{
//高度过大时只截取到最大大小
iHeight=h-StartY;
}
try
{
BitmapbmpOut=newBitmap(iWidth,iHeight);
Graphicsg=Graphics.FromImage(bmpOut);
g.DrawImage(b,newRectangle(0,0,iWidth,iHeight),newRectangle(StartX,StartY,iWidth,iHeight),GraphicsUnit.Pixel);
g.Dispose();
returnbmpOut;
}
catch
{
returnnull;
}
}

再次记录下截取的代码,虽然简单,如果重写还是需要花费时间。