[英]How to save a BitmapImage to StorageFolder in C# Windows Phone 8
I'm trying to save a BitmapImage, which I download from a url, in to the app StorageFolder. 我正在尝试将从URL下载的BitmapImage保存到应用程序StorageFolder中。 I tryed to make a function which saves te image for me. 我试图做一个为我保存图像的功能。 This is what I got so far: 这是我到目前为止所得到的:
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;
}
But this isn't working and I don't get any errors back, so i realy don't know whats going wrong here. 但这是行不通的,我也没有收到任何错误,所以我真的不知道这里出了什么问题。 Any help or code samples would be awesom. 任何帮助或代码示例都是令人敬畏的。
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);
}
}
Try this: 尝试这个:
Download an image via HttpWebRequest: 通过HttpWebRequest下载图像:
linkRequest = (HttpWebRequest)WebRequest.Create(uri);
linkRequest.Method = "GET";
WebRequestState webRequestState = new WebRequestState(linkRequest, additionalDataObject);
linkRequest.BeginGetResponse(client_DownloadImageCompleted, webRequestState);
and then: 接着:
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);
}
}
}
}
});
}
And WebRequestState class is: 和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.