Visual c ++ bitmap operations

Source: Internet
Author: User
I. bitblt

Copy a bitmap from one device scenario to another, that is, copy pixels. The first parameter is the target, and the latter is the source.

 
CaseWm_paint: hdcclient = beginpaint (hwnd, & PS); hdcwindow = getwindowdc (hwnd );For(Y = 0; y <cyclient; y + = cysource)For(X = 0; x <cxclient; x + = cxsource) {bitblt (hdcclient, X, Y, cxsource, cysource, hdcwindow, 0, 0, srccopy );} releasedc (hwnd, hdcwindow); endpaint (hwnd, & PS );Return0;
2. Stretch the bitmap (making the image unclear)

The stretchblt function has two more parameters than bitblt.

CaseWm_paint: hdcclient = beginpaint (hwnd, & PS); hdcwindow = getwindowdc (hwnd); stretchblt (hdcclient, 0, 0, cxclient, cyclient, hdcwindow, 0, 0, cxsource, cysource, mergecopy); releasedc (hwnd, hdcwindow); endpaint (hwnd, & PS );Return0;
3. Create a bitmap

3.1

 
Hbitmap = createcompatiblebitmap (HDC, CX, CY );// This function creates a device-compatible bitmap.
 
Hbitmap createbitmapindirect (& Bitmap );// Create a struct
    1. Load bitmap first
    2. Create createcompatibledc
    3. Bitblt copying pixels
 Switch (Message ){ Case Wm_create: hinstance = (lpcreatestruct) lparam)-> hinstance; hbitmap = loadbitmap (hinstance, text ( "Bricks" ); GetObject (hbitmap, Sizeof (Bitmap), & Bitmap); cxsource = bitmap. bmwidth; cysource = bitmap. bmheight; Return 0; Case Wm_size: cxclient = loword (lparam); cyclient = hiword (lparam ); Return 0; Case Wm_paint: HDC = beginpaint (hwnd, & PS); hdcmem = createcompatibledc (HDC); SelectObject (hdcmem, hbitmap ); For (Y = 0; y <cyclient; y + = cysource)For (X = 0; x <cxclient; x + = cxsource) {bitblt (HDC, X, Y, cxsource, cysource, hdcmem, 0, 0, srccopy );} deletedc (hdcmem); endpaint (hwnd, & PS ); Return 0; Case Wm_destroy: deleteobject (hbitmap); postquitmessage (0 ); Return 0 ;}
3.2 use bitmap to create a text, represented by 0 and 1, which is equivalent to the meaning of the pixel.

Fill in the bmbits field of Bitmap

 StaticBitmap bitmap = {0, 8, 8, 2, 1, 1 };StaticByte bits [8] [2] = {0xff, 0, 0x0c, 0, 0x0c, 0, 0x0c, 0, 0xff, 0, 0xc0, 0, 0xc0, 0, 0xc0, 0 };StaticHbitmap;Static intCxclient, cyclient, cxsource, cysource; HDC, hdcmem;IntX, Y; paintstruct pS;Switch(Message ){CaseWm_create: bitmap. bmbits = bits; hbitmap = createbitmapindirect (& Bitmap); cxsource = bitmap. bmwidth; cysource = bitmap. bmheight;Return0;
3.3 Use bitmap to create a brush
Hbitmap = loadbitmap (hinstance, text ("Bricks"); Hbrush = createpatternbrush (hbitmap); deleteobject (hbitmap );
3.4 plotting in the in-place Graph

Use createcompatiblebitmap to create a bitmap related to device compatibility, and then select the bitmap, SelectObject (hdcmem, hbitmap)

 
HDC = getdc (hwnd); hdcmem = createcompatibledc (HDC); gettextextentpoint32 (HDC, sztext, lstrlen (sztext), & size); cxbitmap = size. CX; cybitmap = size. cy; hbitmap = createcompatiblebitmap (HDC, cxbitmap, cybitmap); releasedc (hwnd, HDC); SelectObject (hdcmem, hbitmap); textout (hdcmem, 0, 0, sztext, lstrlen (sztext ));

After creation, you can use bitblt or stretchblt to operate pixels in the same method as above.

Iv. Menu insert bitmap
  hbitmap = stretchbitmap (loadbitmap (hinstance, text ( "bitmapfont"  ))); appendmenu (hmenu, mf_bitmap | mf_popup, ( int ) hmenupopup, (ptstr) (long) hbitmap);  

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.