У меня проблема с моим приложением ASP.Net, когда оно работает в IIS6 (Windows Server 2003), но оно безупречно работает в IIS7 (Windows Server 2008R2). Я использую ASHX для изменения размера изображений, а затем отображать результаты в ASP: Управление изображением на другой winform (DisplayImage.aspx). Когда я запускаю приложение в IIS7, оно прекрасно отображает изображение в ASP: Управление изображением, но когда я запускаю его в IIS6, изображение не отображается. Я не получаю никаких ошибок в окне IIS6, только в пословице с красным крестиком. Вот мой код:
Вот код из отображения изображения aspx WinForm:
Protected Sub Page_Load (ByVal отправитель как объект, ByVal e как System.EventArgs) Обрабатывает Me.Load
If Not IsPostBack Then
''Code to retrieve filename here
With Image1
.ImageUrl = "~/ImageHandler.ashx?imageName=" & filePath
End With
End If
End Sub
Вот код HTML / ASP.Net из 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>
Вот код из 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
Решение
Проблема была в том, что веб-сайт работал с правильными кредитными картами, но мне пришлось добавить следующую строку кода в мой файл веб-конфигурации, чтобы мое приложение имитировало пользователя, который имел доступ к файлам:
<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>