برنامه ی آپلود تصاویر بر روی سرور با پسوند، طول و عرض مشخص، در صورتی که سایز صحیح نباشد، تصویر تغییر اندازه داده میشود.
1 public bool CheckFileType(string fileName, string MimeType)
2 {
3 string ext = Path.GetExtension(fileName);
4 switch (ext.ToLower())
5 {
6 case “.gif”:
7 if (MimeType.ToLower() == “image/gif”)
8 {
9 return true;
10 }
11 else
12 {
13 return false;
14 }
15
16 break;
17 case “.png”:
18 if (MimeType.ToLower() == “image/png”)
19 {
20 return true;
21 }
22 else if (MimeType.ToLower() == “image/x-png”)
23 {
24 return true;
25 }
26 else
27 {
28 return false;
29 }
30
31 break;
32 case “.jpg”:
33 if (MimeType.ToLower() == “image/jpeg”)
34 {
35 return true;
36 }
37 else if (MimeType.ToLower() == “image/pjpeg”)
38 {
39 return true;
40 }
41 else
42 {
43 return false;
44 }
45
46 break;
47 case “.jpeg”:
48 if (MimeType.ToLower() == “image/jpeg”)
49 {
50 return true;
51 }
52 else if (MimeType.ToLower() == “image/pjpeg”)
53 {
54 return true;
55 }
56 else
57 {
58 return false;
59 }
60
61 break;
62 case “.bmp”:
63 if (MimeType.ToLower() == “image/bmp”)
64 {
65 return true;
66 }
67 else
68 {
69 return false;
70 }
71
72 break;
73 default:
74 return false;
75 }
76 }
77
78 protected void Button1_Click(object sender, EventArgs e)
79 {
80 const int bmpW = 464;// //New image target width
81 const int bmpH = 620;// //New Image target height
82
83 if (FileUpload1.HasFile)
84 {
85 //Clear the error label text
86 lblError.Text = “”;
87
88 //Check to make sure the file to upload has a picture file format extention
89 //and set the target width and height
90
91 if (this.CheckFileType(FileUpload1.FileName, FileUpload1.PostedFile.ContentType))
92 {
93 Int32 newWidth = bmpW;
94
95 Int32 newHeight = bmpH;
96
97 //Use the uploaded filename for saving without the “.” extension
98
99 String upName = FileUpload1.FileName.Substring(0, FileUpload1.FileName.IndexOf(”.”));
100 //Mid(FileUpload1.FileName, 1, (InStr(FileUpload1.FileName, “.”) - 1)) ;
101
102 //Set the save path of the resized image, you will need this directory already created in your web site
103
104 String filePath = “~/Upload/” + upName + “.jpg”;
105
106 //Create a new Bitmap using the uploaded picture as a Stream
107 //Set the new bitmap resolution to 72 pixels per inch
108
109 //This is just for definition
110 Bitmap upBmp = (Bitmap)Bitmap.FromStream(FileUpload1.PostedFile.InputStream);
111
112 Int32 upWidth = upBmp.Width;
113
114 Int32 upHeight = upBmp.Height;
115
116 Bitmap newBmp = new Bitmap(620, 464, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
117
118 // You can change the 620 or 464 on any width and heght you want, and the aspect ratio remains good if respect the patterns
119 if (upWidth > upHeight)
120 {
121 if (upWidth <= 620 && upHeight <= 464)
122 {
123 newBmp = new Bitmap(upWidth, upHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
124 newWidth = upWidth;
125 newHeight = upHeight;
126 }
127 else
128 {
129 newBmp = new Bitmap(620, 464, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
130 newWidth = 620;
131 newHeight = 464;
132 }
133 }
134 else if (upWidth < upHeight)
135 {
136 if (upWidth <= 464 && upHeight <= 620)
137 {
138 newBmp = new Bitmap(upWidth, upHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
139 newWidth = upWidth;
140 newHeight = upHeight;
141 }
142 else
143 {
144 newBmp = new Bitmap(464, 620, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
145 newWidth = 464;
146 newHeight = 620;
147 }
148 }
149 else if (upWidth == upHeight)
150 {
151 if (upWidth <= 500 && upHeight <= 500)
152 {
153 newBmp = new Bitmap(upWidth, upHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
154 newWidth = upWidth;
155 newHeight = upHeight;
156 }
157 else
158 {
159 newBmp = new Bitmap(500, 500, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
160 newWidth = 500;
161 newHeight = 500;
162 }
163 }
164 newBmp.SetResolution(72, 72);
165
166 //Get the uploaded image width and height
167
168 Int32 newX = 0; //Set the new top left drawing position on the image canvas
169
170 Int32 newY = 0;
171
172 //Create a new image from the uploaded picture using the Graphics class
173
174 //Clear the graphic and set the background colour to white
175
176 //Use Antialias and High Quality Bicubic to maintain a good quality picture
177
178 //Save the new bitmap image using //Png// picture format and the calculated canvas positioning
179
180 Graphics newGraphic = Graphics.FromImage(newBmp);
181
182 try
183 {
184 newGraphic.Clear(Color.Empty);
185
186 newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
187
188 newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
189
190 newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight);
191
192 newBmp.Save(MapPath(filePath), System.Drawing.Imaging.ImageFormat.Jpeg);
193
194 //Show the uploaded resized picture in the image control
195
196 Image1.ImageUrl = filePath;
197
198 Image1.Visible = true;
199
200 }
201 catch (Exception ex)
202 {
203 lblError.Text = ex.ToString();
204 throw ex;
205 }
206 finally
207 {
208 upBmp.Dispose();
209
210 newBmp.Dispose();
211
212 newGraphic.Dispose();
213 }
214 }
215 else
216 {
217 lblError.Text = “Please select a picture with a file format extension of either Bmp, Jpg, Jpeg, Gif or Png.”;
218
219 }
220 }
221 }
222
223
224