![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
一个可以使得上传的图片大小按照指定的宽度,高度自动按比例进行缩放
2![](/Images/OutliningIndicators/None.gif)
3
,生成的JPG还可以指定其图片的压缩比。很实用。
4![](/Images/OutliningIndicators/None.gif)
5
public string upimgscalesave(HttpPostedFile UpFile,string id,int imgw,string bcwz)
6![](/Images/OutliningIndicators/None.gif)
7
//UpFile HtmlInputFile控件类
8![](/Images/OutliningIndicators/None.gif)
9
//id 上传后保存的文件名
10![](/Images/OutliningIndicators/None.gif)
11
//改变的图片宽度,高度吗,当然是自动计算拉!
12![](/Images/OutliningIndicators/None.gif)
13
//bcwz上传后要存服务器那里啊!
14![](/Images/OutliningIndicators/None.gif)
15![](/Images/OutliningIndicators/None.gif)
16![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
17
int FileLength;
18
int imgh=0;
19
FileLength = UpFile.ContentLength; //记录文件长度
20
21
if (FileLength == 0)
22![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ //文件长度为零时
23
return "null";
24
}
25
else
26![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
28
Stream StreamObject = UpFile.InputStream; //建立数据流对像
29
StreamObject.Read(FileByteArray,0,FileLength);
30
System.Drawing.Image orimg=System.Drawing.Image.FromStream(StreamObject);
31
32
33
string mimeType ="image/jpeg";
34
ImageCodecInfo[] encoders;
35
encoders = ImageCodecInfo.GetImageEncoders();
36
for(int j = 0; j < encoders.Length; ++j)
37![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
if(encoders[j].MimeType == mimeType)
39
myImageCodecInfo=encoders[j];
40
}
41![](/Images/OutliningIndicators/InBlock.gif)
42![](/Images/OutliningIndicators/InBlock.gif)
43
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
44
myEncoderParameters = new EncoderParameters(1);
45
myEncoderParameter = new EncoderParameter(myEncoder, 80L);
46![](/Images/OutliningIndicators/InBlock.gif)
47
48![](/Images/OutliningIndicators/InBlock.gif)
49
50![](/Images/OutliningIndicators/InBlock.gif)
51
//80L压缩比拉
52![](/Images/OutliningIndicators/InBlock.gif)
53![](/Images/OutliningIndicators/InBlock.gif)
54
55![](/Images/OutliningIndicators/InBlock.gif)
56
myEncoderParameters.Param[0] = myEncoderParameter;
57
58
59
if(imgw==0&&imgh==0)
60![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
62
63
64
orimg.Save(bcwz+"\\"+id+".jpg",myImageCodecInfo,myEncoderParameters);
65
66
67
68
//orimg.Save(bcwz+"\\"+id+".jpg",ImageFormat.Jpeg);
69
orimg.Dispose();
70
71
}
72
else
73![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
double bl=0.0F;
75
bl=imgw/Convert.ToDouble(orimg.Width);
76
imgh=Convert.ToInt32(orimg.Height*bl);
77
b = new Bitmap(imgw,imgh);
78
g = Graphics.FromImage(b);
79
Rectangle destRect1 = new Rectangle( 0, 0, imgw, imgh);
80
//GraphicsUnit units = GraphicsUnit.Pixel;
81
g.DrawImage(orimg,destRect1);
82
b.Save(bcwz+"\\"+id+".jpg",myImageCodecInfo,myEncoderParameters);
83
b.Dispose();
84
g.Dispose();
85
orimg.Dispose();
86![](/Images/OutliningIndicators/InBlock.gif)
87
}
88
return bcwz;
89
}
90
}
91![](/Images/OutliningIndicators/None.gif)