从网站单击图像后,我想在任何容器中捕获并保存图像的 URL,然后将图像保存在隔离存储中。
有没有人可以为此提供代码?
谢谢
要下载图像,您需要获取 URI。一旦你得到它,它非常简单:
public class ServiceImageDownloader
{
private readonly BitmapImage _downloadedBmpImage = new BitmapImage();
public ServiceImageDownloader()
{
_downloadedBmpImage.ImageOpened += DownloadedBmpImageImageOpened;
_downloadedBmpImage.ImageFailed += DownloadedBmpImageImageFailed;
_downloadedBmpImage.CreateOptions = BitmapCreateOptions.BackgroundCreation;
}
public void DownloadImage(string imageUri)
{
_downloadedBmpImage.UriSource = imageUri;
}
private void DownloadedBmpImageImageOpened(object sender, RoutedEventArgs e)
{
try
{
OnImageDownloaded(new WriteableBitmap(_downloadedBmpImage));
}
catch
{
//manage other classic errors
}
}
private void OnImageDownloaded(WriteableBitmap wbImTemp)
{
//here you get your image as a writeableBmp and you can do whatever you wish,
//as save it in your IS in your example
}
}
现在对于 IS,我建议您将您的图像用作流或可序列化的对象。它可能是这样的:
public bool WriteImage(string imageName, Stream streamImage)
{
IsolatedStorageFile store = null;
Stream stream = null;
try
{
using (store = IsolatedStorageFile.GetUserStoreForSite())
{
// Open/Create the file for writing
stream = new IsolatedStorageFileStream(imageName,
FileMode.Create, FileAccess.Write, store);
streamImage.CopyTo(stream);
stream.Close();
streamImage.Close();
}
return true;
}
catch (Exception ie)
{
//manage error the way you prefer (think especially to quota management)
}
}
希望能帮助到你。