4 Replies - 334 Views - Last Post: 21 September 2013 - 06:41 PM Rate Topic: -----

#1 Rhino1111  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 99
  • View blog
  • Posts: 228
  • Joined: 28-August 13

Constructing a Bitmap with multiple image pieces.

Posted 21 September 2013 - 05:57 PM

Hey D.I.C! I'm in a situation where I need to construct a Bitmap image from various pieces from various images.
Bitmap.Clone() fits my needs perfectly for getting the source "piece" from each individual image, but I looked through the bitmap class and there doesn't seem to be a way I can "insert" this cloned piece into (x, y) coord on the bitmap that needs to display the entire image.

Is there a way to even do this? If so, what things do I need to look into?

Is This A Good Question/Topic? 0
  • +

Replies To: Constructing a Bitmap with multiple image pieces.

#2 Rhino1111  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 99
  • View blog
  • Posts: 228
  • Joined: 28-August 13

Re: Constructing a Bitmap with multiple image pieces.

Posted 21 September 2013 - 06:24 PM

Well I managed to solve my problem.
This is the solution I came up with. I'm Absolutely horrible with GDI+, so if something about this "algorithm" can be improved, please let me know!


public void SavePreview(string ApplicationstartUpPath, string FileName)
        {
            preview = new System.Drawing.Bitmap(map.mapWidth * 16, map.mapHeight * 16);
            spriteBatch.Begin();
            int tilesheet = 1;
            int comparer = 0;
            System.Drawing.Bitmap tileSheet;
            tileSheet = new System.Drawing.Bitmap(ApplicationstartUpPath + @"\Content\Tilesets\Tileset1.png");
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(preview);
            System.Drawing.Imaging.PixelFormat format = preview.PixelFormat;
            for (int y = 0; y < map.mapHeight; y++)
            {
                for (int x = 0; x < map.mapWidth; x++)
                {
                    for (int z = 0; z < 4; z++)
                    {
                        if (map.map[x, y, z] != null && map.map[x, y, z].tileID != 0)
                        {
                            comparer = TileHelper.GetTileSet(map.map[x, y, z].tileID) + 1;
                            if (comparer != tilesheet)
                            {
                                tileSheet.Dispose();
                                tilesheet = TileHelper.GetTileSet(map.map[x, y, z].tileID) + 1;
                                string filepath = ApplicationstartUpPath +
                                @"\Content\Tilesets\Tileset" + tilesheet + ".png";
                                tileSheet = new System.Drawing.Bitmap(filepath);
                            }
                            Rectangle sRect = TileHelper.GetPosition(map.map[x, y, z].tileID);
                            System.Drawing.Rectangle source = new System.Drawing.Rectangle(sRect.X, sRect.Y, sRect.Width, sRect.Height);
                            System.Drawing.Bitmap tile = tileSheet.Clone(source, format);
                            g.DrawImage(tile, x*16, y*16, 16, 16);
                            tile.Dispose();
                            
                        }
                        
                    }
                }
            }
            preview.Save(ApplicationstartUpPath + "\\" + FileName + ".bmp");
            spriteBatch.End();
            previewSurface = IntPtr.Zero;
            preview.Dispose();
            g.Dispose();
            
        }


The comparer is used to determine if we need to load a different "tilesheet" or not. If not, we don't waste time reloading the same tileset. the "tile" clone is disposed after every iteration in z. This draws the map perfectly to a bitmap image and saves it, but i'm sure this could be made more efficient.

This post has been edited by Rhino1111: 21 September 2013 - 06:27 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,838
  • Joined: 12-December 12

Re: Constructing a Bitmap with multiple image pieces.

Posted 21 September 2013 - 06:27 PM

Will you be saving the new BitMap or just drawing it?

The docs for Bitmap.Clone suggest that you would draw to the Graphics object rather than directly to a bitmap:

private void Clone_Example1(PaintEventArgs e)
{

    // Create a Bitmap object from a file.
    Bitmap myBitmap = new Bitmap("Grapes.jpg");

    // Clone a portion of the Bitmap object.
    Rectangle cloneRect = new Rectangle(0, 0, 100, 100);
    System.Drawing.Imaging.PixelFormat format =
        myBitmap.PixelFormat;
    Bitmap cloneBitmap = myBitmap.Clone(cloneRect, format);

    // Draw the cloned portion of the Bitmap object.
    e.Graphics.DrawImage(cloneBitmap, 0, 0);
}

Added: but I see that you've solved it ;)

This post has been edited by andrewsw: 21 September 2013 - 06:29 PM

Was This Post Helpful? 0
  • +
  • -

#4 Rhino1111  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 99
  • View blog
  • Posts: 228
  • Joined: 28-August 13

Re: Constructing a Bitmap with multiple image pieces.

Posted 21 September 2013 - 06:30 PM

I'll be saving the Bitmap. I figured what I would need would be somewhere in the System.Drawing namespace but not in the Bitmap class. Check out my 2nd post, as that solution works correctly and saves the bitmap correctly, but i'm not too sure if its as efficient as it can be. I suck at GDI+ related things, so if you see anything I can improve in my solution, I would really appreciate the advice!

EDIT: Oh and I removed the spriteBatch.begin/end calls in that solution just now, as i was initially trying to use xna to render onto the bitmap then save it, but that only saved blank bitmaps.

This post has been edited by Rhino1111: 21 September 2013 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,838
  • Joined: 12-December 12

Re: Constructing a Bitmap with multiple image pieces.

Posted 21 September 2013 - 06:41 PM

Sorry, I can't help you with this, maybe someone else ;). Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1