Click to See Complete Forum and Search --> : Slicing and Saving Parts of a Bitmap


dacky
December 22nd, 2005, 02:38 AM
Hi guyz hope you can help me with this...if im gonna slice a bitmap image into columns and rows and save this parts into a bmp file individually..for instance im gonna slice an image like 2x2 then all in all i have 4 pictures to save...how am i gonna do this? any ideas? thanks guyz

Marc G
December 22nd, 2005, 03:20 AM
You can load the big image. Then create a smaller bitmap and bitblt part of the big image to this small one. Then save it. Check http://www.codeguru.com/cpp/g-m/bitmap/ to learn how to load/save bitmaps.

dacky
December 22nd, 2005, 05:30 AM
You can load the big image. Then create a smaller bitmap and bitblt part of the big image to this small one. Then save it. Check http://www.codeguru.com/cpp/g-m/bitmap/ to learn how to load/save bitmaps.


thanks marc..but i need different sections of the image(like cropping the four parts of the image) and save it into an individual bmp or jpg files...any ideas?

Marc G
December 22nd, 2005, 08:58 AM
Barebone code layout:
CBitmap bmpBig;
// Load big bmp into bmpBig
CDC dcBig;
// Create dcBig and select bmpBig into it.

CBitmap bmpSmall;
// Create empty small bitmap with correct dimensions
CDC dcSmall;
// Create dcSmall and select bmpSmall into it.

// use bitblt to blit a rectangle from dcBig to dcSmall.
// Unselect bmpSmall from dcSmall
// Save bmpSmall to .bmp file.

CBitmap bmpSmall2;
// Create empty small bitmap with correct dimensions
CDC dcSmall2;
// Create dcSmall and select bmpSmall into it.

// use bitblt to blit a rectangle from dcBig to dcSmall.
// Unselect bmpSmall from dcSmall
// Save bmpSmall to .bmp file.

...
Of course, if bmpSmall and bmpSmall2 are equal in size, you don't need to recreate it but you can simply reuse it.

dacky
December 22nd, 2005, 09:19 PM
hello marc..sorry we are not on the same frequency..im a beginner in VC++..what do you mean by creating bmpSmall with correct dimensions? the thing i know in creating a CBitmap is by this declaration.

Example:
CBitmap bmpSmall;
if(bmpSmall.DeleteObject()) //delete the current object
bmpSmall.detach() //if there was a bitmap, dteach it

bmpSmall.Attach(hbitmap); //attach the currently loaded bitmap to the
// bitmap object





thanks marc

dacky
December 23rd, 2005, 03:21 AM
one more thing how do you unselect bmpSmall from dcSmall?marc i got what you mean...i have now the bitmap loaded in the memory and they are now in a CBitmap Object..now, what would i like to do is save this individual files into bmp format by opening the CFileDialog...any ideas?

Marc G
December 23rd, 2005, 03:29 AM
You can use CreateCompatibleBitmap (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cbitmap.3a3a.createcompatiblebitmap.asp) to create a bitmap with certain size.

See this article (http://www.codeguru.com/cpp/g-m/bitmap/capturing/article.php/c4915/) to learn how to save bitmaps.

dacky
December 25th, 2005, 09:41 PM
thanks so much for your knowledge marc..it works and it really helped me a lot..Merry christamas

Marc G
December 26th, 2005, 03:14 PM
You're welcome :wave:
And happy holidays :)

fulltime
April 25th, 2006, 09:05 PM
Barebone code layout:
CBitmap bmpBig;
// Load big bmp into bmpBig
CDC dcBig;
// Create dcBig and select bmpBig into it.

CBitmap bmpSmall;
// Create empty small bitmap with correct dimensions
CDC dcSmall;
// Create dcSmall and select bmpSmall into it.

// use bitblt to blit a rectangle from dcBig to dcSmall.
// Unselect bmpSmall from dcSmall
// Save bmpSmall to .bmp file.

CBitmap bmpSmall2;
// Create empty small bitmap with correct dimensions
CDC dcSmall2;
// Create dcSmall and select bmpSmall into it.

