0

我可以在我的 xaml 中像这样声明我的图像

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanelx" Grid.Row="1" Margin="0,0,0,0">
    <Image x:Name="MyImage" Height="150" HorizontalAlignment="Left" Margin="141,190,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" />
</Grid>

我可以像这样通过 .xaml.cs 从隔离存储中加载我的图像

void loadImage()
{
    // The image will be read from isolated storage into the following byte array

    byte[] data;

    // Read the entire image in one go into a byte array

    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream isfs = isf.OpenFile("0.jpg", FileMode.Open, FileAccess.Read))
        {
            data = new byte[isfs.Length];
            isfs.Read(data, 0, data.Length);
            isfs.Close();
        }
    }

    MemoryStream ms = new MemoryStream(data);
    BitmapImage bi = new BitmapImage();
    bi.SetSource(ms);

    Image image = new Image();
    image.Height = bi.PixelHeight;
    image.Width = bi.PixelWidth;

    image.Source = bi;    
}

当我键入 MyImage。我找不到将其设置为我刚刚创建的图像的方法。请问有大佬可以给点建议吗?

4

1 回答 1

6
void loadImage() { // The image will be read from isolated storage into the following byte array

        byte[] data;

        // Read the entire image in one go into a byte array

        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream isfs = isf.OpenFile("0.jpg", FileMode.Open, FileAccess.Read))
            {
                data = new byte[isfs.Length];
                isfs.Read(data, 0, data.Length);
                isfs.Close();
            }
        }

        MemoryStream ms = new MemoryStream(data);
        BitmapImage bi = new BitmapImage();
        bi.SetSource(ms);

        MyImage.Source = bi;    
    }
}

你需要设置MyImage.Source = bi;. 就是这样

还有一点重构:

void loadImage() { 
        BitmapImage bi = new BitmapImage();

        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream isfs = isf.OpenFile("0.jpg", FileMode.Open, FileAccess.Read))
            {
                bi.SetSource(isfs);
            }
        }

        MyImage.Source = bi;    
    }
}
于 2012-04-26T12:54:24.007 回答