【问题标题】:Win Server 2003, Images and ASHX issueWin Server 2003,图像和 ASHX 问题
【发布时间】:2012-01-10 22:27:44
【问题描述】:

我的 ASP.Net 应用程序在 IIS6 (Windows Server 2003) 中运行时出现问题,但它在 IIS7 (Windows Server 2008R2) 中运行完美。我正在使用 ASHX 调整图像大小,然后在另一个 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) 处理 Me.Load

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

结束子

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

【问题讨论】:

  • 开启 IIS 6 日志。将其设置为记录所有内容。然后查看记录的请求,看看它在做什么。

标签: asp.net vb.net image ashx


【解决方案1】:

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

【讨论】:

  • 你是对的,即使我在应用程序中设置了 AD 用户帐户,我仍然需要添加 到 web 配置文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
  • 2011-04-13
  • 1970-01-01
  • 1970-01-01
  • 2013-12-21
相关资源
最近更新 更多