MFC's CImage Graphics Processing

Reference:  http://www.cnblogs.com/afarmer/archive/2012/03/31/2427273.html

Owed by: Spring Night Joyful Rainhttp://blog.csdn.net/chunyexiyu  reprint   please indicate the source 

 


There are many image formats supported by CImage, such as commonly used jpg, png, bmp, gif, etc. are well supported. (The CImage class is based on GDI+, introduced from VC.NET, not in VC 6.0.)

According to our commonly used image processing requirements, generally: image loading, image assignment to controls, image drawing, image modification, image conversion, (image creation)


1. Image loading/opening

There are three methods for CImage to load/open image loading

a. Specify the path file name to load 

C Image::load method

You can determine whether the image is loaded successfully according to the result returned by load.

b. Specify the resourceid to load. In this case, the image is imported into the resource and loaded using the id defined in the resource.

CImage::LoadFromResource

c. Bind a Bitmap, use the Attach method, bind a Bitmap, so that you can use many methods of Cimage

CImage::Attach

The graphics recorded using this method can be either DIB or DDB graphics, depending on whether the bound HBITMAP is a DIB graphics or not, the IsDIBSection judgment can be given (DIB: device-independent graphics DDB: device-dependent graphics)


2. The image is assigned to a control

Usually MFC internal controls use the HBITMAP or CBitmap structure

After we open the picture, we can use

        image.Detach() returns HBITMAP

        CDC::FromHandle(image.Detach()) 返回CBitamp


3. Picture drawing

      When a picture is drawn on the screen, a related drawing device is required, and it needs to be related to the device DC, so the first parameter of several drawing functions is CDC*

      Common drawing functions:

CImage::Draw draws

CImage::StretchBlt stretch (note that the stretch mode SetStretchBltMode is set before calling this function)

CImage::AlphaBlend color blending

Cimage::TransparentBlt draws transparently

CImage::MaskBlt 掩码绘制 Combines the color data for the source and destination bitmaps using the specified mask and raster operation. 

CImage::PlgBlt 平行四边形绘制 Performs a bit-block transfer from a rectangle in a source device context into a parallelogram in a destination device context. 


4. Image modification

There are several ways to modify image content.

A single pixel modification, using 

Cimage::SetPixelRGB

One is to modify in batches, get all Pixel pixel information, and process them in batches

CImage::GetBits gets the location of the image memory point

When using this method, you need to consider the structure of the graphics, use CImage::GetBPP to get how many bits each pixel occupies, and you need to useCImage::GetPitch obtains the number of bits in a row, and determines whether the head obtained by GetBits is the first row or the last row according to the positive or negative value of GetPitch.

If you modify it, you can modify it directly. For example, 24-bit data, usually in BGR format, the first byte of each pixel is Blue, the second byte is Green, and the third byte is Red.


5. Image format conversion/save

The Cimage class provides functions for saving to other formats:

The parameters supported by CImage::Save are

The file type to save the image as. Can be one of the following:

  • ImageFormatBMP   An uncompressed bitmap image.

  • ImageFormatPNG   A Portable Network Graphic (PNG) compressed image.

  • ImageFormatJPEG   A JPEG compressed image.

  • ImageFormatGIF   A GIF compressed image.


6. Image Creation

In addition to creating an image through the Load method, we can also create an image through the Create method, and then use the image modification method to define the information of each Pixel

Cimage::Create

CImage::CreateEx

The created image is processed using the method in 4. Image modification , and then can be saved using the Save method


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324644693&siteId=291194637