关于生成位图的程序

       GLint nVp0[4] = {0};
       GLint nVp1[4] = {0}; 
      glRasterPos3f(lftBtmPt.x, lftBtmPt.y, lftBtmPt.z);
      glGetIntegerv(GL_CURRENT_RASTER_POSITION, nVp0);
      glRasterPos3f(rgtTopPt.x, rgtTopPt.y, rgtTopPt.z);
      glGetIntegerv(GL_CURRENT_RASTER_POSITION, nVp1);


     uint unWidth = (((abs(nVp1[0] - nVp0[0]) * 24 + 15) / 16) * 2) / 3;
     uint unHeight = abs(nVp1[1] - nVp0[1]);


    Byte *pTextDat = new Byte[unHeight * unWidth * 4];
    ZeroMemory(pTextDat, unHeight * unWidth * 4 * sizeof(Byte));
   //glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glReadPixels(nVp0[0], nVp0[1], unWidth, unHeight, GL_RGBA, GL_UNSIGNED_BYTE, pTextDat);


   Byte *pDat = new  Byte[unHeight * unWidth * 3];
 ZeroMemory(pDat, unHeight * unWidth * 3 * sizeof(Byte));
 uint j = 0;
 for(uint i = 0; i < unHeight * unWidth * 4; i++)
 {
  if(i%4 == 3)
   continue;
  pDat[j++] = pTextDat[i];
  if (j > unHeight * unWidth * 3)
  {
   break;
  }
 }
 
 Save(m_sTgtExpBmpFileName, unWidth, unHeight, pDat);
       delete []pDat;

 delete []pTextDat;








#pragma comment(lib,"gdi32")
//#pragma命令可以让编程者让编译器执行某些事.
#include <windows.h>
#include <stdio.h>
 
int main() 
{
    const DWORD uWidth = 18 + 17 * 256, uHeight = 18 + 17 * 128;
//pbmi指向一个颜色表表项为2的,包含位图信息头、颜色表的bitmapinfo结构
    PBITMAPINFO pbmi = (PBITMAPINFO) LocalAlloc (LPTR, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2);


    pbmi->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
    pbmi->bmiHeader.biWidth = uWidth;
    pbmi->bmiHeader.biHeight = uHeight;
//目标设备级别,1
    pbmi->bmiHeader.biPlanes = 1;
    pbmi->bmiHeader.biBitCount = 1;
//实际位图所占的大小,&~使后四位为0,即可以被32整除
    pbmi->bmiHeader.biSizeImage = ((uWidth + 31) & ~31) / 8 * uHeight;
    pbmi->bmiColors[0].rgbBlue = 0;
    pbmi->bmiColors[0].rgbGreen = 0;
    pbmi->bmiColors[0].rgbRed = 0;
    pbmi->bmiColors[1].rgbBlue = 255;
    pbmi->bmiColors[1].rgbGreen = 255;
    pbmi->bmiColors[1].rgbRed = 255;
//This method creates a memory device context that is compatible with the device //specified by the pointer
    HDC hDC = CreateCompatibleDC (0);
//该变量接收一个指向DIB位数据值的指针
    void * pvBits;
//创建应用程序可以直接写入的、与设备无关的位图(DIB)
    HBITMAP hBitmap = CreateDIBSection (hDC, pbmi, 0, &pvBits, NULL, 0);
    SelectObject (hDC, hBitmap);
    HFONT hFont = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "宋体");
//  HFONT hFont = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, SHIFTJIS_CHARSET, 0, 0, 0, 0, "宋体");
    SelectObject (hDC, hFont);
    BitBlt (hDC, 0, 0, uWidth, uHeight, NULL, 0, 0, WHITENESS);
 
    char c[4];
    int i, j;
//第一列,
    for (i = 128; i < 256; i++) {
        sprintf (c, "%02X", i);
        TextOut (hDC, 1, (i - 127) * 17 + 1, c, 2);
    }
//第一行
    for (j = 0; j < 256; j++) {
        sprintf (c, "%02X", j);
        TextOut (hDC, (j + 1)* 17 + 1, 1, c, 2);
    }
    for (i = 128; i < 256; i++) {
        for (j = 0; j < 256; j++) {
            c[0] = (char) i;
            c[1] = (char) j;
            TextOut (hDC, (j + 1) * 17 + 1, (i - 127) * 17 + 1, c, 2);
        }
    }
    for (i = 0; i < 130; i++) {
        MoveToEx (hDC, 0, i * 17, NULL);
        LineTo (hDC, uWidth, i * 17);
    }
    for (j = 0; j < 258; j++) {
        MoveToEx (hDC, j * 17, 0, NULL);
        LineTo (hDC, j * 17, uHeight);
    }
 
    BITMAPFILEHEADER bmfh;
    bmfh.bfType = *(PWORD) "BM";
    bmfh.bfReserved1 = 0;
    bmfh.bfReserved2 = 0;
    bmfh.bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2;
    bmfh.bfSize = bmfh.bfOffBits + pbmi->bmiHeader.biSizeImage;
 
    HANDLE hFile = CreateFile ("goal.bmp", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
    if (hFile != INVALID_HANDLE_VALUE) {
        DWORD dwWritten;
        WriteFile (hFile, &bmfh, sizeof (BITMAPFILEHEADER), &dwWritten, NULL);
        WriteFile (hFile, pbmi, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2, &dwWritten, NULL);
        WriteFile (hFile, pvBits, pbmi->bmiHeader.biSizeImage, &dwWritten, NULL);
 
        CloseHandle (hFile);
    }
 
    DeleteObject (hFont);
    DeleteObject (hBitmap);
    DeleteDC (hDC);
    LocalFree (pbmi);
 
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值