Home > Back-end >  I have a problem with uploading the image to the canvas in UWP
I have a problem with uploading the image to the canvas in UWP

Time:06-04

In my MainPage when I try to run the function(ShowEntity) the image wont show on the background. When i run it on my player(class in my project) then the image wont show up on the screen. i tried to change the format few time but i dont find the problem. i think there is something wrong in the ShowEntity function.

this is an example of how i try to make it work. Player _player1 = new Player(500, 500, 150, 150, "ms-appx:///Asssets/Player5.gif");

    public class Piece
    {
        private double _x;

        private double _y;

        public int _width;

        private int _height;

        public string _image;

        public Image _imagesource { get; set; }

     public Piece(double x, double y, int width, int height, string image)//BitmapImage imagesource)
        {
            _x = x;

            _y = y;

            _image = image;

            _width = width;

            _height = height;

            _imagesource = ShowEntity();           
        }

        private Image ShowEntity()
        {
            Image _img = new Image();

            _img.Source = new BitmapImage(new Uri(_image));

            _img.Height = _height;

            _img.Width = _width;

            Canvas.SetLeft(_img, _x);

            Canvas.SetTop(_img, _y);

            GameManager.MyCanvas.Children.Add(_img);

            return _img;
        }

CodePudding user response:

The problem looks your image was covered by other element, if you want to show element in the Canvas, you need to set ZIndex for element.

Canvas.ZIndex declares the draw order for the child elements of a Canvas. This matters when there is overlap between any of the bounds of the child elements. A higher z-order value will draw on top of a lower z-order value. If no value is set, the default is -1.

If you want to set image as background, you could set it's ZIndex with samll value. And other way is set Canvas Background with ImageBrush directly like the fllowing.

MyCanvas.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri(this.BaseUri, "ms-appx:///Assets/enemy.png")), Stretch = Stretch.Fill };
  • Related