1

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();
        }
}

別のクラスでは、画像を読み取り、画像ソースにバインドします。

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内のlistBoxにあります

私を助けてください....それは私を夢中にさせます

4

1 に答える 1

1

あなたのコードを徹底的に観察した後、私はいくつかのことを得ました:

SaveImage クラスで、次のようにimageUri = new Uri(uri, UriKind.Relative); 変更します。UriKind.Absolute

そして、クラスを使用する代わりにwebClient_ImageOpenReadCompleted直接設定できますbitmap.SetSource(e.Result);StreamResourceInfo

繰り返しになりますが、Image タグの Source プロパティを XAML で BitmapImage にバインドすることが有効かどうかはわかりません (コード ビハインドで有効です)。

于 2012-06-20T13:35:00.917 に答える