Search code examples
asp.netvb.netimageashx

Win Server 2003, Images and ASHX issue


I am having an issue with my ASP.Net application when it runs in IIS6 (Windows Server 2003) but it runs flawlessly in IIS7 (Windows Server 2008R2). I am using an ASHX to resize images and then display the results in an ASP:Image control on another winform (DisplayImage.aspx). When I run the application in IIS7 it displays the image in the ASP:Image control perfectly but when I run it in IIS6 the image is not displayed. I do not receive any errors on the IIS6 box just the proverbial box with a red X. Here is my code:

Here is the code from Display image aspx WinForm:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then
    ''Code to retrieve filename here
    With Image1
        .ImageUrl = "~/ImageHandler.ashx?imageName=" & filePath 
    End With
End If

End Sub

Here is the HTML/ASP.Net code from DisplayImage.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DisplayImage.aspx.vb" Inherits="Mobile.DisplayImage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head id="Head1" runat="server">
        <meta content="index,follow" name="robots" />
        <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type" />
        <meta content="minimum-scale=1.0, width=device-width, maximum-scale=0.6667, user-scalable=no" name="viewport" />

        <meta name="HandheldFriendly" content="true" />
        <meta name="viewport" content="width=device-width" />

        <link href="../css/style.css" rel="stylesheet" type="text/css" />

        <script src="../scripts/functions.js" type="text/javascript"></script>

        <title>CrimeNtel</title>
    </head>
    <body>
        <form id="theform" runat="server">
            <div id="topbar">
                <div id="leftnav">
                    <a href="Default.aspx"><img alt="home" src="../images/home.png" /></a><a id="goback" runat="server">Return</a>
                </div>
                <div>
                    Image Viewer
                </div>
            </div>
            <div>
                <fieldset>
                    <span>
                        <div id="titleHeader" runat="server" style="text-align:center">Linked Image</div>
                    </span>

                    <div style="text-align:center" style="text-align:center">
                        <asp:Image ID="Image1" runat="server" />
                    </div>
                </fieldset>
            </div>
        </form> 
    </body>
</html>

Here is the code behind from the ImageHandler.ashx:

Imports System.Web
Imports System.Web.Services
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Web.Mobile
Imports System.IO

Public Class ImageHandler
    Implements System.Web.IHttpHandler


    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim fileName As String = HttpContext.Current.Request("imagename")
        Dim screenHeight As Integer = 300
        Dim screenWidth As Integer  = 300
        Dim fileExtension As String = Path.GetExtension(fileName).Replace(".", "").ToUpper

        Select Case fileExtension.ToUpper
            Case "JPG"
                context.Response.ContentType = "image/jpg"
            Case "PNG"
                context.Response.ContentType = "image/png"
            Case "BMP"
                context.Response.ContentType = "image/bmp"
            Case "GIF"
                context.Response.ContentType = "image/GIF"
            Case Else
                context.Response.ContentType = "image/jpg"
        End Select

    ''Loads Virtual Directory  from Web Config File
       ''Note Virtual Directory is located in the Root of the Default Web Site
        Dim _virtualDirectory As String = ConfigurationManager.AppSettings("VirtualDirectory")

        Try
        Dim bmp As Bitmap = New Bitmap((_virtualDirectory & fileName.Trim)

                Dim img As System.Drawing.Image = FixedSize(bmp, screenWidth, screenHeight)
                img.Save(_virtualDirectory & "/Thumbs/displayedImage." & fileExtension)
                img.Dispose()
                context.Response.WriteFile(_virtualDirectory & "/Thumbs/displayedImage." & fileExtension)

        Catch ex As Exception
            'context.Response.Write("<script>alert('" & ex.Message & "');</script>")
        End Try
    End Sub

    Private Shared Function FixedSize(imgPhoto As Image, Width As Integer, Height As Integer) As Image
        ''Found code at 
        ''http://www.codeproject.com/KB/GDI-plus/imageresize.aspx

        Dim sourceWidth As Integer = imgPhoto.Width
        Dim sourceHeight As Integer = imgPhoto.Height
        Dim sourceX As Integer = 0
        Dim sourceY As Integer = 0
        Dim destX As Integer = 0
        Dim destY As Integer = 0

        Dim nPercent As Single = 0
        Dim nPercentW As Single = 0
        Dim nPercentH As Single = 0

        nPercentW = (CSng(Width) / CSng(sourceWidth))
        nPercentH = (CSng(Height) / CSng(sourceHeight))

        If nPercentH < nPercentW Then
            nPercent = nPercentH
            destX = System.Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2)
        Else
            nPercent = nPercentW
            destY = System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2)
        End If

        Dim destWidth As Integer = CInt(Math.Truncate(sourceWidth * nPercent))
        Dim destHeight As Integer = CInt(Math.Truncate(sourceHeight * nPercent))

        Dim bmPhoto As New Bitmap(Width, Height, PixelFormat.Format24bppRgb)
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)

        Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
        grPhoto.Clear(System.Drawing.ColorTranslator.FromHtml("#C5CCD4"))  'Color.DarkGray) ' Color.White)
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic

        grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)

        grPhoto.Dispose()
        Return bmPhoto
    End Function

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class

Solution

What was the issue is that the website was running under the correct creditials but I had to add the following line of code to my Web Config File so that my app would impersonate the user who had access to the files:

<system.web>
    <!--This will allow website to run under a different account than that of ASP.Net
      <identity impersonate="true" userName="DomainName\UserName" password="p@5sW0Rd"/>
</system.web>

Solution

  • I think it is a permission problem. You need to grant network services access to the folder containing the image.