Tips from http://blog.csdn.net/maozefa/article/details/4533770 for image processing with a scan line of GDI + bitmap data

Source: Internet
Author: User
Tags bmp image

Tips for using GDI + bitmap data scanning lines to process images: C/C ++ 2071 people read comments (4) collect reports

In GDI + image processing, we often use the bitmapdata structure to scan the line of image data. This method is used in most of my Bolg articles. Using its lockbits method and unlockbits method, GDI + bitmap is used to lock (obtain) and unlock (release) bitmapdata data respectively. We generally operate the scanned line of image data between these two methods, for example:

 

[CPP]View plaincopyprint?

  1. Bitmap * BMP = new Bitmap (l "d: // 001-1.jpg ");
  2. Bitmapdata data;
  3. Gdiplus: rect R (0, 0, BMP-> getwidth (), BMP-> getheight ());
  4. // The lock is in 32-bit pixel format.
  5. BMP-> lockbits (& R, imagelockmoderead | imagelockmodewrite, pixelformat32bppargb, & data );
  6. // Scan the image data line here. Many methods cannot be called because BMP is locked.
  7. BMP-> unlockbits (& data); // unlock
  8. Gdiplus: Graphics * g = new gdiplus: Graphics (canvas-> handle );
  9. G-> drawimage (BMP, 0, 0 );
  10. Delete g;
  11. Delete BMP;

 

Because the bitmap object is locked between the lockbits method and the unlockbits method, many methods cannot be called, and sometimes it is inconvenient or even cumbersome. For example, if you want to display or save the scanned lines of image data multiple times, you have to call these two methods repeatedly; in addition, images with a bitmap format lower than the 24-bit format cannot be locked to 24-bit or 32-bit data for operations (we mostly use 24-bit or 32-bit pixel scanning lines for image processing.

You can use some tips to avoid the inconvenience caused by the locking of Bitmap objects. You can also scan images of 24 or 32 bits or smaller than 24 bits. See the following example:

 

[CPP]View plaincopyprint?

  1. Gdiplus: Graphics * g = new gdiplus: Graphics (canvas-> handle );
  2. Bitmap * BMP = new Bitmap (l "d: // 001-1.jpg ");
  3. Bitmapdata data;
  4. Gdiplus: rect R (0, 0, BMP-> getwidth (), BMP-> getheight ());
  5. // Pre-set Scan Line Length and Image Data
  6. Data. stride = R. Width * 4;
  7. Data. scan0 = (void *) New char [R. Height * Data. stride];
  8. // Create a 32-bit Custom Data bitmap object
  9. Bitmap * BMP 2 = new Bitmap (R. Width, R. Height, Data. stride,
  10. Pixelformat32bppargb, (byte *) data. scan0 );
  11. // Use the imagelockmoderead tag to copy BMP image data to data. scan0.
  12. // Use imagelockmodeuserinputbuf to mark and lock the image BMP so that BMP and BMP 2 Share the image data.
  13. // Use the imagelockmodewrite mark to synchronize BMP and BMP 2, that is, when Image Data Processing
  14. // Apply to BMP and BMP 2. After imagelockmodewrite is removed, the BMP image remains unchanged,
  15. // Remove imagelockmodewrite from images in less than 24 bits.
  16. BMP-> lockbits (& R, imagelockmoderead | imagelockmodewrite | imagelockmodeuserinputbuf,
  17. Pixelformat32bppargb, & data );
  18. // You can perform step-by-step operations on the image scanning line, and also display BMP 2, or call any methods of BMP 2.
  19. Imagegray (& data); // grayscale images. The specific process is omitted.
  20. G-> drawimage (BMP 2, 0, 0); // draw an image (or call other methods)
  21. Imagetwovalues (& Dada, 127); // binarization of images. The specific process is omitted.
  22. G-> drawimage (BMP 2, 200, 0); // draw an image (or call other methods)
  23. BMP-> unlockbits (& data );
  24. Delete g;
  25. Delete BMP;
  26. Delete BMP 2;
  27. Delete [] data. scan0; // must be released

 

The code in the above example is described in detail and will not be explained.

The code in the above example Seems messy to explain the bitmap object sharing and data processing synchronization.Remember: In this example, after the custom bitmap object BMP 2 acquires data through the BMP-> lockbits method, BMP can be unlocked or even deleted without special requirements, in this case, you no longer need to lock BMP 2. You can change the image data contained in BMP 2 by processing the data.

 

Re-plan the code above to make it clearer:

 

[CPP]View plaincopyprint?

  1. Bool getbitmapdata (wchar * filename, pixelformat, bitmapdata * Data)
  2. {
  3. Bitmap * BMP = new Bitmap (filename );
  4. If (BMP-> getlaststatus ()! = OK)
  5. Return false;
  6. Gdiplus: rect R (0, 0, BMP-> getwidth (), BMP-> getheight ());
  7. Uint pixelsize = getpixelformatsize (pixelformat );
  8. Data-> stride = (pixelsize * R. Width + 31) & 0xffffffe0)> 3;
  9. Data-> scan0 = (void *) New char [R. Height * Data-> stride];
  10. BMP-> lockbits (& R, imagelockmoderead | imagelockmodeuserinputbuf,
  11. Pixelformat, data );
  12. BMP-> unlockbits (data );
  13. Delete BMP;
  14. Return true;
  15. }
  16. Void _ fastcall tform2: button1click (tobject * sender)
  17. {
  18. // Obtain the 24-bit pixel data of the image
  19. Bitmapdata data;
  20. If (! Getbitmapdata (l "d: // 001-1.jpg", pixelformat24bpprgb, & Data ))
  21. Return;
  22. // Create a user-defined data bitmap object in 24-bit pixel format
  23. Bitmap * BMP = new Bitmap (data. Width, Data. Height, Data. stride,
  24. Data. pixelformat, (byte *) data. scan0 );
  25. Gdiplus: Graphics * g = new gdiplus: Graphics (canvas-> handle );
  26. // Scan lines of image data can be operated here, so you do not have to lock BMP, and you can also display BMP, or call any methods of BMP.
  27. Imagegray (& data); // grayscale images. The specific process is omitted.
  28. G-> drawimage (BMP, 0, 0); // draw an image (or call other methods)
  29. Imagetwovalues (& Dada, 127); // binarization of images. The specific process is omitted.
  30. G-> drawimage (BMP, 200, 0); // draw an image (or call other methods)
  31. Delete g;
  32. Delete BMP;
  33. Delete [] data. scan0; // must be released
  34. }

 

