private static Size NewSize( int maxWidth,
int maxHeight, int width,
int height )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
w = sw;
h = sh;
}
else if (( sw/sh ) > ( mw/mh ))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
w = maxWidth;
h = ( w * sh )/sw;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
h = maxHeight;
w = ( h * sw )/sh;
}
return new Size( Convert.ToInt32( w ), Convert.ToInt32( h ) );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public static void SendSmallImage( string fileName,
string newFile,
int maxHeight,
int maxWidth )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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 对象.
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders( );
ImageCodecInfo jpegICI = null;
for ( int x = 0;
x < arrayICI.Length;
x++ )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( arrayICI[x].FormatDescription.Equals( "JPEG" ) )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
jpegICI = arrayICI[x];
//设置JPEG编码
break;
}
}
if ( jpegICI != null )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
outBmp.Save( newFile, jpegICI, encoderParams );
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
outBmp.Save( newFile,
thisFormat );
}
img.Dispose( );
outBmp.Dispose( );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2007-09-11 10:05
宿远
阅读(
379)
评论()
编辑
收藏
举报