Asp.net Fancybox是否可以使用httphandler?
当我不通过httphandler提供图像服务,而只是直接在HTML中添加图像URL时,下面的代码可以工作。Asp.net Fancybox是否可以使用httphandler?,asp.net,fancybox,httphandler,Asp.net,Fancybox,Httphandler,当我不通过httphandler提供图像服务,而只是直接在HTML中添加图像URL时,下面的代码可以工作。 当我通过httphandler加载图像时,控件中的基本图像会正确显示,但当我单击该图像以在lightbox中加载较大的图像时,我会看到置乱代码(wierd字符)请参见此图像: 所以我假设Fancybox需要一个指向图像的直接URL来填充lightbox,但我不确定。 如何确保当用户单击超链接时,fancybox显示pichandler.ashx提供的较大图像 HTML <scrip
当我通过httphandler加载图像时,控件中的基本图像会正确显示,但当我单击该图像以在lightbox中加载较大的图像时,我会看到置乱代码(wierd字符)请参见此图像: 所以我假设Fancybox需要一个指向图像的直接URL来填充lightbox,但我不确定。
如何确保当用户单击超链接时,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>
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
publicssubprocessrequest(ByVal上下文作为HttpContext)实现IHttpHandler.ProcessRequest
作为字节()的Dim数据
Dim i As Integer=CInt(context.Request.QueryString(“i”))
Dim类型为String=context.Request.QueryString(“t”)
Dim fName As String=String.Empty
如果i=0,那么
data=My.Computer.FileSystem.ReadAllBytes(context.Server.MapPath(String.Format(“~/images/noimage_{0}.jpg”,Threading.Thread.CurrentThread.CurrentCulture.TwoLetterIsLanguageName)))
其他的
选择案例类型
案例“freemedia”
使用w作为新的GeneralTableStableApters.freemediaTableAdapter
fName=w.GetDataById(i)(0).medialink.ToString
终端使用
如果fName=“”或fName.Contains(“soundcloud.com”)或fName.Contains(“youtube.com”),则
data=My.Computer.FileSystem.ReadAllBytes(context.Server.MapPath(String.Format(“~/images/noimage_{0}.jpg”,Threading.Thread.CurrentThread.CurrentCulture.TwoLetterIsLanguageName)))
其他的
data=My.Computer.FileSystem.ReadAllBytes(context.Server.MapPath(“~/images/freemedia/thumbs/”&fName))
如果结束
案例“freemediadetails”
Dim searchquery作为新的StringBuilder
searchquery.Append(ConfigurationManager.AppSettings(“freemedia\u solrUrl”))
searchquery.Append(“选择/?缩进=on&facet=true”)
searchquery.Append(“&fq=id:”+i.ToString+”)
searchquery.Append(“&fl=id,medialink,copyrighttext&q=*:*”)
searchquery.Append(“&facet.mincount=1”)不显示没有值的facet
searchquery.Append(searchquery.ToString)
作为HttpWebRequest的Dim请求
作为HttpWebResponse的Dim响应
作为StreamReader的Dim reader
作为弦的暗响应弦
将XPath设置为String=“response/result”
req=HttpWebRequest.Create(searchquery.ToString)
Resp=req.GetResponse()
reader=新的StreamReader(分别为GetResponseStream)
responseString=reader.ReadToEnd()
Dim objXML作为新的xml文档
LoadXml(responseString)
XPath=“响应/结果”
Dim root作为XmlNode=objXML.DocumentElement
Dim nodeList作为XmlNodeList=root.SelectNodes(“后代::文档”)
fName=nodeList(0)。选择SingleNode(“str[@name=”“medialink”“]”)。InnerText
Dim watermarkText为String=nodeList(0)。选择SingleNode(“str[@name=”“copyrighttext”“]”)。InnerText
Dim大小为String=context.Request.QueryString(“s”)
'直接从路径创建图像
作为字符串的dim fpath
如果size=“1”,则
fpath=“~/images/freemedia/largethumbs/”
ElseIf size=“2”则
fpath=“~/images/freemedia/”
如果结束
Dim image=Drawing.image.FromFile(context.Server.MapPath(fpath&fName))
暗字体作为新字体(“Tahoma”,16,FontStyle.Regular,GraphicsUnit.Pixel)
'添加透明水印
暗颜色为color=color.White
将笔刷变暗为新的SolidBrush(颜色)
Dim gr As Graphics=图形。FromImage(图像)
'测量水印文本,以便我们可以偏移其宽度和高度
尺寸水印大小=gr.MeasureString(水印文本,字体)
'创建应打印水印文本的点
将点调整为新点(image.Width-watermarkSize.Width,image.Height-watermarkSize.Height)
gr.DrawString(水印文本、字体、画笔、点)
gr.Dispose()
image.Save(context.Response.OutputStream,ImageFormat.Jpeg)
Case-Else是一个通用对象
使用w作为新的GenoObjectStableApters.GenoObjects_photosTableAdapter
fName=w.GetPhotoById(i)(0).locpath.ToString
终端使用
data=My.Computer.FileSystem.ReadAllBytes(context.Server.MapPath(“~/images/objphotos/thumbs/”&fName))
结束选择
如果结束
context.Response.ContentType=“image/jpeg”
如果键入“freemediadetails”,则
context.Response.BinaryWrite(数据)
如果结束
端接头
我假设您使用的是fancybox v1.3.4,因为您的代码中有API选项,不是吗
如果以这种方式链接到图像:
/pichandler.ashx?i=2&t=freemediadetails&s=2
。。。您的链接href
没有任何图像扩展名(jpg、png、gif)。因此,fancybox不知道要处理的内容类型。您需要将内容的类型
强制为fancybox自定义脚本中的图像
。这是记录在这里的,第6条
然后你可以做:
如果您访问大图像的URL(不使用fancybox查看),图像是否正确显示?是的,当我直接在浏览器中请求/pichandler.ashx时,I=2&t=freemediadetails&s=2,我确实看到了正确的图像。我用fancyb的图片更新了我的帖子
/pichandler.ashx?i=2&t=freemediadetails&s=2
$("a.freemediaimage").fancybox({
// other API options
"type": "image"
});