| By Steven Smith from aspalliance.com
displayimage.asp: Now, lets do this in .NET. We can use the System.Drawing library to open an image and display it, using the following code: displayimage.aspx: Here is the result of using this code as the SRC of an IMG tag: Old:(Notice that there is a problem here -- this is an animated GIF, but this version doesnt show any more than the first frame of the graphic. Not good. So well scrap that version, hope that perhaps there is an animated GIF type supported in the future, and move on.) As you can see, the above example renders the animated GIF image just fine now, thanks to updates to the .NET Framework in Beta2. The HTTPImage Class, below, is no longer necessary, but is left for posterity. -- Steve The HTTPImage Class -- NO LONGER FUNCTIONAL (or necessary) UNDER BETA 2 You can tell were getting to the real thing now, because I actually bothered to put the working code into a class file. In this case, I called it HTTPImage, and its written in C# and has two simple methods. Actually, one overloaded method, outputImageViaHTTP, which takes either a virtual file path or a static file path. Before we get into the class, lets see it in action by looking at a simple ASPX page that uses it. Click here for the example, and below is the source code. Note that the ASP.NET page is passing its own instances of Response and Server to the component (line 8). Well see how the component uses these below. displayimage2.aspx: Pretty cool, eh? The image is actually animated, as its supposed to be. Now, the reason this is even remotely useful is so that if you want to show a random image, like for an ad banner, you can use this method to output the image from your file system. For example, this image tag has as its source the same file that we just looked at (it is animated to fade in about once per minute): Ok, now were finally ready to actually look at the class. First, here is the source code: HTTPImage.cs: As you can see, this is a pretty simple class. No frills. Lets look at the second overloaded method first, since its simpler. On line 38 you can see the declaration. We need to pass in the a Response and a Server object from our ASP.NET page, which we saw on line 8 of displayimage2.aspx. We only need server for this second overloaded call, because were going to use Server.MapPath to determine the absolute path to the image and then simply call the other method. Which takes us to line 19, the other method call. Were going to use the FileStream object to access the image, and we will need to know the files size so that we know when to stop reading and avoid an error. Lines 27 to 30 do all 90% of the work for this class, by loading the buffer with the contents of the file. Then all that remains is to set the content type to an image type (in this case, Ive hardcoded it to be "image/gif", but you could make that a parameter or base it on the file extension) and then use the BinaryWrite command to output the image. The BinaryWrite command is nothing new to Classic ASP developers -- in fact, the basic concepts of this whole article are all old news for Class ASP developers, but I hadnt done this in .NET before, so I thought Id share it with you. Hopefully itll help a few of you get going with .NET a little faster. If you just want to plug the component in and see it work, you can download it here: HTTPImage.dllX. The download didnt like the fact that it was a dll, so remove the X from the end of the filename after you download it. You can get the C# source here. |