Win Server 2003,图像和 ASHX 问题

发布于 2024-12-25 22:14:42 字数 6901 浏览 4 评论 0原文

我的 ASP.Net 应用程序在 IIS6 (Windows Server 2003) 中运行时遇到问题,但在 IIS7 (Windows Server 2008R2) 中运行完美。我正在使用 ASSX 来调整图像大小,然后在另一个 winform (DisplayImage.aspx) 上的 ASP:Image 控件中显示结果。当我在 IIS7 中运行该应用程序时,它可以完美地在 ASP:Image 控件中显示图像,但是当我在 IIS6 中运行它时,图像不会显示。我在 IIS6 框中没有收到任何错误,只是众所周知的带有红色 X 的框。 这是我的代码:

这是来自 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

这是来自 DisplayImage.aspx 的 HTML/ASP.Net 代码

<%@ 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>

这是来自 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

解决

方案 问题是网站在正确的环境下运行但我必须将以下代码行添加到我的 Web 配置文件中,以便我的应用程序能够模拟有权访问这些文件的用户:

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2025-01-01 22:14:42

我认为这是一个权限问题。您需要授予网络服务访问包含图像的文件夹的权限。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文