[英]How to save a BitmapImage to StorageFolder in C# Windows Phone 8
我正在嘗試將從URL下載的BitmapImage保存到應用程序StorageFolder中。 我試圖做一個為我保存圖像的功能。 這是我到目前為止所得到的:
public async Task<string> savePhotoLocal(BitmapImage photo, string photoName)
{
var profilePictures = await storageRoot.CreateFolderAsync("profilePictures", CreationCollisionOption.OpenIfExists);
var profilePicture = await profilePictures.CreateFileAsync(photoName+".jpg", CreationCollisionOption.ReplaceExisting);
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
using (Stream outputStream = await profilePicture.OpenStreamForWriteAsync())
{
await stream.CopyToAsync(outputStream);
}
}
return profilePicture.Path;
}
但這是行不通的,我也沒有收到任何錯誤,所以我真的不知道這里出了什么問題。 任何幫助或代碼示例都是令人敬畏的。
public async Task<string> savePhotoLocal(BitmapImage photo, string photoName)
{
string folderName ="profilePictures";
var imageName =photoName+".jpg";
Stream outputStream = await profilePictures.OpenStreamForWriteAsync();
if(outputStream!=null)
{
this.SaveImages(outputStream,folderName,imageName );
}
return imageName ;
}
private void SaveImages(Stream data, string directoryName, string imageName)
{
IsolatedStorageFile StoreForApplication =IsolatedStorageFile.GetUserStoreForApplication();
try
{
using (MemoryStream memoryStream = new MemoryStream())
{
data.CopyTo(memoryStream);
memoryStream.Position = 0;
byte[] buffer = null;
if (memoryStream != null && memoryStream.Length > 0)
{
BinaryReader binaryReader = new BinaryReader(memoryStream);
buffer = binaryReader.ReadBytes((int)memoryStream.Length);
Stream stream = new MemoryStream();
stream.Write(buffer, 0, buffer.Length);
stream.Seek(0, SeekOrigin.Begin);
string FilePath = System.IO.Path.Combine(directoryName, imageName);
IsolatedStorageFileStream isoFileStream = new IsolatedStorageFileStream(FilePath, FileMode.Create, StoreForApplication);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
BitmapImage bitmapImage = new BitmapImage { CreateOptions = BitmapCreateOptions.None };
bitmapImage.SetSource(stream);
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage);
writeableBitmap.SaveJpeg(isoFileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100);
});
}
}
}
catch (Exception ex)
{
//ExceptionHelper.WriteLog(ex);
}
}
嘗試這個:
通過HttpWebRequest下載圖像:
linkRequest = (HttpWebRequest)WebRequest.Create(uri);
linkRequest.Method = "GET";
WebRequestState webRequestState = new WebRequestState(linkRequest, additionalDataObject);
linkRequest.BeginGetResponse(client_DownloadImageCompleted, webRequestState);
接着:
private void client_DownloadImageCompleted(IAsyncResult asynchronousResult)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
WebRequestState webRequestState = asynchronousResult.AsyncState as WebRequestState;
AdditionalDataObject file = webRequestState._object;
using (HttpWebResponse response = (HttpWebResponse)webRequestState.Request.EndGetResponse(asynchronousResult))
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (Stream stream = response.GetResponseStream())
{
BitmapImage b = new BitmapImage();
b.SetSource(stream);
WriteableBitmap wb = new WriteableBitmap(b);
using (var isoFileStream = isoStore.CreateFile(yourImageFolder + file.Name))
{
var width = wb.PixelWidth;
var height = wb.PixelHeight;
System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
}
}
}
}
});
}
和WebRequestState類是:
public class WebRequestState
{
public HttpWebRequest Request { get; set; }
public object _object { get; set; }
public WebRequestState(HttpWebRequest webRequest, object obj)
{
Request = webRequest;
_object = obj;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.