Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User
Image ; Stream ; File ;

Wie lädt man in Windows ein Bild in ein Image-Control

08.04.2020 (👁10786)


Mit dem folgenden Code lädt man in Windows Desktop Programmen ein Bild in ein Anzeigeelement.

//*get File

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(this.BaseUri, "_Images/Demo_Photo01.jpg"));

//*load file as stream

IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

//*create Bitmap from filestream

BitmapImage bmpImage = new BitmapImage();

bmpImage.SetSource(fileStream);

//*load bitmap into image-control

ctlImage.Source = bmpImage;

UWP, WPF

Windows Developer

 

        private async void BtnLoad_Click(object sender, RoutedEventArgs e)

        {

            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(this.BaseUri, "_Images/Demo_Photo01.jpg"));

            IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

            BitmapImage bmpImage = new BitmapImage();

            bmpImage.SetSource(fileStream);

            ctlImage.Source = bmpImage;

        }

<Page

    x:Class="Win2D01.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:Win2D01"

    xmlns:canvas="using:Microsoft.Graphics.Canvas.UI.Xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

   

    mc:Ignorable="d"

    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

 

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="199*"/>

            <ColumnDefinition Width="1301*"/>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="63*"/>

            <RowDefinition Height="437*"/>

        </Grid.RowDefinitions>

 

 

        <Button x:Name="BtnLoad" Click="BtnLoad_Click" Content="Load Image" Grid.Row="0"  Grid.Column="0"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  />

        <Border Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >

            <Image x:Name="ctlImage"   />

        </Border>

       

    </Grid>

</Page>