Windows Runtime: How to set image in a ellipse shape button?

前端 未结 4 1124
滥情空心
滥情空心 2021-01-25 18:50
4条回答
  •  醉话见心
    2021-01-25 19:30

    You can use Background property of the button to set image on the background.You can use it from code behind also like:

    public BitmapImage LoadBackgroundImage(string fileName)
    {
                var image = new BitmapImage();
                try
                {
                    image.BeginInit();
                    if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
                    {
                        var bytes = File.ReadAllBytes(fileName);
                        image.StreamSource = new MemoryStream(bytes);
                    }
                    else
                    {
                        var bytes = File.ReadAllBytes(Path.GetFullPath(Properties.Resources.DefaultBackgroundImage));
                        image.StreamSource = new MemoryStream(bytes);
                    }
                    image.CacheOption = BitmapCacheOption.OnLoad;
                    image.EndInit();
                    image.Freeze();
                }
                catch (FileNotFoundException ex)
                {
                      throw ex;
                }
    
                return image;
            }
    

    in your button click event add the following line

    btnProfilePicture.Background=LoadBackgroundImage(yourfilename.jpg); //you can use .jpg,.jpeg,*.png

提交回复
热议问题