winforms 将PictureBox的图像更改为我的资源中的图像?

30byixjq  于 6个月前  发布在  其他
关注(0)|答案(6)|浏览(120)

如何将PictureBox图像设置为来自资源的图像?

  • (我尝试了这个没有成功:pictuerbox.Image = "img_location";)*
p4tfgftt

p4tfgftt1#

如果您使用Visual Studio UI加载资源,那么您应该能够做到这一点:

picturebox.Image = project.Properties.Resources.imgfromresource

字符串

qlckcl4x

qlckcl4x2#

Ken有正确的解决方案,但您不想添加picturebox.Image.Load()成员方法。
如果你使用一个Load来执行这个操作,并且ImageLocation没有被设置,那么它将失败并出现“Image Location must be set”异常。如果你使用picturebox.Refresh()成员方法,那么它将在没有异常的情况下正常工作。
完整代码如下:

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

字符串
它被调用为:showAnimatedPictureBox(myPictureBox);
我的XAML看起来像:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        xmlns:winForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="myApp.MainWindow"
        Title="myApp" Height="679.079" Width="986">

        <StackPanel Width="136" Height="Auto" Background="WhiteSmoke" x:Name="statusPanel">
            <wfi:WindowsFormsHost>
                <winForms:PictureBox x:Name="myPictureBox">
                </winForms:PictureBox>
            </wfi:WindowsFormsHost>
            <Label x:Name="myLabel" Content="myLabel" Margin="10,3,10,5" FontSize="20" FontWeight="Bold" Visibility="Hidden"/>
        </StackPanel>
</Window>


我知道这是一个旧的职位,但直接从资源加载图像是非常不清楚的微软的网站上,这是(部分)解决方案,我来了。希望它能帮助别人!

w8f9ii69

w8f9ii693#

好的,首先你需要将图片导入到你的项目中
1).在表单设计中选择图片框
2).打开PictureBox任务(它是一个小箭头,位于PictureBox边缘的右侧)
3).点击“选择图像...”
4).选择第二个选项“Project resource file:“(此选项将创建一个名为“Resources”的文件夹,您可以使用Properties.Resources访问该文件夹)
5).单击导入并从计算机中选择您的映像(现在,将在步骤4中创建的资源文件夹中发送与映像同名的映像副本)
6).单击Ok
现在图片已经在你的项目中了,你可以用Properties命令来使用它。当你想改变图片框中的图片时,只需输入以下代码:

pictureBox1.Image = Properties.Resources.myimage;

字符串
注意:myimage代表图像的名称.在Resources后面输入点后,在您的选项中,它将是您导入的图像文件。

eagi6jfj

eagi6jfj4#

请尝试以下操作:

myPictureBox.Image = global::mynamespace.Properties.Resources.photo1;

字符串
并将命名空间替换为项目命名空间

pw9qyyiw

pw9qyyiw6#

您必须将资源文件的完整路径指定为应用程序资源中的“image”的名称,请参见下面的示例。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    PictureBox1.Image = My.Resources.Chrysanthemum
End Sub

字符串
在分配给Image属性的路径中,在MyResources后面指定资源的名称。
但是在你做任何你必须从一个图像文件导入到你的应用程序的资源部分之前,它可以存在,也可以创建你自己的。
再见

相关问题