About cropping images with VC

xiaoxiao2021-03-06  49

Due to the user's requirements, the image needs to be acquired, the image that can be collected has a large part of the black border, the user needs to cut it, which is difficult to come to me, I have found a lot of information, and there is no current thing available (haha, China) The programmer is like this!), Including 9CBS website, CodeProject, Search, also Google, did not find a very suitable function or library, several examples of online, about image display, rotation, zoom I saw it, I still didn't understand, (mainly, the structure of the image did not make it), then I took a VC digital image processing, mainly for bitmaps' basic format and structure, to clear After that, the information used to read the sequence of the program is basically successful (read the color image, no palette). On this basis, I want to copy a region in the source image and save it, but this is a problem, I have two days, I still have to copy it (mainly for the pixel distribution of bitmaps. Clear), the copy is also a mess, and then I read a CDIB (this is also a letter called Laiyiling in the jar, thank you here!), There is a function about Rotate Let me suddenly open, let me post the results of these two days! ! ! // For 24-color bitmap, cropping // psrcfile source bit diagram file // PDESTFILE target bitmap file // RECT To copy Rectangular area BOOL CGDIDLG :: CropbitMap (LPTSTSTSTSTSTSTSTSTSTSTSTSTSTSTFILE, LPRECT RECT) {CFile File; CfileException Fe; Bool Ret = FALSE;

IF (! file.open) // Open source bitmap return ret; cfile f; cfileException E; if (! f.open (pdestfile, cfile :: modecreate | CFILE: :: MODECREATE | CFILE :: ModeWrite, & E) // The bitmap file Return False to copy;

DWord Retlen = 0; dword dwbitssize = file.getLength (); // file length

Hglobal HimageBuf = GLOBALLOC (GMEM_MOVEABLE | GMEM_ZEROINIT, DWBITSSIZE); if (! Himagebuf) {// Allocation Failback Returns file.close (); Return False;}

LPSTR lpImage = (LPSTR) GlobalLock (hImageBuf); // read into the image memory retLen = file.Read (lpImage, dwBitsSize); if (retLen

// bitmap information analysis, int len ​​= sizeof (BITMAPFILEHEADER); LPBITMAPFILEHEADER lpFhdr = (LPBITMAPFILEHEADER) lpImage; // file header information LPBITMAPINFOHEADER lpbmpInfo = (LPBITMAPINFOHEADER) (lpImage sizeof (BITMAPFILEHEADER)); // bitmap header int NLineBytes = 0, NHEight = 0, nwidth = 0; NHEIGHT = lpbmpinfo-> biheight; // Source image high nwidth = lpbmpinfo-> biwidth; // Source image wide

int rectWidth = rect-> right-rect-> left; int rectHeight = rect-> bottom-rect-> top; // allocate an additional memory, storage size is rectWidth * rectHeight image pixel HGLOBAL hTmpBuf = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT RectWidth * RectHeight * 3); lpstr lppic = (lpstr) Globalock (HTMPBUF);

// The integer bit of less than 4 per line is complement IF ((lpbmpinfo-> biwidth% 4! = 0) {nLineBytes = lpbmpinfo-> biwidth (lpbmpinfo-> biwidth)% 4;} else nlinebytes = lpbmpinfo-> biwidth;

// Copy the original image to the newly allocated image storage area for (int y = 0; y = Rect-> Left && x < RECT-> Right) {IF ((Y> = Rect-> Top && Y BOTTOM)) {/ source image system pointer (LPIMAGE LPFHDR-> BFOFFBITS) // Number of bytes per row * 3 (NLINEBYTES * Y * 3) // Now the offset (x * 3) * lppic = * (LPIMAGE LPFHDR-> BFOFFBITS NLINEBYTES * Y * 3 X * 3); lppic ; // * LPPIC = * (LPIMAGE LPFHDR-> BFOFFBITS NLINEBYTES * Y * 3 X * 3 1); lppic ; // * lppic = * (LPIMAGE LPFHDR-> BFOFFBITS NLINEBYTES * Y * 3 X * 3 2); LPPC ;}}}}

// Transfer the pointer back to the start position LPPIC = LPPIC-RectWidth * RectHeight * 3;

// Storage bitmap // In addition to the height, width, other information of the original image is not changed lpbmpinfo-> biWidth = RectWidth; lpbmpinfo-> biheight = RectHeight;

F.Write (lpfhdr, len); // write file headfrite F.Write (LPBMPINFO, SIZEOF (BitMapInfoHeader); // Write diagram information F.Write (LPPIC, RectWidth * RectHeight * 3); // Write copied Image pixel f.close (); globalunlock (htmpbuf); // Release GlobalFree (HTMPBUF);

GlobalUnlock (HimageBuf); // Release GlobalFree (HimageBuf); file.close (); return true;} The above function is still very imperfect, please praise more advice! ! ! ! However, I think it is very helpful for beginners for VC processing images, so I posted this contest. ! ! The main reference materials and websites are as follows: 1, www.9cbs.net (Image Processing and Algorithm Edition) 2, www.codeproject.com 3, www.vckbase.com 4, 〖VC Digital Image Processing〗 Second Edition I contact : LargeAndsmall@163.com, welcome more advice! ! !

转载请注明原文地址:https://www.9cbs.com/read-81872.html

New Post(0)