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}