Change PictureBox's image to image from my resources?

后端 未结 6 2061
我寻月下人不归
我寻月下人不归 2020-12-08 14:38

How do I set a PictureBox image to an image from my resources?

(I tried this without success: pictuerbox.Image = \"img_location\";)

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 14:48

    Ken has the right solution, but you don't want to add the picturebox.Image.Load() member method.

    If you do it with a Load and the ImageLocation is not set, it will fail with a "Image Location must be set" exception. If you use the picturebox.Refresh() member method, it works without the exception.

    Completed code below:

    public void showAnimatedPictureBox(PictureBox thePicture)
    {
                thePicture.Image = Properties.Resources.hamster;
                thePicture.Refresh();
                thePicture.Visible = true;
    }
    

    It is invoked as: showAnimatedPictureBox( myPictureBox );

    My XAML looks like:

        
    
            
                
                    
                    
                
                
    
    

    I realize this is an old post, but loading the image directly from a resource is was extremely unclear on Microsoft's site, and this was the (partial) solution I came to. Hope it helps someone!

提交回复
热议问题