Forum Moderators: open

Message Too Old, No Replies

Displaying an Image in ASP

A System.Drawing.Image, that is..

         

garann

8:49 pm on May 8, 2003 (gmt 0)

10+ Year Member



Hi,

I'm trying to find out if there's an alternative method of displaying a System.Drawing.Image object from the server in an ASP.NET page. The method I've seen used everywhere is to save it to the Response stream:

objImage.Save(Response.OutputStream, ImageFormat.gif)

I don't think that will work for me, though, as I'm displaying the results of a search, with thumbnails. So if I tried the response stream method, I'd have to have a bunch of different ASP pages, all doing the same thing (putting an image in the stream), to fill the src attributes of my img tags.

Does anyone know another way of showing the Images? I looked at the asp:Image server control, but it seems it only takes a URL, not an object.

TIA,
g.

makeupalley

5:37 pm on May 10, 2003 (gmt 0)

10+ Year Member



Here's what I'm doing:

I made a file called thumb.aspx which looks like this:
<%@ OutputCache Duration="120" VaryByParam="FileName;MaxWidth;MaxHeight" %>
<%@ Import Namespace=System.Drawing %>
<%@ Import Namespace=System %>
<%@ Import Namespace=System.Web %>

<script language="VB" runat="server">

Sub Page_Load(Sender As Object, E As EventArgs)

Dim orginalimg, thumb As System.Drawing.Image
Dim resImage As System.Drawing.Graphics
Dim FileName As String
Dim inp As New IntPtr()
Dim width, height As Integer
Dim imgHeight, imgWidth, MaxWidth, MaxHeight As Integer
Dim rootPath As String

MaxWidth = Request.QueryString("MaxWidth")
MaxHeight = Request.QueryString("MaxHeight")

rootPath = "C:\inetbub\www\mysite.com\images\"

FileName = rootPath & Request.QueryString("FileName")

Try
orginalimg = orginalimg.FromFile(FileName)
imgHeight = orginalimg.Height
imgWidth = orginalimg.Width

If imgWidth > MaxWidth Or imgHeight > MaxHeight Then

'Determine what dimension is off by more
Dim deltaWidth As Integer = imgWidth - MaxWidth
Dim deltaHeight As Integer = imgHeight - MaxHeight
Dim scaleFactor As Double

If deltaHeight > deltaWidth Then
'Scale by the height
scaleFactor = MaxHeight / imgHeight

Else
'Scale by the Width
scaleFactor = MaxWidth / imgWidth
End If

imgWidth *= scaleFactor
imgHeight *= scaleFactor

End If
Catch
orginalimg = orginalimg.FromFile(rootpath & "error.gif") ' Fetch error.gif
End Try

dim g2 as New System.Drawing.Bitmap(orginalimg, imgWidth, imgHeight)

' Sending Response JPEG type to the browser.
Response.ContentType = "image/jpeg"
g2.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)

' Disposing the objects.
orginalimg.Dispose()
g2.Dispose()

End Sub
</script>

Then, I call the images from other files using this:
<img src="thumb.aspx?filename=myimage.jpg&maxheight=100&maxwidth=80" border="0">

Hope this helps,

Elky

garann

10:20 pm on May 14, 2003 (gmt 0)

10+ Year Member



Hi Elky,

That's what I'm trying to avoid, as I can't see a way to reuse that one page to show several thumbnails on the same page. I'd think that the stream served up by thumb.aspx in every image source would be the last source thumb.aspx was asked to load - it needs to show several different thumbnails.

Anyone else know of anything I can use besides Response.OutputStream?

Thanks,
g.

Gibble

7:52 pm on May 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it will show the right thumbnails...or at least it should, if it doesn't...well then I really don't want to switch to .NET

garann

7:47 pm on May 13, 2003 (gmt 0)

10+ Year Member



Elky and Gibble, my apologies. You're both right and the problem with my thumbnails was elsewhere. I'm dumb. Thanks for the help!