Silverlight下載網狀圖片

來源:互聯網
上載者:User

話說Silverlight做下載的時候是一個很麻煩的事情,剛開始的時候使用WebClient去下載圖片,這樣只能下載本域下的圖片資源,不能跨域。

比如http://www.baidu.com/a.jpg這樣的話就會出現安全問題,找了好多資料,都說要搞配置XML什麼的,結果試了也不可以,話說也不怎麼懂

然後我就想到一個辦法,在WebService裡面使用WebClient下載圖片到本地,然後在Silverlight裡面使用FileInfo(“C:\\a.jpg”)這樣去找,

可惡的是Silverlight不允許這樣擷取本地檔案。然後就測底無語了。。。

 

然後使用

WebRequest request = WebRequest.Create(url);
 WebResponse response = request.GetResponse();

Stream s=response.GetResponseStream();

這裡又報S.leng的異常

 

 

最後東找西找,終於找到瞭解決這個異常的問題

下面是代碼

Webservice裡面的方法 (例如url=http://baidu.com/a.jpg)

[WebMethod]        //根據網路地址,下載圖片        public byte[] DownLoadImage(string url)        {            WebRequest request = WebRequest.Create(url);            WebResponse response = request.GetResponse();            BinaryReader rsReader = new BinaryReader(response.GetResponseStream());            byte[] buff = rsReader.ReadBytes((int)response.ContentLength);            rsReader.Read(buff, 0, buff.Length);            rsReader.Close();            response.Close();            return buff;        }

 

然後在Silverlight裡面調用

MasterDataServiceSoapClient service = new      MasterDataServiceSoapClient();
                service.DownLoadImageAsync(this.txbaddress.Text);                service.DownLoadImageCompleted += new EventHandler<DownLoadImageCompletedEventArgs>(service_DownLoadImageCompleted);

void service_DownLoadImageCompleted(object sender, DownLoadImageCompletedEventArgs e)        {            if (e.Result.Length==0)            {                this.txbMessage.Text = "網路故障,下載失敗!";                this.txbaddress.IsEnabled = true;            }            else {                //FileInfo mapInfo = new FileInfo(e.Result);                //string[] mapFullName = mapInfo.Name.Split('.');                //if (!(mapFullName[mapFullName.Length - 1].Contains("jpg")))                //{                //    MsgBox.Show("圖片不是jpg格式!", "訊息提示", MsgBoxButton.OKCancel, MsgInfo.Error);                //    return;                //}                //FileStream inputStream = mapInfo.OpenRead();                //int readSize = 204800;                //if (inputStream.Length > readSize)                //{                //    MsgBox.Show("圖片大小不能超過200KB!", "訊息提示", MsgBoxButton.OKCancel, MsgInfo.Error);                //    return;                //}                //byte[] imageb = new byte[inputStream.Length];                //inputStream.Read(imageb, 0, imageb.Length);                //inputStream.Close();                this.FrmCustomerManager.upload_UploadComplete(e.Result);                this.DialogResult = false;            }

 

void service_DownLoadImageCompleted(object sender, DownLoadImageCompletedEventArgs e)        {            if (e.Result.Length==0)            {                this.txbMessage.Text = "網路故障,下載失敗!";                this.txbaddress.IsEnabled = true;            }            else {                //FileInfo mapInfo = new FileInfo(e.Result);                //string[] mapFullName = mapInfo.Name.Split('.');                //if (!(mapFullName[mapFullName.Length - 1].Contains("jpg")))                //{                //    MsgBox.Show("圖片不是jpg格式!", "訊息提示", MsgBoxButton.OKCancel, MsgInfo.Error);                //    return;                //}                //FileStream inputStream = mapInfo.OpenRead();                //int readSize = 204800;                //if (inputStream.Length > readSize)                //{                //    MsgBox.Show("圖片大小不能超過200KB!", "訊息提示", MsgBoxButton.OKCancel, MsgInfo.Error);                //    return;                //}                //byte[] imageb = new byte[inputStream.Length];                //inputStream.Read(imageb, 0, imageb.Length);                //inputStream.Close();                this.FrmCustomerManager.upload_UploadComplete(e.Result);                this.DialogResult = false;            }

 

 

e.result返回的是一個位元組數組byte[]..

uploadcomplete這裡不用管,裡面我寫的是把位元組數組e.result轉換成了一個burshimage 也就是一個背景圖片類型給了一個Stackpanel的bg

 

下面是如何把位元組數群組轉換城圖片的代碼

public ImageBrush ConverterImageBrush(byte[] Image)        {            //MemoryStream inputStream = new MemoryStream(Image, true);            //inputStream.Write(Image, 0, Image.Length);            System.IO.Stream inputStream = new System.IO.MemoryStream(Image);            System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();            image.SetSource(inputStream);            return new ImageBrush() { ImageSource = image, Stretch = Stretch.Uniform };        }

 

好了 就這麼多

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.