C#图片按比例缩放

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

C#图片按比例缩放:

1//按比例缩放图片2publicImage ZoomPicture(Image SourceImage,intTargetWidth,intTargetHeight)3{4intIntWidth;//新的图片宽5intIntHeight;//新的图片高6try7{8System.Drawing.Imaging.ImageFormat format =SourceImage.RawFormat;9System.Drawing.Bitmap SaveImage =newSystem.Drawing.Bitmap(TargetWidth, TargetHeight);10Graphics g =Graphics.FromImage(SaveImage);11g.Clear(Color.White);1213//计算缩放图片的大小 http://www.cnblogs.com/roucheng/1415if(SourceImage.Width > TargetWidth && SourceImage.Height <= TargetHeight)//宽度比目的图片宽度大,长度比目的图片长度小16{17IntWidth =TargetWidth;18IntHeight = (IntWidth * SourceImage.Height) /SourceImage.Width;19}20elseif(SourceImage.Width <= TargetWidth && SourceImage.Height > TargetHeight)//宽度比目的图片宽度小,长度比目的图片长度大21{22IntHeight =TargetHeight;23IntWidth = (IntHeight * SourceImage.Width) /SourceImage.Height;24}25elseif(SourceImage.Width <= TargetWidth && SourceImage.Height <= TargetHeight)//长宽比目的图片长宽都小26{27IntHeight =SourceImage.Width;28IntWidth =SourceImage.Height;29}30else//长宽比目的图片的长宽都大31{32IntWidth =TargetWidth;33IntHeight = (IntWidth * SourceImage.Height) /SourceImage.Width;34if(IntHeight > TargetHeight)//重新计算35{36IntHeight =TargetHeight;37IntWidth = (IntHeight * SourceImage.Width) /SourceImage.Height;38}39}4041g.DrawImage(SourceImage, (TargetWidth - IntWidth) /2, (TargetHeight - IntHeight) /2, IntWidth, IntHeight);42SourceImage.Dispose();4344returnSaveImage;45}46catch(Exception ex)47{4849}5051returnnull;52}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航