如何检查是否图像对象是同为一个从资源? [英] How to check if image object is the same as one from resource?

查看:104
本文介绍了如何检查是否图像对象是同为一个从资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想创建一个简单的程序,只是改变了图片在一个图片框中单击时。我只用两张图片的时刻,所以我的图片框点击事件函数
的代码看起来像:

 私人无效pictureBox1_Click(对象发件人,EventArgs五)
{
如果(pictureBox1.Image == Labirint.Properties.Resources.first)
pictureBox1.Image = Labirint.Properties.Resources。 reitmi;
,否则如果(pictureBox1.Image == Labirint.Properties.Resources.reitmi)
pictureBox1.Image = Labirint.Properties.Resources.first;
}



由于某些原因,if语句它不工作正常,图像没有变化。
我应该怎么办?






请注意:原代码包含的错误与第二个如果撤消效果第一,如果条件的将会的与修复工作由 Cyral的回答,但添加其他没有解决的问题 - 通过与其他步进仍然没有显示出对于任何一个图像匹配

$ b $。 b

 如果(pictureBox1.Image == Labirint.Properties.Resources.first)
pictureBox1.Image = Labirint.Properties.Resources.reitmi;
如果(pictureBox1.Image == Labirint.Properties.Resources.reitmi)//失踪别的
pictureBox1.Image = Labirint.Properties.Resources.first;


解决方案

 如果( pictureBox1.Image == Labirint.Properties.Resources.first)

这里有没有足够的陷阱。 .NET程序员都知道的。负责的很多的那臃肿的内存空间运行的程序。使用Labirint.Properties.Resources.xxxx属性创建一个图片对象,它永远不会匹配任何其它图像。你需要在你的类的字段使用属性只有一次,存储图像。大致为:

 私人形象第一; 
私人图片reitmi;

公共Form1的(){
的InitializeComponent();
第一= Labirint.Properties.Resources.first;
reitmi = Labirint.Properties.Resources.reitmi;
pictureBox1.Image =第一;
}

和现在你可以对它们进行比较:

 私人无效pictureBox1_Click(对象发件人,EventArgs五){
如果(pictureBox1.Image ==第一)pictureBox1.Image = reitmi;
,否则pictureBox1.Image =第一;
}

和避免记忆膨胀:

 私人无效Form1_FormClosed(对象发件人,FormClosedEventArgs E){
first.Dispose();
reitmi.Dispose();
}


So I'm trying to create a simple program that just change the picture in a picture box when it's clicked. I'm using just two pictures at the moment so my code for the picture box click event function looks like that:

private void pictureBox1_Click(object sender, EventArgs e)
    {
       if (pictureBox1.Image == Labirint.Properties.Resources.first)
            pictureBox1.Image = Labirint.Properties.Resources.reitmi;
       else if (pictureBox1.Image == Labirint.Properties.Resources.reitmi)
            pictureBox1.Image = Labirint.Properties.Resources.first;
    }

For some reason the if statement it's not working and the picture don't change. What should I do?


Note: original code contained bug with second if undoing effect of first if condition would work with fix suggested by Cyral's answer, but adding else did not fix the issue - stepping through the code with else still shows no matches for either image.

if (pictureBox1.Image == Labirint.Properties.Resources.first)
    pictureBox1.Image = Labirint.Properties.Resources.reitmi;
if (pictureBox1.Image == Labirint.Properties.Resources.reitmi) // was missing else
    pictureBox1.Image = Labirint.Properties.Resources.first; 

解决方案

     if (pictureBox1.Image == Labirint.Properties.Resources.first)

There's a trap here that not enough .NET programmers are aware of. Responsible for a lot of programs that run with bloated memory footprints. Using the Labirint.Properties.Resources.xxxx property creates a new image object, it will never match any other image. You need to use the property only once, store the images in a field of your class. Roughly:

    private Image first;
    private Image reitmi;

    public Form1() {
        InitializeComponent();
        first = Labirint.Properties.Resources.first;
        reitmi = Labirint.Properties.Resources.reitmi;
        pictureBox1.Image = first;
    }

And now you can compare them:

    private void pictureBox1_Click(object sender, EventArgs e) {
        if (pictureBox1.Image == first) pictureBox1.Image = reitmi;
        else pictureBox1.Image = first;
    }

And to avoid the memory bloat:

    private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
       first.Dispose();
       reitmi.Dispose();
    }

这篇关于如何检查是否图像对象是同为一个从资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