The best way to clip and store an image

I have a 256x256 *.bmp file that I am loading in and I am not quite sure what the best way to actually clip and store all 64 tiles (32x32) out of it.

In the end I want something that I can easily place onto the screen.

What would be the best way to clip portions out of an image
What kind of object would be the best way to store this information

Any advice is welcomed.



Answer this question

The best way to clip and store an image

  • dehinson

    I've pretty much decided that the best way to do what i'm doing is to create 64 buttons to apply the clipped images too.

    So I create an array list and a Bitmap. I copy and paste the pixels from the loaded bitmap to the new bitmap and add it to the ArrayList. Then I just set each button image from the array list.


  • PRIDE

    I may have been a little missleading. I'm do apologize. I am wanting to clip and store inside the application in some sort of object array.

    Thank You PJ.



  • FloatingPoint

    I tried something like this:

    Graphics g = this.CreateGraphics();
    TextureBrush TileClip = new TextureBrush(TileMap);
    g.FillRectangle(TileClip, 0, 0, 32, 32);

    It seems that if you draw anything from a brush or a pen, it will get erased once another window goes over it. Thats is definitely not the route I want to take since I would likely have to update it constantly.


  • sushila

    what if i want to cut the image in ellipse shape or custome mouse path

  • Alexander Dsugan

    Here is a little non-tested example, it would break it the chunk size isn't a full piece of the source size but you can allways at more logic to it:


    public void SeperateBitmap( Bitmap source, Size chunkSize, string destinationFolder )
    {
    int counter = 1;

    for( int y = 0; y < source.Height; y += (y + chunkSize.Height <= source.Height chunkSize.Height : source.Height - y))
    {
    for( int x = 0; x < source.Width; x =+ (x + chunkSize.Width <= source.Width chunkSize.Width : source.Width - x ) )
    {
    String filename = Path.Combine( destinationFolder, counter + ".bmp" );

    Rectangle chunkBounds = new Rectangle( x, y, chunkSize.Width, chunkSize.Height );
    Bitmap chunk = source.Clone( chunkBounds, source.PixelFormat );
    chunk.Save( filename, ImageFormat.Bmp );
    }
    }
    }




  • srinivas jani

    The best way you decided isn't the best way in my humble opiniun. You can better use GDI+ for it, here is a little example:


    protected override void OnPaint(PaintEventArgs e)
    {
    SeperateBitmapandDrawInGraphics( _myBitmap, new Size( 32, 32 ), e.Graphics );
    }

    public void SeperateBitmapandDrawInGraphics( Bitmap source, Size chunkSize, Graphics g )
    {
    for( int y = 0; y < source.Height; y += (y + chunkSize.Height <= source.Height chunkSize.Height : source.Height - y))
    {
    for( int x = 0; x < source.Width; x =+ (x + chunkSize.Width <= source.Width chunkSize.Width : source.Width - x ) )
    {
    Rectangle chunkBounds = new Rectangle( x, y, chunkSize.Width, chunkSize.Height );
    Bitmap chunk = source.Clone( chunkBounds, source.PixelFormat );

    // Draw the image to the graphics object.
    g.DrawImageUnscaled( chunk, x, y );
    }
    }
    }




  • The best way to clip and store an image