// use bitblt to blit a rectangle from dcBig to dcSmall.
// Unselect bmpSmall from dcSmall
// Save bmpSmall to .bmp file.

...
Of course, if bmpSmall and bmpSmall2 are equal in size, you don't need to recreate it but you can simply reuse it.

Hi Marc,

I have exactly the same problem as dacky and i need your help..

I am relatively new to visual C and i wonder what do u mean by CDC dcBig;
// Create dcBig and select bmpBig into it.

how do i select bmpBig into it? thks

fulltime
April 25th, 2006, 10:45 PM
wat i need to do is

with a BIG image of 635*450, i wan to divide it into many smaller imageS of 40*30... so how do i move my so called dcSmall along the BIG image so that it is able to capture all the different images on the BIG image?

is dcSmall like a template, that is set to the size of the image tat i wan to cut out?

thks alot.. i am in urgent need of help..

FT :)

Marc G
April 27th, 2006, 02:26 PM
CDC dcBig;
// Create dcBig and select bmpBig into it.

how do i select bmpBig into it? thks
CDC dcBig;
dcBig.CreateCompatibleDC(&targetDC); // See MSDN and articles on codeguru.com

CBitmap bmpBitmap;
//load your big bitmap into bmpBitmap (see articles on codeguru.com)

CBitmap* pOldBmp = dcBig.SelectObject(&bmpBitmap);
...

fulltime
May 3rd, 2006, 09:19 PM
Hi Marc,

This is my 1st time "venturing" into graphics programming n i seek ur assistance in explaining to me what the following mean.. thks

CBitmap bmpBig;
// Load big bmp into bmpBig

--> do u mean use loadBitmap?

CDC dcBig;
CBitmap* pOldBmp = dcBig.SelectObject(&bmpBitmap);
// Create dcBig and select bmpBig into it.

--> wats the purpose of the above statement? dcBig is a Device Context, so are we trying to create a DC then we load our desired bmp file onto it?


CBitmap bmpSmall;
// Create empty small bitmap with correct dimensions
CDC dcSmall;
// Create dcSmall and select bmpSmall into it.


--> are the codes for small bitmap same as the big bmp?


// use bitblt to blit a rectangle from dcBig to dcSmall.
// Unselect bmpSmall from dcSmall
// Save bmpSmall to .bmp file.

bitblt is to copy a bmp rite? y is there a need to unselect bmpSmall from dcSmall? wats the difference between the 2?

CBitmap bmpSmall2;
// Create empty small bitmap with correct dimensions
CDC dcSmall2;
// Create dcSmall and select bmpSmall into it.

// use bitblt to blit a rectangle from dcBig to dcSmall.
// Unselect bmpSmall from dcSmall
//Save bmpSmall to .bmp file.

--> wats the purpose of bmpSmall2??

thks marc for ur time and patientence...

FT

fulltime
May 4th, 2006, 04:01 AM
hi all, this is wat i have so far..


#include <afxwin.h>

