2009年3月3日
Silverlight Download Image
我在設計網路相簿時,必須由 server 下載照片,花了不少時間找相關資料,發現下列三種方式可以使用。
1. 利用 Web Service 將 image 以 byte[] 下傳。
2. 利用 BitmapImage.UriSource 方式下載。
3. 利用 WebClient 方式下載。(ps.僅能讀取同一站台內的資料)
範例分別如下:
--- Web Service ---
private void LoadAllPhoto()
{
PhotoServiceReference.PhotoServiceClient proxy = new PhotoManager.PhotoServiceReference.PhotoServiceClient();
proxy.GetPhotoCompleted += new EventHandler<PhotoManager.PhotoServiceReference.GetPhotoCompletedEventArgs>(proxy_GetPhotoCompleted);
proxy.GetPhotoAsync();
}
private Image imageShow;
void proxy_GetPhotoCompleted(object sender, PhotoManager.PhotoServiceReference.GetPhotoCompletedEventArgs e)
{
MemoryStream stream = new MemoryStream();
stream.Write(e.Result, 0, e.Result.Length);
stream.Position = 0;
BitmapImage image = new BitmapImage();
image.SetSource(stream);
Image imageShow = new Image();
imageShow.Source = image;
this.LayoutRoot.Children.Add(imageShow);
}
--- BitmapImage ---
BitmapImage image = new BitmapImage();
image.UriSource = new Uri("...", UriKind.Absolute);
image.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(image_DownloadProgress);
Image image.SetSource(stream);
imageShow = new Image();
imageShow.Source = image;
this.LayoutRoot.Children.Add(imageShow);
--- WebClient ---
WebClient imageLoader = new WebClient();
imageLoader.OpenReadCompleted += new OpenReadCompletedEventHandler(imageLoader_OpenReadCompleted);
imageLoader.OpenReadAsync(new Uri("...", UriKind.Absolute));
void imageLoader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
BitmapImage image = new BitmapImage();
image.SetSource(e.Result);
Image imageShow = new Image();
imageShow.Source = image;
this.LayoutRoot.Children.Add(imageShow);
}
訂閱:
張貼留言 (Atom)
Deploying Vue & .NET with Google OAuth on GCP Cloud Run
Deploying Vue & .NET with Google OAuth on GCP Cloud Run Deploying Vue & .NET with Google OAuth on GCP Cloud Run...
沒有留言:
張貼留言