Click here to Skip to main content
15,909,656 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
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?
C#
//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;
}

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.
Posted
Updated 28-May-13 16:55pm
v3
Comments
[no name] 28-May-13 21:32pm    
"are they all the same image?", how would you expect us to know what it is that you have done when you do not show us the code that demonstrates your problem?
Sergey Alexandrovich Kryukov 28-May-13 21:37pm    
Apparently, you did touch them somehow? How? At this moment, my access to your hard drive is somewhat limited. You did not even bother to tag the UI library or application type.
—SA
Member 10040991 28-May-13 21:52pm    
Yes, well, there is a lot of code, and in several places. I pass one of the images to a function and get the overlaid image back - but apparently the original image is getting changed in the process. I'll see if I have an example.

1 solution

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.
C#
Passmap = (MapType)Drawnmap.Clone()

Make sure you change you clone your other maps if this works for you.
 
Share this answer
 
Comments
Member 10040991 29-May-13 16:54pm    
Cloning the image was just the start of the fix. I cloned the passimage from the image I needed. Then in the function, I immediately cloned it to the return image and disposed of the passimage. I switched all the background commands to the third image, some were using the second. Then I disposed of the third image just before calling for its update.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900