2012-06-20 78 views
1

我正在将图像从URL链接保存到独立存储,稍后我需要将它绑定到图像。从URL中读取图像将其保存在独立存储中,然后读取并绑定到图像源

这是从哪里获得并保存图像的代码:
(路径值并且该值是一个类属性)

private void saveImage(string name) 
{ 
     path = name; 

     string uri = "http://sherutnetphpapi.cloudapp.net/mini_logos/" + path; 
     WebClient m_webClient = new WebClient(); 
     imageUri = new Uri(uri, UriKind.Relative); 
     m_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_ImageOpenReadCompleted); 
     m_webClient.OpenReadAsync(imageUri); 
} 

void webClient_ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
{ 
     using (IsolatedStorageFile myIsf = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      if (myIsf.FileExists(path)) 
      { 
       myIsf.DeleteFile(path); 
      } 

      IsolatedStorageFileStream fileStream = myIsf.CreateFile(path); 

      StreamResourceInfo sri = null; 
      sri = Application.GetResourceStream(imageUri); 

      BitmapImage bitmap = new BitmapImage(); 
      bitmap.SetSource(sri.Stream); 
      WriteableBitmap wb = new WriteableBitmap(bitmap); 

      // Encode WriteableBitmap object to a JPEG stream. 
      System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); 
      wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); 
      fileStream.Close(); 
     } 
} 

在另一个I类读出的图像,并将其结合到图像源:

public class CompanyItem 
{ 
    public String companyIcon { get; set; } //save the file name in the isolated storge 
    public String companyName { get; set; } 

    [IgnoreDataMember] 
    public BitmapImage ReadImageFromStorage 
    { 
     get 
     { 
      BitmapImage image = new BitmapImage(); 
      IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication(); 
      string isoFileName = this.companyIcon; 
      var stream = isoStore.OpenFile(isoFileName, System.IO.FileMode.Open); 
      image.SetSource(stream); 
      return image; 
     } 
    } 

XAML中的代码是:

<Image Height="63" HorizontalAlignment="Left" Margin="392,7,0,0" Name="imgConpamyIcon" Stretch="Fill" VerticalAlignment="Top" Width="70" CacheMode="BitmapCache" Source="{Binding ReadImageFromStorage}" /> 

图像是在ListBox

请帮我内一个列表框....这让我发疯

+0

你在这个问题中有什么问题?哪里出问题了? – nkchandra

+0

当我运行程序时,我得到异常:“价值不在预期的范围内”在类的CompanyItem行image.SetSource(流); – tovash

+0

当程序试图获取并保存图像时,我得到以下消息:“由于'uriString'参数代表绝对URI,因此无法创建相对URI。 – tovash

回答

1

后彻底ovserving你的代码,我有几件事情:

在你SaveImage类,imageUri = new Uri(uri, UriKind.Relative); 将其更改为UriKind.Absolute

,然后在webClient_ImageOpenReadCompleted,你可以直接设置bitmap.SetSource(e.Result);而不是使用StreamResourceInfo

再次,我dnt知道是否有效将图像标记的Source属性绑定到XAML中的BitmapImage(它在代码后面有效)

+0

我做了你的建议。它真的改变了一些东西。注释:“不能创建相对URI,因为'uriString'参数表示绝对URI”不出现。但现在我得到异常“WebException是未处理 - 远程服务器返回一个错误:不Fount”的行:bitmap.SetSource(e.Result);你有什么建议,为什么发生? – tovash

+0

我现在就试试它,程序甚至没有进入函数'webClient_ImageOpenReadCompleted'。事实上,实际发生的事情是我创建了一个代表公司的对象。它的一个属性就是图标(公司标志)。在开始时,我从网络上读取所有数据,包括图像 - 图标。因此,函数saveImage'对所有公司运行n次,并且仅在函数'webClient_ImageOpenReadCompleted'运行n次之后运行。我需要使用等待功能吗? – tovash

+0

在这种情况下,问题主要发生在'path'。因为当第i个webClient_ImageOpenReadCompleted实例被调用时,实际路径值已经被更改。让我看看是否有其他选择 – nkchandra

相关问题