Visual C++位元影像操作

來源:互聯網
上載者:User
一.BitBlt

將一幅位元影像從一個裝置情境複製到另一個,即複製像素,前面參數為目標,後者為源

case   WM_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) ;               return 0 ;
二.展開位元影像(會使圖片不清晰)

使用StretchBlt函數,比BitBlt多了兩個參數

case   WM_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) ;               return 0 ;
三.建立位元影像

3.1

hBitmap = CreateCompatibleBitmap (hdc, cx, cy) ;         //此函數建立了一個與裝置相容的位元影像
hBitmap CreateBitmapIndirect (&bitmap) ;//通過結構體建立
  1. 先LoadBitmap 載入位元影像
  2. 然後建立CreateCompatibleDC
  3. BitBlt 拷貝像素
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用位元影像建立文字,用0和1表示,相當於畫像素點的意思.

填充BITMAP的bmBits欄位

 static BITMAP bitmap = {   0, 8, 8, 2, 1, 1 } ; static BYTE                   bits [8][2]={ 0xFF, 0, 0x0C, 0, 0x0C, 0, 0x0C, 0,                                               0xFF, 0, 0xC0, 0, 0xC0, 0, 0xC0, 0 } ; static HBITMAP hBitmap ; static int                    cxClient, cyClient, cxSource, cySource ; HDC                           hdc, hdcMem ; int                           x, y ; PAINTSTRUCT                  ps ;                    switch (message)        {         case   WM_CREATE:                        bitmap.bmBits = bits ;                        hBitmap       = CreateBitmapIndirect (&bitmap) ;                        cxSource      = bitmap.bmWidth ;                        cySource      = bitmap.bmHeight ;                        return 0 ;
3.3使用位元影像建立筆刷
hBitmap = LoadBitmap (hInstance, TEXT ("Bricks"));hBrush = CreatePatternBrush (hBitmap);DeleteObject (hBitmap);
3.4在位元影像中繪圖

用CreateCompatibleBitmap 建立一幅與裝置相容有關位元影像,然後選擇位元影像,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));

建立好以後就可以同上方法用BitBlt或者StretchBlt方法操作像素了

四.菜單插入位元影像
hBitmap = StretchBitmap (LoadBitmap (hInstance, TEXT ("BitmapFont"))) ;AppendMenu (hMenu, MF_BITMAP | MF_POPUP, (int) hMenuPopup,                   (PTSTR) (LONG) hBitmap) ;
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.