方法一:
本地圖片庫(kù)選擇圖片進(jìn)行顯示:
async
private
void
LoadPictureByPicker()
{
FileOpenPicker
fileOpen =
new
FileOpenPicker()
{
FileTypeFilter={
".jpg"
,
".jpeg"
,
".png"
,
".bmp"
},
ViewMode=PickerViewMode.Thumbnail,
SuggestedStartLocation=PickerLocationId.PicturesLibrary
};
bitmapImage
=
new
BitmapImage();
storageFile
= await fileOpen.PickSingleFileAsync();
if
(storageFile
!=
null
)
{
using
(IRandomAccessStream
stream = await
storageFile.OpenAsync(FileAccessMode.Read))
{
bitmapImage.SetSource(stream);
}
}
this.image.Source
= bitmapImage;
}
方法二:
請(qǐng)求網(wǎng)絡(luò)URL:
async
private
void
LoadPictrueByUrl()
{
var
rass =
RandomAccessStreamReference.CreateFromUri(
new
Uri(url));
var
streamRandom
= await rass.OpenReadAsync();
BitmapImage
bitmapImage =
new
BitmapImage();
bitmapImage.SetSource(streamRandom);
this.image
.Source
= bitmapImage;
}
方法三:
顯示項(xiàng)目文件圖片:
string url = "ms-appx:///Images/flower.jpg";
async
private
void
LoadPictureByNative()
{
var
rass =
RandomAccessStreamReference.CreateFromUri(
new
Uri(url));
streamRandom
= await rass.OpenReadAsync();
var
bitmapImage =
new
BitmapImage();
bitmapImage.SetSource(streamRandom);
img.Source
= bitmapImage;
}