Through the code above, we can see that after getbitmapdata is called, all the image data information is included in the bitmapdata structure, so we can perform any operation on this data structure, instead of relying on any GDI + object, this avoids the inconvenience mentioned above. To create a custom data bitmap object, you only need to use it for image display, storage, and other operations.

 

You can also perform the puzzle operation by using code similar to the previous example:

 

[CPP]View plaincopyprint?

  1. Gdiplus: Graphics * g = new gdiplus: Graphics (canvas-> handle );
  2. Bitmap * BMP = new Bitmap (l "d: // 001-1.jpg ");
  3. Bitmapdata data;
  4. Gdiplus: rect R (0, 0, BMP-> getwidth (), BMP-> getheight ());
  5. // Pre-set the scanning line length and image data. The scanning line is the width of two images.
  6. Data. stride = R. Width * 2*4;
  7. Data. scan0 = (void *) New char [R. Height * Data. stride];
  8. // Copy the image to the left. Note that only data is read here, So imagelockmodewrite is not used.
  9. BMP-> lockbits (& R, imagelockmoderead | imagelockmodeuserinputbuf,
  10. Pixelformat32bppargb, & data );
  11. BMP-> unlockbits (& data );
  12. // Splice the image to the right. to simplify the image, use the same image.
  13. // If the image size is different, as long as the size of the r is the same (cannot be larger than the source image)
  14. (Char *) data. scan0 + = (R. Width * 4); // The scanned line address moves to the right.
  15. BMP-> lockbits (& R, imagelockmoderead | imagelockmodeuserinputbuf,
  16. Pixelformat32bppargb, & data );
  17. BMP-> unlockbits (& data );
  18. (Char *) data. scan0-= (R. Width * 4); // restore the scanned line address
  19. Data. width = R. Width * 2; // reset the tile width.
  20. // The tiled graph data information is included in data. You can continue to perform any operations on data.
  21. // Create a custom data bitmap object
  22. Bitmap * BMP 2 = new Bitmap (data. Width, Data. Height, Data. stride,
  23. Data. pixelformat, (byte *) data. scan0 );
  24. // Display the spliced image.
  25. G-> drawimage (BMP 2, 0, 0 );
  26. Delete g;
  27. Delete BMP;
  28. Delete BMP 2;
  29. Delete [] data. scan0; // must be released

 

The code in this article uses the BCB compiler. If there is an error, you can send a letter to correct, and please make suggestions: maozefa@hotmail.com

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.