image3.aspx by Chris Garrett
1 <% ' import all relevant namespaces %>
2 <%@ import namespace="System" %>
3 <%@ import namespace="System.Drawing" %>
4 <%@ import namespace="System.Drawing.Imaging" %>
5 <%@ import namespace="System.IO" %>
6
7 <script runat="server">
8
9 Function NewthumbSize(currentwidth, currentheight)
10
11
12 ' Calculate the Size of the New image
13
14 dim tempMultiplier as Double
15
16 if currentheight > currentwidth then ' portrait
17 tempMultiplier = 200 / currentheight
18 Else
19 tempMultiplier = 200 / currentwidth
20 end if
21
22
23 dim NewSize as New Size(CInt(currentwidth * tempMultiplier), CInt(currentheight * tempMultiplier))
24
25
26 return NewSize
27 End Function
28
29
30 Sub sendFile()
31
32 ' create New image and bitmap objects. Load the image file and put into a resized bitmap.
33 dim g as System.Drawing.Image = System.Drawing.Image.FromFile(server.mappath(request("src")))
34 dim thisFormat=g.rawformat
35
36 dim thumbSize as New size
37 thumbSize=NewthumbSize(g.width,g.height)
38
39 dim imgOutput as New Bitmap(g, thumbSize.width, thumbSize.height)
40
41 ' Set the contenttype
42
43 if thisformat.equals(system.drawing.imaging.imageformat.Gif) then
44 response.contenttype="image/gif"
45 else
46 response.contenttype="image/jpeg"
47 end if
48
49
50
51 ' send the resized image to the viewer
52 imgOutput.save(response.outputstream, thisformat) ' output to the user
53
54 ' tidy up
55 g.dispose()
56 imgOutput.dispose()
57
58 end sub
59
60 Sub sendError()
61
62 ' if no height, width, src then output "error"
63 dim imgOutput as New bitmap(120, 120, pixelformat.format24bpprgb)
64 dim g as graphics = graphics.fromimage(imgOutput) ' create a New graphic object from the above bmp
65 g.clear(color.yellow) ' blank the image
66 g.drawString("ERROR!", New font("verdana",14,fontstyle.bold),systembrushes.windowtext, New pointF(2,2))
67
68 ' Set the contenttype
69 response.contenttype="image/gif"
70
71 ' send the resized image to the viewer
72 imgOutput.save(response.outputstream, imageformat.gif) ' output to the user
73
74 ' tidy up
75 g.dispose()
76 imgOutput.dispose()
77
78
79 end sub
80
81
82 </script>
83
84
85
86 <%
87
88
89 ' make sure Nothing has gone to the client
90 response.clear
91
92
93 if request("src")="" then
94
95 call sendError()
96
97 else
98
99 if file.exists(server.mappath(request("src"))) then
100 call sendFile()
101 else
102 call sendError()
103 end if
104
105 end if
106
107
108 response.end
109
110 %>
111
112