Hi @水 知 ,
Welcome to Micrsoft Q&A!
It is recommended that you use RandomAccessStreamReference.CreateFromUri(Uri) to access BitmapImage.
The name of my image control is Testimg
.
You can refer to the following code sample, hope it helps!
private void Image_Loaded(object sender, RoutedEventArgs e)
{
Image img = sender as Image;
BitmapImage bitmapImage = new BitmapImage();
img.Width = bitmapImage.DecodePixelWidth = 80; //natural px width of image source
// don't need to set Height, system maintains aspect ratio, and calculates the other
// dimension, so long as one dimension measurement is provided
bitmapImage.UriSource = new Uri(img.BaseUri, "Assets/StoreLogo.png");
img.Source = bitmapImage;
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
BitmapImage bitmapImage= Testimg.Source as BitmapImage;
RandomAccessStreamReference stream = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource);
var streamContent = await stream.OpenReadAsync();
byte[] buffer = new byte[streamContent.Size];
await streamContent.ReadAsync(buffer.AsBuffer(), (uint)streamContent.Size, InputStreamOptions.None);
}
<Image x:Name="Testimg" Loaded="Image_Loaded" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100"/>
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.