我有一段内存数据,是我自己画的位图数据,纯数据。
想用GDI显示在屏幕上。请大家指点

解决方案 »

  1.   


    //Code come from FreeCL 2.00 for skinning
    void DrawBitmap(HDC dc, HBITMAP bitmap, const RECT& cRect, int xClient = 0, int yClient = 0)
    {
    BITMAP bInfo;
    memset(&bInfo, 0, sizeof(bInfo));
    ::GetObject(bitmap, sizeof(bInfo), &bInfo);
    int iWidth = (int)(bInfo.bmWidth);
    int iHeight = (int)(bInfo.bmHeight);//iWidth & iHeight is width & height of Image   

    HDC tempDC = ::CreateCompatibleDC(dc);
    HGDIOBJ iBitmap = ::SelectObject(tempDC, (HGDIOBJ)bitmap);
     
    int cHeight = cRect.bottom - cRect.top;
    int cTop = cRect.top;
    int y = yClient+cTop;
    while(y<0)
    y += iHeight;
    int imageY = y % iHeight;
    int uOffsetY = cTop;
    int uHeight = iHeight-imageY;
    if(uHeight > cHeight)
    uHeight = cHeight;
    while(uHeight > 0)
    { //Y axis loop
    int cWidth =  cRect.right - cRect.left;
    int cLeft  = cRect.left;
    int x = xClient+cLeft;
    while(x < 0)
    x += iWidth;
    int imageX = x % iWidth;
    int uOffsetX = cLeft;
    int uWidth = iWidth - imageX;
    if(uWidth > cWidth)
    uWidth = cWidth;
    while(uWidth > 0)
    { //X axis loop
    ::BitBlt(dc, uOffsetX, uOffsetY, uWidth, uHeight, tempDC, imageX, imageY, SRCCOPY); cWidth -= uWidth;
    if(cWidth <= 0)
    break;
    cLeft += uWidth;
    x = xClient+cLeft;
    while(x < 0)
    x += iWidth;
    imageX = x % iWidth;
    uOffsetX = cLeft;
    uWidth = iWidth - imageX;  
    if(uWidth > cWidth)
    uWidth = cWidth;
    } cHeight -= uHeight;
    if(cHeight <= 0)
    break;
    cTop += uHeight;
    y = yClient+cTop;
    while(y < 0)
    y += iHeight;
    imageY = y % iHeight;
    uOffsetY = cTop;
    uHeight = iHeight-imageY;
    if(uHeight > cHeight)
    uHeight = cHeight;
    } ::SelectObject(tempDC, iBitmap);
    ::DeleteDC(tempDC);
    }
      

  2.   

    bitblt我知道,但是那是从dc拷到dc,
    我不懂的是怎么从内存数据-》位图-》dc
      

  3.   

    那就用这个函数,使用前需要填充BITMAPINFO结构
    int SetDIBitsToDevice(
      HDC hdc,                 // handle to DC
      int XDest,               // x-coord of destination upper-left corner
      int YDest,               // y-coord of destination upper-left corner 
      DWORD dwWidth,           // source rectangle width
      DWORD dwHeight,          // source rectangle height
      int XSrc,                // x-coord of source lower-left corner
      int YSrc,                // y-coord of source lower-left corner
      UINT uStartScan,         // first scan line in array
      UINT cScanLines,         // number of scan lines
      CONST VOID *lpvBits,     // array of DIB bits
      CONST BITMAPINFO *lpbmi, // bitmap information
      UINT fuColorUse          // RGB or palette indexes
    );
      

  4.   

    哎。越说越乱。我把目前的代码放上来,大家看看有啥问题,static unsigned char bmpdata[1024*1024*3];
    static HBITMAP rgb_2_bitmap(unsigned char* src, HDC dc)
    {
    int w,h;
    BITMAPINFOHEADER   bmih;   
    BITMAPINFO   bmi;    h = sys_height();
    w = sys_width(); memset(&bmih,0,sizeof(BITMAPINFOHEADER));   
    bmih.biSize   =   sizeof(BITMAPINFOHEADER);   
    bmih.biWidth  =   w;   
    bmih.biHeight =   h;   
    bmih.biPlanes =   1;   
    bmih.biSizeImage = h*w*3;
    bmih.biBitCount =   24;   
    bmih.biCompression =   BI_RGB;    memset(&bmi,0,sizeof(BITMAPINFO));   
    bmi.bmiHeader =   bmih;    return CreateDIBitmap(dc, &bmih, CBM_INIT, bmpdata, &bmi, DIB_RGB_COLORS);   
    }static void draw(void)
    {
    HDC dc;
    HBITMAP bitmap;
    HDC memDC; int w, h;
    unsigned char* tmp; h = sys_height();
    w = sys_width(); dc = GetDC(hwnd);
            memDC = CreateCompatibleDC (dc);
    bitmap = rgb_2_bitmap(tmp, dc);
            SelectObject (memDC, bitmap);
    memset(bmpdata, 0x75, 4096); h = BitBlt(dc, 0, 0, w, h, memDC, 0, 0, SRCCOPY);

    DeleteDC(memDC);
    DeleteObject(bitmap);
    ReleaseDC(hwnd, dc);
    }
      

  5.   

    5楼的兄弟,看了help,觉得你这个函数是最简单的,我写了下面代码,但是显示出来都是白色,没有我画的东西 memset(&bi, 0, sizeof(BITMAPINFO));
    bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bi.bmiHeader.biWidth = w;
    bi.bmiHeader.biHeight = h;
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 24;
    bi.bmiHeader.biCompression = BI_RGB;
    bi.bmiHeader.biSizeImage = w * h *3;
     
    SetDIBitsToDevice(dc, 0, 0, w, h, 0, h-1, 0, h, buf, &bi, DIB_RGB_COLORS);