![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
一个可以使得上传的图片大小按照指定的宽度,高度自动按比例进行缩放
![](/Images/OutliningIndicators/None.gif)
,生成的JPG还可以指定其图片的压缩比。很实用。
![](/Images/OutliningIndicators/None.gif)
public string upimgscalesave(HttpPostedFile UpFile,string id,int imgw,string bcwz)
![](/Images/OutliningIndicators/None.gif)
//UpFile HtmlInputFile控件类
![](/Images/OutliningIndicators/None.gif)
//id 上传后保存的文件名
![](/Images/OutliningIndicators/None.gif)
//改变的图片宽度,高度吗,当然是自动计算拉!
![](/Images/OutliningIndicators/None.gif)
//bcwz上传后要存服务器那里啊!
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int FileLength;
int imgh=0;
FileLength = UpFile.ContentLength; //记录文件长度
if (FileLength == 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ //文件长度为零时
return "null";
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对像
StreamObject.Read(FileByteArray,0,FileLength);
System.Drawing.Image orimg=System.Drawing.Image.FromStream(StreamObject);
string mimeType ="image/jpeg";
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(int j = 0; j < encoders.Length; ++j)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(encoders[j].MimeType == mimeType)
myImageCodecInfo=encoders[j];
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, 80L);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//80L压缩比拉
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
myEncoderParameters.Param[0] = myEncoderParameter;
if(imgw==0&&imgh==0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
orimg.Save(bcwz+"\\"+id+".jpg",myImageCodecInfo,myEncoderParameters);
//orimg.Save(bcwz+"\\"+id+".jpg",ImageFormat.Jpeg);
orimg.Dispose();
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
double bl=0.0F;
bl=imgw/Convert.ToDouble(orimg.Width);
imgh=Convert.ToInt32(orimg.Height*bl);
b = new Bitmap(imgw,imgh);
g = Graphics.FromImage(b);
Rectangle destRect1 = new Rectangle( 0, 0, imgw, imgh);
//GraphicsUnit units = GraphicsUnit.Pixel;
g.DrawImage(orimg,destRect1);
b.Save(bcwz+"\\"+id+".jpg",myImageCodecInfo,myEncoderParameters);
b.Dispose();
g.Dispose();
orimg.Dispose();
![](/Images/OutliningIndicators/InBlock.gif)
}
return bcwz;
}
}
![](/Images/OutliningIndicators/None.gif)