0

当我不通过 h​​ttphandler 提供图像而直接在 HTML 中添加图像 URL 时,下面的代码有效。
当我通过 httphandler 加载图像时,控件中的基本图像正确显示,但是当我单击该图像以将较大的图像加载到灯箱中时,我看到了乱码(奇怪的字符)看到这个图像:

在此处输入图像描述

所以我假设 Fancybox 需要一个图像的直接 URL 来填充灯箱,但我不确定。
如何确保当用户单击超链接时,fancybox 显示由 pichandler.ashx 提供的较大图像?

HTML

<script type="text/javascript" language="javascript">

$(document).ready(function () {
    $(".fancylink").click(function () {
        var cuRRent = $("a.advance-link img").attr("src");
        cuRRent = cuRRent.replace('largethumbs/', '');
        $.fancybox({
            'href': cuRRent,
            // other API options etc 
            'overlayColor': '#000',
            'opacity': true,
            'transitionIn': 'elastic',
            'transitionOut': 'none'
        }); //fancybox 
    }); //click 

    $("a.freemediaimage").fancybox({
        'transitionIn': 'elastic',
        'overlayColor': '#000',
        'opacity': true,
        'transitionOut': 'elastic',
        'speedIn': 600,
        'speedOut': 200,
        'overlayShow': true
    });

}); // ready 


</script>



<asp:HyperLink ID="hlMediaImage" CssClass="freemediaimage" runat="server">    
    <asp:Image ID="MediaImage" runat="server" />
</asp:HyperLink>

代码隐藏

hlMediaImage.NavigateUrl = "~/pichandler.ashx?i=2&t=freemediadetails&s=2"  
MediaImage.ImageUrl = "~/pichandler.ashx?i=2&t=freemediadetails&s=1"

pichandler.ashx

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Dim data As Byte()


    Dim i As Integer = CInt(context.Request.QueryString("i"))

    Dim type As String = context.Request.QueryString("t")

    Dim fName As String = String.Empty

    If i = 0 Then
        data = My.Computer.FileSystem.ReadAllBytes(context.Server.MapPath(String.Format("~/images/noimage_{0}.jpg", Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName)))
    Else


        Select Case type
            Case "freemedia"
                Using w As New generaltablesTableAdapters.freemediaTableAdapter
                    fName = w.GetDataById(i)(0).medialink.ToString                                               
                End Using
                If fName = "" Or fName.Contains("soundcloud.com") Or fName.Contains("youtube.com") Then
                    data = My.Computer.FileSystem.ReadAllBytes(context.Server.MapPath(String.Format("~/images/noimage_{0}.jpg", Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName)))
                Else
                    data = My.Computer.FileSystem.ReadAllBytes(context.Server.MapPath("~/images/freemedia/thumbs/" & fName))
                End If
            Case "freemediadetails"

                Dim searchquery As New StringBuilder
                searchquery.Append(ConfigurationManager.AppSettings("freemedia_solrUrl"))
                searchquery.Append("select/?indent=on&facet=true")
                searchquery.Append("&fq=id:""" + i.ToString + """")
                searchquery.Append("&fl=id,medialink,copyrighttext&q=*:*")
                searchquery.Append("&facet.mincount=1") 'dont show facets which have no value
                searchquery.Append(searchquery.ToString)

                Dim req As HttpWebRequest
                Dim Resp As HttpWebResponse
                Dim reader As StreamReader
                Dim responseString As String
                Dim XPath As String = "response/result"

                req = HttpWebRequest.Create(searchquery.ToString)
                Resp = req.GetResponse()
                reader = New StreamReader(Resp.GetResponseStream)
                responseString = reader.ReadToEnd()

                Dim objXML As New XmlDocument

                objXML.LoadXml(responseString)
                XPath = "response/result"

                Dim root As XmlNode = objXML.DocumentElement
                Dim nodeList As XmlNodeList = root.SelectNodes("descendant::doc")

                fName = nodeList(0).SelectSingleNode("str[@name=""medialink""]").InnerText
                Dim watermarkText As String = nodeList(0).SelectSingleNode("str[@name=""copyrighttext""]").InnerText

                Dim size As String = context.Request.QueryString("s")

                ' Create image directly from the path

                dim fpath as string
                If size = "1" Then
                    fpath = "~/images/freemedia/largethumbs/"
                ElseIf size = "2" Then
                    fpath = "~/images/freemedia/"
                End If

                Dim image = Drawing.Image.FromFile(context.Server.MapPath(fpath & fName))
                Dim font As New Font("Tahoma", 16, FontStyle.Regular, GraphicsUnit.Pixel)

                'Adds a transparent watermark 
                Dim color As Color = color.White 
                Dim brush As New SolidBrush(color)
                Dim gr As Graphics = Graphics.FromImage(image)

                ' Measure the watermark text so we can offset it's width and height
                Dim watermarkSize = gr.MeasureString(watermarkText, font)

                ' Create the point where the watermark text should be printed
                Dim point As New Point(image.Width - watermarkSize.Width, image.Height - watermarkSize.Height)

                gr.DrawString(watermarkText, font, brush, point)
                gr.Dispose()

                image.Save(context.Response.OutputStream, ImageFormat.Jpeg)


            Case Else 'its a general object
                Using w As New genobjectsTableAdapters.genobjects_photosTableAdapter
                    fName = w.GetPhotoById(i)(0).locpath.ToString
                End Using
                data = My.Computer.FileSystem.ReadAllBytes(context.Server.MapPath("~/images/objphotos/thumbs/" & fName))


        End Select
    End If


    context.Response.ContentType = "image/jpeg"

    If type <> "freemediadetails" Then
        context.Response.BinaryWrite(data)
    End If
End Sub
4

1 回答 1

2

我假设您使用的是 fancybox v1.3.4,因为您的代码中有 API 选项,不是吗?

如果您以这种方式链接到图像:

/pichandler.ashx?i=2&t=freemediadetails&s=2

...您的链接href没有任何图像扩展名(jpg、png、gif)。因此,fancybox 不知道type要处理什么内容。您需要强制将type内容添加到image您的 fancybox 自定义脚本中。这是记录在这里http://fancybox.net/faq , No. 6

然后你可以这样做:

$("a.freemediaimage").fancybox({
  // other API options
  "type": "image"
});
于 2012-12-13T19:33:10.157 回答