当我期望它们保持不变时,图像会发生变化。 [英] Images getting changed when I expect them to remain untouched.

查看:66
本文介绍了当我期望它们保持不变时,图像会发生变化。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用背景图片地图的游戏。在地图上我绘制游戏片段,当用户点击某个区域时,我会在他们点击的部分上画一个高亮显示。所以我创建了4个全局图像,每个图像一个加一个传递到绘图部分。最终结果是所有4个图像看起来都一样。它必须与通过引用传递有关 - 它们是否都是相同的图像?我希望不同名称的变量保持独立。当我使用克隆或新的时它可以正常工作但是程序在点击20次后内存不足。如果我丢弃图像,它们都会变成空白。很高兴找到一种方法来使这些图像通过值传递而不是引用。它们都是相同尺寸,原始的扁平覆盖层。但是我需要保留原版和原版+游戏部分,最后突出显示的叠加是可以消耗的。关于正确方法的任何建议吗?

I have game that uses a background image map. On the map I draw game pieces, and when the user clicks on an area I draw a highlight on the section they clicked. So I''ve created 4 global images, one for each plus one to pass to the drawing section. The final result is that all 4 images look the same. It must be something to do with passing by reference - are they all the same image? I would expect variables of different names to remain independent. When I use clone or new it works correctly but the program runs out of memory after 20 clicks. If I dispose of an image, they all go blank. It would be nice to find a way to make these images pass by value and not reference. They are all the same size, flattened overlays of the original. But I need to preserve the Original and the original + the game pieces, the final highlighted overlay is expendable. Any suggestions on the right approach for this?

//One of the scenarios has this:
 
Draw d = new Draw();
PassMap = GameMap;
d.DrawMap(UiP, MapHexes, PassMap, Globals.Gaul, Globals.Germania, out DrawnMap);
map.BackgroundImage = DrawnMap;
map.BackgroundImageLayout = ImageLayout.Stretch;
map.Refresh();
 
//So I would expect the DrawnMap to have images on it that the GameMap doesn't.
 
//The mouseclick has this:
 
PassMap = DrawnMap;
mt.selectHex(Globals.CurrentHex, zoom, MapHexes, HexHighlight, PassMap, out HighlightMap);
map.BackgroundImage = HighlightMap;
map.BackgroundImageLayout = ImageLayout.Stretch;
map.Refresh();
 
//Here is where the area is highlighted:
 
public void selectHex(int hex_id, int zoom, List MapHexes, Bitmap HexHighlight, Bitmap PassMap, out Bitmap HighlightMap)
{
using (Graphics g = Graphics.FromImage(PassMap))
g.DrawImage(HexHighlight, new Rectangle(MapHexes[hex_id].Hex_x - 48, MapHexes[hex_id].Hex_y - 55, HexHighlight.Width, HexHighlight.Height));
HighlightMap = PassMap;
}



当我点击地图时,我会得到一串突出显示的区域,当我只想要一个时。显然,HighlightMap是其他参考地图。如果我使用new或clone,我会得到预期的结果,但是在点击20次后内存不足。



不确定这有多大帮助。


When I click around the map, I get a string of highlighted areas, when I only want one. Apparently the HighlightMap is the other maps by reference. If I use new or clone, I get the expected result, but run out of memory after 20 clicks.

Not sure this helps much.

推荐答案

当你将Passmap指定为Drawnmap时,它有一个参考,尝试克隆地图。

我不知道对象的类型是什么 Drawnmap 但是你只需要替换它。

When you assign Passmap as Drawnmap it has a reference, try cloning the map.
I don''t know what the type of object Drawnmap is but you''ll just have to replace it.
Passmap = (MapType)Drawnmap.Clone()



如果适用,请确保更改克隆其他地图。


Make sure you change you clone your other maps if this works for you.


这篇关于当我期望它们保持不变时,图像会发生变化。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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