简体   繁体   中英

UWP C# Fill Rectangle with Image

My program has a rectangle that I color depending on the color from a color picker. What I want to do is to have it choose an image for black instead of just showing black. The code I have to fill the rectangle with the image is:

p1rect1.Fill = new ImageBrush
{
    ImageSource = new BitmapImage(new Uri(@"pack://application:,,,/LED;Assets/Images/off.png", UriKind.Absolute))
};

I located this code from ac# wpf discussion, but it doesn't seem to be working in mine, which is UWP. What happens is that the rectangle doesn't change at all and is left as its original color when it was created in the xaml code.

What would the proper code be to put an image into a rectangle in C# code? Thanks.

You need to use the ms-appx scheme to reference files in your app package.

p1rect1.Fill = new ImageBrush
{
    ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/Images/off.png"))
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM