【问题标题】:Toggle the picture of picture box with picture box on_click c#用图片框on_click c#切换图片框的图片
【发布时间】:2016-03-17 08:18:31
【问题描述】:

我的图片框中有一个图像,使用资源我想要更改图像,当我单击 icon.png 时,它应该在图片框中更改为 icon1.png,然后当我再次单击图片框时,它应该改为icon.png

private void pictureBox10_Click(object sender, EventArgs e)
    {
        if (pictureBox10.ImageLocation != @"icon1.png")
        {
            var image = Image.FromFile(@"icon1.png");
            pictureBox10.Image = image;

        }
        if (pictureBox10.ImageLocation == @"icon1.png")
        {
            var image = Image.FromFile(@"icon.png");
            pictureBox10.Image = image;
        }

    }

但它不起作用,请帮我解决这个问题。

【问题讨论】:

  • 你试过什么?发生什么了?你试过打电话给DoEvents()吗?
  • 我只是使用 onlick 函数来更改图像,而 picurebox10.imagelocation 给出了一个空值,这就是为什么它只适用于第一个 if 条件
  • 调试代码时,ImageLocation 属性中出现了什么?我不是 100% 确定设置 Image 属性也会设置 ImageLocation 属性
  • 它有效,但仅适用于第一个 if 条件,其中 picturebox10.imagelocation 的值为 null,因此它满足第一个条件,因此仅适用于第一个条件图像正在更改
  • 其实条件不行

标签: c# winforms


【解决方案1】:

您从图像位置获得空值,因为在将图片分配给 Image 属性时未设置该值。有几种方法可以解决这个问题:

  1. 更改分配,以便使用 ImageLocation 分配

    pictureBox10.ImageLocation = @"icon1.png";
    
  2. 更改检查以查看 Image 属性是否等于您的新 Image

    pictureBox10.Image == Image.FromFile(@"icon.png");
    
  3. 在设置图片属性的同时设置图片位置

    pictureBox10.Image == Image.FromFile(@"icon.png");
    pictureBox10.ImageLocation = @"icon.png" ;
    

我觉得第二个可能不一样,你可能想尝试第一个或第三个

建议代码:

private void pictureBox10_Click(object sender, EventArgs e)
{
    if (pictureBox10.ImageLocation != @"icon1.png")
    {
        pictureBox10.ImageLocation = @"icon1.png"

    }
    if (pictureBox10.ImageLocation == @"icon1.png")
    {
        pictureBox10.ImageLocation = @"icon.png";
    }

}

或者:

private void pictureBox10_Click(object sender, EventArgs e)
{
    if (pictureBox10.ImageLocation != @"icon1.png")
    {
        var image = Image.FromFile(@"icon1.png");
        pictureBox10.Image = image;
        pictureBox10.ImageLocation = @"icon1.png";

    }
    if (pictureBox10.ImageLocation == @"icon1.png")
    {
        var image = Image.FromFile(@"icon.png");
        pictureBox10.Image = image;
        pictureBox10.ImageLocation = @"icon.png";
    }

}

您还需要更新初始属性设置以设置 ImageLocation 而不是 Image 属性,或者在设置 Image 文件的同时设置 ImageLocation

编辑

在我的脑海中,最初设置属性,你可以这样做(Source):

protected override void OnLoad(EventArgs e){
    pictureBox10.ImageLocation = @"icon.png";
}

虽然我不记得当时是否会创建 PictureBox,但如果没有,请改用 onShown 事件 (Source)

编辑 2

这里是另一种创建事件和设置属性的方法,首先按照here的步骤将事件onShown添加到表单中。您需要单击表单本身而不是表单内的控件来查找事件。

完成后,在事件中添加以下代码:

pictureBox10.ImageLocation = @"icon.png";

这应该有助于解决您的问题

【讨论】:

  • 您知道该属性工作正常,但条件不正确,因为 ipicturebox10.location 返回一个空值
  • 它返回null,因为它没有被设置,当你设置属性Image时它不会同时设置属性ImageLocation!
  • 什么不工作 @arn48 是 ImageLocation 属性设置与否?
  • 当我复制代码并粘贴它时什么都不做,这是什么问题?
  • 那是因为它可能在第一次创建 PictureBox 时没有设置。您是初始化 PictureBox 还是使用 Visual Studio 自动生成代码?如果是第一个,则需要在设置 Image 的第一点设置 ImageLocation,如果是第二个,则在表单构建期间(可能在您的构造函数中),您需要手动设置 ImageLocation 的属性
【解决方案2】:

尝试将图片直接引用到图片框中:

pictureBox10.Image = Image.FromFile(@"Images\a.bmp");

Source

【讨论】:

    【解决方案3】:

    谢谢大家,非常感谢,但有一些问题,但已经解决了这个问题,所以我写了实际的代码......

    public Form1()
        {
            InitializeComponent();
            pictureBox10.ImageLocation = @"icon.png";
        }
        private void pictureBox10_Click(object sender, EventArgs e)
        {
    
            if (pictureBox10.ImageLocation == @"icon1.png")
            {
                pictureBox10.ImageLocation = @"icon.png";
    
            }
            else
            {
                pictureBox10.ImageLocation = @"icon1.png";
            }
    
    
    
        }
    

    首先你必须初始化图像位置,然后使用两个 if 条件我认为这是主要问题使用 if else 无论如何,每个人都非常感谢@Draken

    【讨论】:

      猜你喜欢
      • 2015-08-29
      • 1970-01-01
      • 2017-10-18
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多