1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Drawing ;
4+ using System . Drawing . Drawing2D ;
5+ using System . Drawing . Imaging ;
6+ using System . IO ;
7+ using System . Linq ;
8+ using System . Web ;
9+
10+ namespace BookCollection . Helpers
11+ {
12+ public class ImageUpload
13+ {
14+ // set default size here
15+ public int Width { get ; set ; }
16+
17+ public int Height { get ; set ; }
18+
19+ // folder for the upload, you can put this in the web.config
20+ private readonly string UploadPath = "~/Images/Uploaded/" ;
21+
22+ public ImageResult RenameUploadFile ( HttpPostedFileBase file , Int32 counter = 0 )
23+ {
24+ var fileName = Path . GetFileName ( file . FileName ) ;
25+
26+ string prepend = "item_" ;
27+ string finalFileName = prepend + ( ( counter ) . ToString ( ) ) + "_" + fileName ;
28+ if ( System . IO . File . Exists
29+ ( HttpContext . Current . Request . MapPath ( UploadPath + finalFileName ) ) )
30+ {
31+ //file exists => add country try again
32+ return RenameUploadFile ( file , ++ counter ) ;
33+ }
34+ //file doesn't exist, upload item but validate first
35+ return UploadFile ( file , finalFileName ) ;
36+ }
37+
38+ private ImageResult UploadFile ( HttpPostedFileBase file , string fileName )
39+ {
40+ ImageResult imageResult = new ImageResult { Success = true , ErrorMessage = null } ;
41+
42+ var path = Path . Combine ( HttpContext . Current . Request . MapPath ( UploadPath ) , fileName ) ;
43+ string extension = Path . GetExtension ( file . FileName ) ;
44+
45+ //make sure the file is valid
46+ if ( ! ValidateExtension ( extension ) )
47+ {
48+ imageResult . Success = false ;
49+ imageResult . ErrorMessage = "Invalid Extension" ;
50+ return imageResult ;
51+ }
52+
53+ try
54+ {
55+ file . SaveAs ( path ) ;
56+
57+ Image imgOriginal = Image . FromFile ( path ) ;
58+
59+ //pass in whatever value you want
60+ Image imgActual = Scale ( imgOriginal ) ;
61+ imgOriginal . Dispose ( ) ;
62+ imgActual . Save ( path ) ;
63+ imgActual . Dispose ( ) ;
64+
65+ imageResult . ImageName = fileName ;
66+
67+ return imageResult ;
68+ }
69+ catch ( Exception ex )
70+ {
71+ // you might NOT want to show the exception error for the user
72+ // this is generally logging or testing
73+
74+ imageResult . Success = false ;
75+ imageResult . ErrorMessage = ex . Message ;
76+ return imageResult ;
77+ }
78+ }
79+
80+ private bool ValidateExtension ( string extension )
81+ {
82+ extension = extension . ToLower ( ) ;
83+ switch ( extension )
84+ {
85+ case ".jpg" :
86+ return true ;
87+ case ".png" :
88+ return true ;
89+ case ".gif" :
90+ return true ;
91+ case ".jpeg" :
92+ return true ;
93+ default :
94+ return false ;
95+ }
96+ }
97+
98+ private Image Scale ( Image imgPhoto )
99+ {
100+ float sourceWidth = imgPhoto . Width ;
101+ float sourceHeight = imgPhoto . Height ;
102+ float destHeight = 0 ;
103+ float destWidth = 0 ;
104+ int sourceX = 0 ;
105+ int sourceY = 0 ;
106+ int destX = 0 ;
107+ int destY = 0 ;
108+
109+ // force resize, might distort image
110+ if ( Width != 0 && Height != 0 )
111+ {
112+ destWidth = Width ;
113+ destHeight = Height ;
114+ }
115+ // change size proportially depending on width or height
116+ else if ( Height != 0 )
117+ {
118+ destWidth = ( float ) ( Height * sourceWidth ) / sourceHeight ;
119+ destHeight = Height ;
120+ }
121+ else
122+ {
123+ destWidth = Width ;
124+ destHeight = ( float ) ( sourceHeight * Width / sourceWidth ) ;
125+ }
126+
127+ Bitmap bmPhoto = new Bitmap ( ( int ) destWidth , ( int ) destHeight ,
128+ PixelFormat . Format32bppPArgb ) ;
129+ bmPhoto . SetResolution ( imgPhoto . HorizontalResolution , imgPhoto . VerticalResolution ) ;
130+
131+ Graphics grPhoto = Graphics . FromImage ( bmPhoto ) ;
132+ grPhoto . InterpolationMode = InterpolationMode . HighQualityBicubic ;
133+
134+ grPhoto . DrawImage ( imgPhoto ,
135+ new Rectangle ( destX , destY , ( int ) destWidth , ( int ) destHeight ) ,
136+ new Rectangle ( sourceX , sourceY , ( int ) sourceWidth , ( int ) sourceHeight ) ,
137+ GraphicsUnit . Pixel ) ;
138+
139+ grPhoto . Dispose ( ) ;
140+
141+ return bmPhoto ;
142+ }
143+ }
144+ }
0 commit comments