void main(void)
{

CBitmap bmpBig;


HANDLE hBitMap = LoadImage(0, "D:\\Foo\\01.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

bmpBig.Attach (hBitMap); // Load big bmp into bmpBig
CDC dcBig;
dcBig.CreateCompatibleDC(NULL);
CBitmap* pOldBmp = dcBig.SelectObject(&bmpBig); // Create dcBig and select bmpBig into it.

CBitmap bmpSmall; // Create empty small bitmap with correct dimensions
HDC dc= CreateCompatibleDC(NULL);
BITMAPINFO i;
ZeroMemory( &i.bmiHeader, sizeof(BITMAPINFOHEADER) );
i.bmiHeader.biWidth=12; // Set size you need
i.bmiHeader.biHeight=16; // Set size you need
i.bmiHeader.biPlanes=1;
i.bmiHeader.biBitCount=1; // Can be 8, 16, 32 bpp or
// other number
i.bmiHeader.biSizeImage=0;
i.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
i.bmiHeader.biClrUsed= 0;
i.bmiHeader.biClrImportant= 0;
VOID *pvBits;
HBITMAP hbmp= CreateDIBSection( dc, &i,DIB_RGB_COLORS,&pvBits,NULL,0 );
bmpSmall.Attach (hbmp);


CDC dcSmall; // Create dcSmall and select bmpSmall into it.
dcSmall.CreateCompatibleDC(NULL);
CBitmap* pSOldBmp = dcSmall.SelectObject(&bmpSmall);


// use bitblt to blit a rectangle from dcBig to dcSmall.
// Unselect bmpSmall from dcSmall
// Save bmpSmall to .bmp file.

CBitmap bmpSmall2;
// Create empty small bitmap with correct dimensions
CDC dcSmall2;
// Create dcSmall and select bmpSmall into it.

// use bitblt to blit a rectangle from dcBig to dcSmall.
// Unselect bmpSmall from dcSmall
// Save bmpSmall to .bmp file.

}

3 qns

1. wats the purpose of pOldBmp in "CBitmap* pOldBmp = dcBig.SelectObject(&bmpBig);"

2. when i create "CBitmap bmpSmall; // Create empty small bitmap with correct dimensions", where shld i obtain my DC reference from? wat i wrote for creating a small bmp file, are the codes correct?

3. can someone pls elaborate wats the purpose of the 3 steps below and how can i code them? thks

// use bitblt to blit a rectangle from dcBig to dcSmall.
// Unselect bmpSmall from dcSmall
// Save bmpSmall to .bmp file.

i am trying to get started but as i am relatively new to visual c programming, esp in graphics, i realli appreciate the patient advice tat u guys been provideing.. thks

Marc G
May 5th, 2006, 03:08 PM
Hi Marc,
CBitmap bmpBig;
// Load big bmp into bmpBig

--> do u mean use loadBitmap?Yes, you can use LoadBitmap.

CDC dcBig;
CBitmap* pOldBmp = dcBig.SelectObject(&bmpBitmap);
// Create dcBig and select bmpBig into it.

--> wats the purpose of the above statement? dcBig is a Device Context, so are we trying to create a DC then we load our desired bmp file onto it?We first create a device context. When a device context is created windows will automatically put a 1x1 monochrome bitmap in it. If you want to do something more with your device context you need to select another bitmap into it and this is what is happening above.


CBitmap bmpSmall;
// Create empty small bitmap with correct dimensions
CDC dcSmall;
// Create dcSmall and select bmpSmall into it.


--> are the codes for small bitmap same as the big bmp?bmpSmall is the target bitmap. You create an empty bitmap with the function bmpSmall.CreateBitmap or bmpSmall.CreateCompatibleBitmap. See MSDN for an explanation of these functions.


// use bitblt to blit a rectangle from dcBig to dcSmall.
// Unselect bmpSmall from dcSmall
// Save bmpSmall to .bmp file.

bitblt is to copy a bmp rite? y is there a need to unselect bmpSmall from dcSmall? wats the difference between the 2?Yes, bitblt is to copy part of a DC to the same DC or another DC. Everytime you use SelectObject to select something into a DC (a bitmap, bruch, pen, ...) you need to use SelectObject to select the original object back into the DC before the DC gets destroyed. This is the way Windows requires you to do it.

CBitmap bmpSmall2;
// Create empty small bitmap with correct dimensions
CDC dcSmall2;
// Create dcSmall and select bmpSmall into it.

// use bitblt to blit a rectangle from dcBig to dcSmall.
// Unselect bmpSmall from dcSmall
//Save bmpSmall to .bmp file.

--> wats the purpose of bmpSmall2??That was just to show you that you can create a new dc/bitmap to copy another part of the big image. Note: you can reuse the DC from dcSmall and if the small bitmaps are equal in size, you can reuse bmpSmall.

I recommend you to take a look at the articles at http://www.codeguru.com/Cpp/G-M/bitmap/ to learn how to work with graphics in Windows.

fulltime
May 8th, 2006, 09:01 PM
thks marc, i am almost there.. will post here if i have any further queries.. thks alot...

FT :)

Marc G
May 9th, 2006, 02:53 PM
You're welcome :)