摘要:[Silverlight][VB]跨網域取得圖片
這是昨天的那篇[Silverlight][VB]使用程式碼設定物件圖片的補充...
情境四:載入不同網域的圖片
方法1:設定clientaccesspolicy.xml,這個網路找一堆,不作重複說明了。
方法2:主要是因為方法1的限制條件,讓我們無法事事盡如人意,就有方法2囉~
概述一下..其實這個網路上也有許多方法,大部份就是要透過第三方黑暗的力量來完成,另外看到比較難的是用jquery來作(好難哦~),而我是採用,自行撰寫web service,透過service抓取圖片,SL程式再透過此serviec取得圖片,就醬樣~
Step1.在web專案中加入GetImage.ashx
<%@ WebHandler Language="VB" Class="GetImage" %>
Imports System
Imports System.Web
Imports System.Net
Imports System.IO
Public Class GetImage : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
If Not String.IsNullOrEmpty(HttpContext.Current.Request.QueryString("Path")) Then
Dim path As String = HttpContext.Current.Request.QueryString("Path")
Dim wb As New WebClient()
Dim imgbyte As Byte() = wb.DownloadData(path)
context.Response.Clear()
context.Response.BufferOutput = True
context.Response.ContentType = "Image/JPG"
context.Response.BinaryWrite(imgbyte)
End If
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Step2.就像下面這樣用啦~
Dim wb As New WebClient()
wb.OpenReadAsync(New Uri("..\GetImage.ashx?Path=http://www.google.com.tw/intl/en_com/images/srpr/logo1w.png", UriKind.Relative))
Path參數就是設定路徑用的,希望不用我解釋您就能懂了...
另外使用QueryString
有個限制,字串長度好像不能超過256的樣子,所以請自行注意囉!
Question:WebClient抓下來後,我怎麼設定圖片?