本文由中国C#技术学习中心整理 如果你对本文有不明之处请到技术论坛讨论!
private static Size NewSize( int maxWidth, int maxHeight,
int width, int height ) { double w = 0.0; double h = 0.0; double sw = Convert.ToDouble( width ); double sh = Convert.ToDouble( height );
double mw = Convert.ToDouble( maxWidth ); double mh = Convert.ToDouble( maxHeight );
if (sw < mw && sh < mh) { w = sw; h = sh; } else if (( sw/sh ) > ( mw/mh )) { w = maxWidth; h = ( w * sh )/sw; } else { h = maxHeight; w = ( h * sw )/sh; } return new Size( Convert.ToInt32( w ), Convert.ToInt32( h ) ); }
public static void SendSmallImage( string fileName, string newFile, int maxHeight, int maxWidth ) { System.Drawing.Image img = System.Drawing.Image.FromFile( fileName ); System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat; Size newSize = NewSize( maxWidth, maxHeight, img.Width, img.Height ); Bitmap outBmp = new Bitmap( newSize.Width, newSize.Height ); Graphics g = Graphics.FromImage( outBmp ); g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage( img, new Rectangle( 0, 0, newSize.Width, newSize.Height ), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel ); g.Dispose( ); EncoderParameters encoderParams = new EncoderParameters( ); long[] quality = new long[1]; quality[0] = 100; EncoderParameter encoderParam = new EncoderParameter( System.Drawing.Imaging.Encoder.Quality, quality ); encoderParams.Param[0] = encoderParam; ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders( ); ImageCodecInfo jpegICI = null; for ( int x = 0; x < arrayICI.Length; x++ ) { if ( arrayICI[x].FormatDescription.Equals( "JPEG" ) ) { jpegICI = arrayICI[x]; break; } } if ( jpegICI != null ) { outBmp.Save( newFile, jpegICI, encoderParams ); } else { outBmp.Save( newFile, thisFormat ); } img.Dispose( ); outBmp.Dispose( ); }
本文由中国C#技术学习中心整理 如果你对本文有不明之处请到技术论坛讨论!
|