質問

I'm back, and I've been making smooth progress over the last few weeks but this has stumped me for the last three days or so with no breakthroughs.

This code is for reference.

    private void paintMap(int xToUpdate, int yToUpdate, int layerToUpdate)
    {
        // this should ONLY be called if mapBitmap has already been drawn initially
        Graphics gfx;

        // create a blank tile to place on top of whatever tile they want to place (to sucessfully update our tile)
        Bitmap blankTile = new Bitmap(Program.pixelSize, Program.pixelSize);
        gfx = Graphics.FromImage(blankTile);
        gfx.Clear(Color.Transparent);

        gfx = Graphics.FromImage(mapBitmap[layerToUpdate]);

        // only draw a map, if a map has been loaded
        if (mapLoaded)
        {
            #region Draw Map Loop
            // draw the map

            // find the tile at that point
            int tile = map.mapTile[xToUpdate][yToUpdate].getTileLayer(layerToUpdate);
            int x1 = Program.getTileXLocation(tile);
            int y1 = Program.getTileYLocation(tile);

            // set the tile's rectangle location 
            srcRect = new Rectangle(x1 * Program.pixelSize, y1 * Program.pixelSize, Program.pixelSize, Program.pixelSize);

            // draw the tile
            gfx.DrawImage(blankTile, xToUpdate * Program.pixelSize, yToUpdate * Program.pixelSize);
            gfx.DrawImage(gfxTiles, xToUpdate * Program.pixelSize, yToUpdate * Program.pixelSize, srcRect, units);

            // weather crap
            // screenMain.DrawImage(gfxNight, x1 * Program.pixelSize, y1 * Program.pixelSize, night, units);
            #endregion
        }
        else // otherwise, a map hasn't been loaded; clear the drawing surface
        {
            gfx.Clear(Color.Black);
        }

        gfx.Dispose();
    }

This is one of three methods that share the same name. I have a method that accepts no parameters, and refreshes the entire map (layers and all) and this code works.

But, when a user updates a map (by placing/removing a tile), I don't want to repaint the entire map. instead, I want to just update that particular space with the changes being made. Instead of updating each layer (by switching the tile painted there), etc etc, all the method did was place new tiles on top of the old ones. I added the blankTile bitmap, thinking that painting that, before painting the changes would rectify the issue, but it doesn't.

My inquiry is this: is there any way to just update that particular tile on the bitmap by deleting what is there and replacing it with the new image?

I can give more information if needed. I would like to keep using the built in GDI libraries, but if the only way to fix this problem is to switch, I would do so. But I'm almost sure there should be a way to fix this particular issue without switching my graphics library out. So far, it more than suits the needs of this project (minus this particular error).

役に立ちましたか?

解決

Found a quick and dirty solution, not sure if it's the best solution.

// clear the tile to be redrawn
int x2 = xToUpdate * Program.pixelSize;
int y2 = yToUpdate * Program.pixelSize;

for (int a = x2; a <= x2 + Program.pixelSize - 1; a++)
{
    for (int b = y2; b <= y2 + Program.pixelSize - 1; b++)
    {
        mapBitmapFringe.SetPixel(a, b, Color.Transparent);
    }
 }

Pretty much, I set the area of the bitmap I wish to update to transparent via SetPixel. After that, I can freely redraw a tile onto the area, because what was previously there has been "cleared".

Sorry for wasting time. I guess if I just spent a few more hours researching, I would have found the solution to my problem. (I don't know why I kept ignoring the SetPixel method!) I'm going to rename my question so that it's easier to understand what I was trying to accomplish. :)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top