.net – 如何通过webclient下载图像(jpg)并保存到Windows Phone 7上的独立存储?
|
由于我缺乏编程经验(3个月),我无法重新创建任何上述问题的例子.我发现的例子涉及非WP7 Silverlight,基于摄像头的图像保存,已根据我的需要复杂或者没有工作.我已经能够使用Webclient的实例下载文本文件,并使用StreamWriter将其保存到独立存储中.我需要用jpg图像完成相同的任务.以下是我用来下载文本文件的内容. ================================================== ============================= IsolatedStorageFile MyStore = IsolatedStorageFile.GetUserStoreForApplication();
private void GetTextFile()
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://mywebsite.com/textfile.txt"));
}
private void client_DownloadStringCompleted(object sender,DownloadStringCompletedEventArgs e)
{
StreamWriter MyStreamWriter = new StreamWriter(new IsolatedStorageFileStream("textfile.txt",FileMode.Create,MyStore));
MyStreamWriter.WriteLine(e.result)
MyStreamWriter.Close();
}
================================================== ============================= 我已经删除了一些用于处理错误等的行,以使其尽可能简单. 请有人修改以上内容以便我下载并保存jpg吗? 请保持尽可能简单,因为我很容易混淆. 感谢您提前的时间! 解决了!================================================ =============================== 我设法使用以下网站提供的信息. 希望这将有助于未来其他受挫的新手! IsolatedStorageFile MyStore = IsolatedStorageFile.GetUserStoreForApplication();
private void GetImageFile()
{
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri("http://mywebsite.com/1.jpg"),client);
}
void client_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e)
{
var resInfo = new StreamResourceInfo(e.Result,null);
var reader = new StreamReader(resInfo.Stream);
byte[] contents;
using (BinaryReader bReader = new BinaryReader(reader.BaseStream))
{
contents = bReader.ReadBytes((int)reader.BaseStream.Length);
}
IsolatedStorageFileStream stream = MyStore.CreateFile("10.jpg");
stream.Write(contents,contents.Length);
stream.Close();
}
试试这个,也许对你很有帮助,private void PhoneApplicationPage_Loaded(object sender,RoutedEventArgs e)
{
HttpWebRequest reqest1 = (HttpWebRequest)WebRequest.Create(url);
reqest1.BeginGetResponse(DownloadImageCallback,reqest1);
Thread.Sleep(1000);
}
void DownloadImageCallback(IAsyncResult result)
{
HttpWebRequest req1 = (HttpWebRequest)result.AsyncState;
HttpWebResponse responce = (HttpWebResponse)req1.EndGetResponse(result);
Stream s = responce.GetResponseStream();
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
string directory = "Images";
if (!myStore.DirectoryExists(directory))
{
myStore.CreateDirectory(directory);
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isoFileStream = myIsolatedStorage.CreateFile(directory + "//yourfilename.jpg"))
{
var wb = new WriteableBitmap(bitimage);
var width = wb.PixelWidth;
var height = wb.PixelHeight;
Extensions.SaveJpeg(wb,isoFileStream,width,height,100);
}
}
}
else
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(directory + "//yourfilename.jpg"))
{
myIsolatedStorage.DeleteFile(directory + "//yourfilename.jpg");
}
using (var isoFileStream = myIsolatedStorage.CreateFile(directory + "//yourfilename.jpg"))
{
var wb = new WriteableBitmap(bitimage);
var width = wb.PixelWidth;
var height = wb.PixelHeight;
Extensions.SaveJpeg(wb,100);
}
}
}
});
} (编辑:4S站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 如何正确使用MSBuild社区任务库在.NET dll上设置SVN版本号
- .net – Windows Azure Web角色缓存(预览)“挂起”使模拟器
- winforms – 在单元测试方法中显示Windows窗体
- .net – 在Windows应用程序中格式化标签内的文本
- Windows Server 2016-Win Ser 2016已删减内容
- windows – 通过PowerShell运行.cmd文件
- wpf – xaml Scrollviewer – 禁用整个窗口的过度滚动/橡皮
- 如何正确创建一个nim / nimrod windows dll
- .net – 可以按需下载先决条件的好安装程序
- Windows调试工具入门 — windebug
- Microsoft Dynamics(Navision)vs C#.NET
- Windows – 无法从GitBash运行TASKKILL
- ms-office – Microsoft Office 2010功能区自定义
- Windows下wamp php单元测试工具PHPUnit安装及生成
- windows-phone-8.1 – 如何检测在Windows 10 Mob
- EvoPDF库在Windows Azure托管站点上失败
- window下强制杀死某个进程用taskkill /pid 进程号
- https – 如何在非Windows平台上忽略“System.Ne
- windows – dokan sshfs有多稳定?
- Vulkan Tutorial 07 Window surface
