`
hzx535lb
  • 浏览: 18074 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Bitmap简单操作-平移旋转缩放(VC++/Windows SDK)

阅读更多

Bitmap简单操作-平移旋转缩放(VC++/Windows SDK)
2010年10月30日
  #include  
  #include  
  #include  
  #include  PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp); 
  void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, 
  HBITMAP hBMP, HDC hDC) ; LRESULT CALLBACK WindowProc( 
  HWND hwnd,      // handle to window 
  UINT uMsg,      // message identifier 
  WPARAM wParam,  // first message parameter 
  LPARAM lParam   // second message parameter 
  ); void    translate(HWND hwnd,LPCTSTR lpszName,int x,int y) 
  { 
  HANDLE h0=LoadImage(NULL,lpszName,IMAGE_BITMAP,0,0,LR_LOA DFROMFILE); 
  BITMAP bmp0; 
  GetObject(h0,sizeof(BITMAP),&bmp0); 
  HDC dc0=CreateCompatibleDC(GetDC(hwnd)); 
  SelectObject(dc0,h0);     HANDLE h; 
  HDC hdc,dc; 
  BITMAP bmp;     h=LoadImage(NULL,lpszName,IMAGE_BITMAP,0,0,LR_LOAD FROMFILE);//打开图片C:\a.bmp并得到句柄
  GetObject(h,sizeof(BITMAP),&bmp);//得到图片的宽,高等信息,储存在bmp对象 
  hdc=GetDC(hwnd);//得到窗口的DC(hWnd是窗口句柄) 
  dc=CreateCompatibleDC(hdc);//得到与窗口DC兼容的DC 
  SelectObject(dc,h);//把得到的DC与图片句柄关联起来 
  //BitBlt(hdc,0,0,bmp.bmWidth,bmp.bmHeight,dc,0,0,S RCCOPY);//把图片画在窗体上
  for(int i=0;i删除CreateCompatibleDC得到的图片DC 
  ReleaseDC(hwnd,hdc);//释放GetDC得到的DC
  DeleteObject(h);//删除内存中的位图 
  }
  void    rotate(HWND hwnd,LPCTSTR lpszName,int x,int y,float theta) 
  { 
  HANDLE h0=LoadImage(NULL,lpszName,IMAGE_BITMAP,0,0,LR_LOA DFROMFILE); 
  BITMAP bmp0; 
  GetObject(h0,sizeof(BITMAP),&bmp0); 
  HDC dc0=CreateCompatibleDC(GetDC(hwnd)); 
  SelectObject(dc0,h0);
  HANDLE h; 
  HDC hdc,dc; 
  BITMAP bmp; 
  int max=bmp0.bmHeight; 
  if(bmp0.bmHeight>max) 
  { 
  max=bmp0.bmHeight; 
  } 
  max*=3; 
  h=LoadImage(NULL,lpszName,IMAGE_BITMAP,max,max,LR_ LOADFROMFILE);//打开图片C:\a.bmp并得到句柄
  GetObject(h,sizeof(BITMAP),&bmp);//得到图片的宽,高等信息,储存在bmp对象 
  hdc=GetDC(hwnd);//得到窗口的DC(hWnd是窗口句柄) 
  dc=CreateCompatibleDC(hdc);//得到与窗口DC兼容的DC 
  SelectObject(dc,h);//把得到的DC与图片句柄关联起来 
  //BitBlt(hdc,0,0,bmp.bmWidth,bmp.bmHeight,dc,0,0,S RCCOPY);//把图片画在窗体上
  for(int i=0;i删除CreateCompatibleDC得到的图片DC 
  ReleaseDC(hwnd,hdc);//释放GetDC得到的DC
  DeleteObject(h);//删除内存中的位图 
  }
  void scale() 
  {
  }
  void show(HWND hwnd,LPCTSTR lpszName) 
  { 
  HANDLE h; 
  HDC hdc,dc; 
  BITMAP bmp;
  h=LoadImage(NULL,lpszName,IMAGE_BITMAP,0,0,LR_LOAD FROMFILE);//打开图片C:\a.bmp并得到句柄
  GetObject(h,sizeof(BITMAP),&bmp);//得到图片的宽,高等信息,储存在bmp对象
  hdc=GetDC(hwnd);//得到窗口的DC(hWnd是窗口句柄) 
  dc=CreateCompatibleDC(hdc);//得到与窗口DC兼容的DC 
  SelectObject(dc,h);//把得到的DC与图片句柄关联起来 
  BitBlt(hdc,0,0,bmp.bmWidth,bmp.bmHeight,dc,0,0,SRC COPY);//把图片画在窗体上
  DeleteDC(dc);//删除CreateCompatibleDC得到的图片DC 
  ReleaseDC(hwnd,hdc);//释放GetDC得到的DC
  DeleteObject(h);//删除内存中的位图 
  }
  int WINAPI WinMain( 
  HINSTANCE hInstance,      // handle to current instance 
  HINSTANCE hPrevInstance,  // handle to previous instance 
  LPSTR lpCmdLine,          // command line 
  int nCmdShow              // show state 
  ) 
  { 
  WNDCLASS wndcls; 
  wndcls.cbClsExtra=0; 
  wndcls.cbWndExtra=0; 
  wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_ BRUSH); 
  wndcls.hCursor=LoadCursor(NULL,IDC_CROSS); 
  wndcls.hIcon=LoadIcon(NULL,IDI_ERROR); 
  wndcls.hInstance=hInstance; 
  wndcls.lpfnWndProc=WindowProc; 
  wndcls.lpszClassName=_T("MyWndClass"); 
  wndcls.lpszMenuName=NULL; 
  wndcls.style=CS_HREDRAW | CS_VREDRAW; 
  RegisterClass(&wndcls); 
  HWND hwnd; 
  hwnd=CreateWindow(_T("MyWndClass"),_T("MyWnd"),WS_ OVERLAPPEDWINDOW,0,0,700,700,NULL,NULL,hInstance,NU LL); 
  ShowWindow(hwnd,SW_SHOWNORMAL); 
  UpdateWindow(hwnd);
  //translate(hwnd,_T("lena.bmp"),100,100); 
  rotate(hwnd,_T("lena.bmp"),100,100,2); 
  show(hwnd,_T("out.bmp")); 
  MSG msg; 
  while(GetMessage(&msg,NULL,0,0)) 
  { 
  TranslateMessage(&msg); 
  DispatchMessage(&msg); 
  } 
  return 0; 
  }
  LRESULT CALLBACK WindowProc( 
  HWND hwnd,      // handle to window 
  UINT uMsg,      // message identifier 
  WPARAM wParam,  // first message parameter 
  LPARAM lParam   // second message parameter 
  ) 
  { 
  HDC hdc; 
  switch(uMsg) 
  { 
  case WM_CHAR: 
  char szChar[20]; 
  sprintf(szChar,"char is %d",wParam); 
  MessageBoxA(hwnd,szChar,"MyMessageBox",0); 
  break; 
  case WM_LBUTTONDOWN: 
  MessageBox(hwnd,_T("mouse clicked"),_T("MyMessageBox"),0); 
  hdc=GetDC(hwnd); 
  TextOut(hdc,0,50,_T("mouse clicked"),strlen(("mouse clicked"))); 
  ReleaseDC(hwnd,hdc); 
  break; 
  case WM_PAINT: 
  PAINTSTRUCT ps; 
  hdc=BeginPaint(hwnd,&ps); 
  TextOut(hdc,0,0,_T("mouse clicked"),strlen(("mouse clicked"))); 
  EndPaint(hwnd,&ps); 
  break; 
  case WM_CLOSE: 
  if(IDYES==MessageBox(hwnd,_T("exit or not?"),_T("MyMessageBox"),MB_YESNO)) 
  { 
  DestroyWindow(hwnd); 
  } 
  break; 
  case WM_DESTROY: 
  PostQuitMessage(0); 
  break; 
  default: 
  return DefWindowProc(hwnd,uMsg,wParam,lParam); 
  } 
  return 0; 
  }
  PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp) 
  { 
  BITMAP bmp; 
  PBITMAPINFO pbmi; 
  WORD    cClrBits; 
  // Retrieve the bitmap's color format, width, and height. 
  if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp)) ; 
  //errhandler("GetObject", hwnd); 
  // Convert the color format to a count of bits. 
  cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); 
  if (cClrBits == 1) 
  cClrBits = 1; 
  else if (cClrBits bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
  pbmi->bmiHeader.biWidth = bmp.bmWidth; 
  pbmi->bmiHeader.biHeight = bmp.bmHeight; 
  pbmi->bmiHeader.biPlanes = bmp.bmPlanes; 
  pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel; 
  if (cClrBits bmiHeader.biClrUsed = (1bmiHeader.biCompression = BI_RGB; 
  // Compute the number of bytes in the array of color 
  // indices and store the result in biSizeImage. 
  // For Windows NT/2000, the width must be DWORD aligned unless 
  // the bitmap is RLE compressed. This example shows this. 
  // For Windows 95/98, the width must be WORD aligned unless the 
  // bitmap is RLE compressed. 
  pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8 
  * pbmi->bmiHeader.biHeight; 
  // Set biClrImportant to 0, indicating that all of the 
  // device colors are important. 
  pbmi->bmiHeader.biClrImportant = 0; 
  return pbmi; 
  } 
  void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, 
  HBITMAP hBMP, HDC hDC) 
  { 
  HANDLE hf;                 // file handle 
  BITMAPFILEHEADER hdr;       // bitmap file-header 
  PBITMAPINFOHEADER pbih;     // bitmap info-header 
  LPBYTE lpBits;              // memory pointer 
  DWORD dwTotal;              // total count of bytes 
  DWORD cb;                   // incremental count of bytes 
  BYTE *hp;                   // byte pointer 
  DWORD dwTmp; 
  pbih = (PBITMAPINFOHEADER) pbi; 
  lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
  if (!lpBits) ; 
  //errhandler("GlobalAlloc", hwnd); 
  // Retrieve the color table (RGBQUAD array) and the bits 
  // (array of palette indices) from the DIB. 
  if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, 
  DIB_RGB_COLORS)) 
  { 
  //errhandler("GetDIBits", hwnd); 
  }
  // Create the .BMP file. 
  hf = CreateFile(pszFile, 
  GENERIC_READ | GENERIC_WRITE, 
  (DWORD) 0, 
  NULL, 
  CREATE_ALWAYS, 
  FILE_ATTRIBUTE_NORMAL, 
  (HANDLE) NULL); 
  if (hf == INVALID_HANDLE_VALUE) ; 
  // errhandler("CreateFile", hwnd); 
  hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M" 
  // Compute the size of the entire file. 
  hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + 
  pbih->biSize + pbih->biClrUsed 
  * sizeof(RGBQUAD) + pbih->biSizeImage); 
  hdr.bfReserved1 = 0; 
  hdr.bfReserved2 = 0; 
  // Compute the offset to the array of color indices. 
  hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + 
  pbih->biSize + pbih->biClrUsed 
  * sizeof (RGBQUAD); 
  // Copy the BITMAPFILEHEADER into the .BMP file. 
  if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), 
  (LPDWORD) &dwTmp,  NULL)) 
  { 
  // errhandler("WriteFile", hwnd); 
  }
  // Copy the BITMAPINFOHEADER and RGBQUAD array into the file. 
  // Copy the BITMAPINFOHEADER and RGBQUAD array into the file. 
  if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof (RGBQUAD), (LPDWORD) &dwTmp, ( NULL) )); 
  // errhandler("WriteFile", hwnd); 
  // Copy the array of color indices into the .BMP file. 
  dwTotal = cb = pbih->biSizeImage; 
  hp = lpBits; 
  if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL)) 
  printf("WriteFile", hwnd); 
  // Close the .BMP file. 
  if (!CloseHandle(hf)) ; 
  // errhandler("CloseHandle", hwnd); 
  // Free memory. 
  GlobalFree((HGLOBAL)lpBits); 
  }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics