صفحه: 1 2 3   پایین
  چاپ صفحه  
نويسنده موضوع: آپلود و تغییر سایز عکس در دات نت - #C  (دفعات بازدید: 923 بار)
Siavash
مدیر ارشد
*
تعداد ارسال: 5414



فعالیت هفتگی
0%
سپاسگزاری
-اهدا شده: 3977
-دریافت شده: 1741




« : 05 ارديبهشت 1388,ساعت 20:18:25 »
پاسخپاسخ

برنامه ی آپلود تصاویر بر روی سرور با پسوند، طول و عرض مشخص، در صورتی که سایز صحیح نباشد، تصویر تغییر اندازه داده میشود.


 
کد:
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 
گزارش به مدیر انجمن   خارج شده است

چند روزی شدیدا درگیرم... کمتر سر میزنم.

آنجا که همه مثل هم فکر میکنند، هیچ کس خیلی فکر نمیکند!

تصور كن اگر قرار بود هر كس به اندازه ی دانش خود حرف بزند چه سكوتی بر دنیا حاكم میشد (ناپلئون)
فاطیما
عضو تیم طراحی بازی
*
تعداد ارسال: 741



فعالیت هفتگی
0%
سپاسگزاری
-اهدا شده: 378
-دریافت شده: 750




« پاسخ #1 : 10 ارديبهشت 1388,ساعت 05:29:13 »
پاسخپاسخ

من شرمنده اخلاق ورزشکاریتم
میشه لطف کنی یه توضیح کوچولو بدین؟
 
ممنون از لطف تون
گزارش به مدیر انجمن   خارج شده است
Siavash
مدیر ارشد
*
تعداد ارسال: 5414



فعالیت هفتگی
0%
سپاسگزاری
-اهدا شده: 3977
-دریافت شده: 1741




« پاسخ #2 : 10 ارديبهشت 1388,ساعت 08:31:42 »
پاسخپاسخ

چیز خاصی نیست، تو خط سوم پسوند فایل رو میگیره.
بعدش از خط 4 تا 78 نوع فایل رو با توجه به پسوندش مشخص میکنه: MimeType

از خط 80 به بعد هم که انصافا خیلی خوب کامنت گذاری شده، فقط قسمت های مهمش رو میگم:

. از 80 تا 110 سعی میکنه اطلاعات اندازه، نام و نوع فایل رو برای ذخیر ه آماده کنه.
. در خط 110 یک فایل تصویر جدید ایجاد میکنه و تا خط 164 شرط های مختلف رو چک میکنه تا تصویر به انداز ه ی دلخواه باشه.
. از خط 172 تا 194 با استفاده از توایع سیستمی یه فایل جدید ایجاد میکنه که تمام مشخصات خواسته شده رو با سایز مورد نظر داره و بعد و در محلی که با استفاده از خطوط 80 تا 110 مشخص شده بود ذخیره میکنه.
. در خط 196، 197، 198 نتیجه ی کارش رو به شما نشون میده.
. در خطوط پایانی هم تمامی فضایی که گرفته بود رو آزاد میکنه.
اگه در عملیات تغییر سایز و شناسایی نوع خطایی رخ داده باشه، در خط آخر پیام خطا صادر میشه و شما رو ملزم میکنه یکی از پسوند های مورد نظرش رو انتخاب کنید.

به جز 172 تا 194 مطمئنم بقیه اش رو متوجه شدی. اون قسمت رو هم باید فقط یاد بگیری، چون چیز خاصی نیست، توایع خود سیستمه!
گزارش به مدیر انجمن   خارج شده است
فاطیما
عضو تیم طراحی بازی
*
تعداد ارسال: 741



فعالیت هفتگی
0%
سپاسگزاری
-اهدا شده: 378
-دریافت شده: 750




« پاسخ #3 : 11 ارديبهشت 1388,ساعت 06:18:53 »
پاسخپاسخ

ممنون,مرسی....
ما قرار بیستم بشیم مهندس بعد از این.....
گزارش به مدیر انجمن   خارج شده است
Siavash
مدیر ارشد
*
تعداد ارسال: 5414



فعالیت هفتگی
0%
سپاسگزاری
-اهدا شده: 3977
-دریافت شده: 1741




« پاسخ #4 : 11 ارديبهشت 1388,ساعت 07:43:42 »
پاسخپاسخ

موفق باشین، ولی اینجا هم باشین چشمک
گزارش به مدیر انجمن   خارج شده است
yas
کاربر فعال
*
تعداد ارسال: 108



فعالیت هفتگی
0%
سپاسگزاری
-اهدا شده: 0
-دریافت شده: 1


سیستم عامل:
Windows XP Windows XP
کاوشگر:
MS Internet Explorer 6.0 MS Internet Explorer 6.0



« پاسخ #5 : 11 ارديبهشت 1388,ساعت 14:10:16 »
پاسخپاسخ

سلام
ببین  من کنترل های لازم این کد رو تو صفحه گذاشتم تو اجرا می ره
errorی ام نمی ده
 ولی نمی دونم چرا چیزی نمایش نمی ده
خواهش می کنم یکم توضیح بدین
تودقیقه نودم . پروژهه بد جوری بیخ ریش امه
گزارش به مدیر انجمن   خارج شده است

آن سوی همه....
"خدایی " هست که داشتنش  جبران همه نداشته هاست.!
Siavash
مدیر ارشد
*
تعداد ارسال: 5414



فعالیت هفتگی
0%
سپاسگزاری
-اهدا شده: 3977
-دریافت شده: 1741


سیستم عامل:
Windows XP Windows XP
کاوشگر:
Opera 9.64 Opera 9.64



« پاسخ #6 : 11 ارديبهشت 1388,ساعت 14:22:45 »
پاسخپاسخ

کد تولید شده رو اینجا قرار بده...

(روی صفحه ای که نتیجه رو نمایش میده راست کلیک کن و View Source رو بزن و هرچی توش بون اینجا قرار بده - یادت نره اول دکمه ی رو بزنی بعد کدت رو بین دو تا تگ code قرار بدی)
گزارش به مدیر انجمن   خارج شده است
صفحه: 1 2 3   بالا
  چاپ صفحه  
 
پرش به :