Visual C++位图操作(1)

 

一.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) ;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值