类和位图

时间:2016-02-07 06:45:04

标签: c# bitmap gdi+ handle

目前我有一个非常有趣的困境。我有一个名为misc.Weapon的自定义对象,其中包含一个位图。例如:

private Image _proj;
            public Image ProjectileImage
            {
                get
                {
                    return _proj;
                }
                set
                {
                    _proj = value;
                }
            }

除非我错了,否则图像类会占用一个句柄。手柄太多,我遇到了一些问题。我还有一个庞大的misc.Weapon s库 叫weapons。这就是它的样子:

public class Weapons
    {
        public misc.Trinket endanklet = new misc.Trinket("Anklet of Edurance", "Given to the 4th hero of Aebenclaw during the war, to live for a 100 years more.");
        public void inittrink()
        {
            endanklet.Method = () =>{PSTATS.maxhealth += 50;};
            endanklet.end = () =>{PSTATS.maxhealth -= 50;};
        }
        /// <summary>
        /// Speeds:
        /// 1 = very slow
        /// 2 = slow
        /// 3 = normal
        /// 4 = quick
        /// 5 = lightning
        /// </summary>
        public misc.Weapon natureblade = new misc.Weapon("Blade of Nature", "Laiden with the flowers of The 9 Wreathes", 22, 22, "M", 3, Properties.Resources.BladeOfNature);
        public misc.Weapon dagger = new misc.Weapon("Dagger", "Sharp as the eye that throws it", 4, 0, "M", 5, Properties.Resources.Dagger);
        public misc.Weapon bluescepter = new misc.Weapon("Saphire Sceptre", "This is what happens when a saphire is put on a stick", 7, 15, "S", 2, Properties.Resources.SaphireSceptre);
        public misc.Weapon silscepter = new misc.Weapon("Platinum Sceptre", "Dat platinum doe", 15, 40, "S", 2, Properties.Resources.SilverSceptre);
        public misc.Armour rustychainmail = new misc.Armour(7, "Rusty Chainmail", "So out of fashion");

        public misc.Armour wraitharmour = new misc.Armour(8, "Shadow plated hide", "Found in a warehouse in the southern spike, which was said to be haunted");
        public misc.Armour telepathic = new misc.Armour(2, "Telepathic armour", "Use your mind!");

        public misc.Weapon volcanictome = new misc.Weapon("Volcanic Storm", "Fire is always a slow death, the Volcanic Storm, slightly less so", 5, 40, 6, 3, Properties.Resources.VolcanicTome,Properties.Resources.VSProjectile);
        public misc.Weapon icyblow = new misc.Weapon("Icy Blow", "Make's your head hurt", 18, 10, "M", 1, Properties.Resources.IcyBlow);
    }

我需要这个库,但是对于每种武器,有一些有价值的手柄被从我的PC内存中拿走。我需要能够单独引用每个武器(因为我让它们随机丢弃而我不想在现场制作新的misc.Weapon)而不需要花费GDI +对象/句柄。

2 个答案:

答案 0 :(得分:0)

您可以使ProjectileImage属性变得懒惰。您不能立即加载和分配Bitmap,而只能在第一次访问该属性时进行分配。

此外,还有许多句柄可用。你手柄用完的事实让我很怀疑。你是否正确处置资源?您确定最终删除了所有引用,以便它们可以被GC化吗?

答案 1 :(得分:0)

我想通了,实际上是几行代码与其他类一起调用它们的类:/