Chinaunix首页 | 论坛 | 博客
  • 博客访问: 104574259
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: C/C++

2008-04-14 23:32:32

作者:徐景周 来源:赛迪网 

19.如何转换并保存位图

//****************************************************************************
//* 名称:DDBToDIB
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:设备相关转换为设备无关位图
//****************************************************************************
HANDLE CScreenSnapDlg::DDBToDIB( CBitmap& bitmap, DWORD dwCompression /*
= BI_RGB */) 
{
    BITMAP				bm;
    BITMAPINFOHEADER    bi;
    LPBITMAPINFOHEADER  lpbi;
    DWORD				dwLen;
    HANDLE				hDIB;
    HANDLE				handle;
    HDC					hDC;
    HPALETTE			hPal;

    CWindowDC			dc( this );
    CPalette			pal;
    //如果支持调色板的话,则建立它
    if( dc.GetDeviceCaps( RASTERCAPS ) & RC_PALETTE )
    {
        UINT        nSize   = sizeof(LOGPALETTE) + ( sizeof(PALETTEENTRY) * 256 );
        LOGPALETTE* pLP     = (LOGPALETTE*)new BYTE[nSize];
        pLP->palVersion     = 0x300;
        pLP->palNumEntries = (unsigned short)GetSystemPaletteEntries( dc, 0, 255, 
        pLP->palPalEntry );

        pal.CreatePalette( pLP );

        //释放
        delete[] pLP;
    }

    ASSERT( bitmap.GetSafeHandle() );

    //不支持BI_BITFIELDS类型
    if( dwCompression == BI_BITFIELDS )
        return NULL;

    //如果调色板为空,则用默认调色板
    hPal = (HPALETTE) pal.GetSafeHandle();
    if (hPal==NULL)
        hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);

    //获取位图信息
    bitmap.GetObject(sizeof(bm),(LPSTR)&bm);

    //初始化位图信息头
    bi.biSize        = sizeof(BITMAPINFOHEADER);
    bi.biWidth        = bm.bmWidth;
    bi.biHeight         = bm.bmHeight;
    bi.biPlanes         = 1;
    bi.biBitCount        = (unsigned short)(bm.bmPlanes * bm.bmBitsPixel) ;
    bi.biCompression    = dwCompression;
    bi.biSizeImage        = 0;
    bi.biXPelsPerMeter    = 0;
    bi.biYPelsPerMeter    = 0;
    bi.biClrUsed        = 0;
    bi.biClrImportant    = 0;

    //计算信息头及颜色表大小
    int nColors = 0;
    if(bi.biBitCount <= 8)
        {
        nColors = (1 << bi.biBitCount);
        }
    dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);

    hDC = ::GetDC(NULL);
    hPal = SelectPalette(hDC,hPal,FALSE);
    RealizePalette(hDC);

    //为信息头及颜色表分配内存
    hDIB = GlobalAlloc(GMEM_FIXED,dwLen);

    if (!hDIB){
        SelectPalette(hDC,hPal,FALSE);
        ::ReleaseDC(NULL,hDC);
        return NULL;
    }

    lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

    *lpbi = bi;

    //调用 GetDIBits 计算图像大小
    GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
            (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);

    bi = *lpbi;

    //图像的每一行都对齐(32bit)边界
    if (bi.biSizeImage == 0){
        bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) &#38; ~31) / 8) 
                        * bi.biHeight;

        if (dwCompression != BI_RGB)
            bi.biSizeImage = (bi.biSizeImage * 3) / 2;
    }

    //重新分配内存大小,以便放下所有数据
    dwLen += bi.biSizeImage;
    handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE) ;
    if (handle != NULL)
        hDIB = handle;
    else
        {
        GlobalFree(hDIB);

        //重选原始调色板
        SelectPalette(hDC,hPal,FALSE);
        ::ReleaseDC(NULL,hDC);
        return NULL;
        }

    //获取位图数据
    lpbi = (LPBITMAPINFOHEADER)hDIB;

    //最终获得的DIB
    BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
                0L,                      //扫描行起始处
                (DWORD)bi.biHeight,      //扫描行数
                (LPBYTE)lpbi             //位图数据地址
                + (bi.biSize + nColors * sizeof(RGBQUAD)),
                (LPBITMAPINFO)lpbi,      //位图信息地址
                (DWORD)DIB_RGB_COLORS);  //颜色板使用RGB

    if( !bGotBits )
    {
        GlobalFree(hDIB);
        
        SelectPalette(hDC,hPal,FALSE);
        ::ReleaseDC(NULL,hDC);
        return NULL;
    }

    SelectPalette(hDC,hPal,FALSE);
    ::ReleaseDC(NULL,hDC);
    return hDIB;
}

//********************************************************************************
//* 名称:SaveBitmapToFile
//* 修改:徐景周(jingzhou_xu@163.net)
//* 功能:保存为位图文件
//********************************************************************************
BOOL CScreenSnapDlg::SaveBitmapToFile(HBITMAP hBitmap , CString lpFileName) 
{    	 
	HDC				hDC;
		//设备描述表  
    int				iBits;
	//当前显示分辨率下每个像素所占字节数
	WORD            wBitCount;
	//位图中每个像素所占字节数
	DWORD           dwPaletteSize=0, 
	//定义调色板大小, 位图中像素字节大小 ,位图文件大小 , 写入文件字节数
		dwBmBitsSize,
		dwDIBSize, dwWritten;
	BITMAP          Bitmap;        
	BITMAPFILEHEADER   bmfHdr;
	//位图属性结构    
    BITMAPINFOHEADER   bi;
    //位图文件头结构       
	LPBITMAPINFOHEADER lpbi;			
	//位图信息头结构     
    HANDLE          fh, hDib, hPal,hOldPal=NULL;	
//指向位图信息头结构,定义文件,分配内存句柄,调色板句柄
//计算位图文件每个像素所占字节数
   hDC = CreateDC("DISPLAY",NULL,NULL,NULL);
   iBits = GetDeviceCaps(hDC, BITSPIXEL) * 
   GetDeviceCaps(hDC, PLANES);
   DeleteDC(hDC);
   if (iBits <= 1)
      wBitCount = 1;
   else if (iBits <= 4)
      wBitCount = 4;
   else if (iBits <= 8)
      wBitCount = 8;
   else if (iBits <= 24)
      wBitCount = 24;
   //计算调色板大小
   if (wBitCount <= 8)
      dwPaletteSize = (1 << wBitCount) *sizeof(RGBQUAD);
   
   //设置位图信息头结构
   GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&#38;Bitmap);
   bi.biSize            = sizeof(BITMAPINFOHEADER);
   bi.biWidth           = Bitmap.bmWidth;
   bi.biHeight          = Bitmap.bmHeight;
   bi.biPlanes          = 1;
   bi.biBitCount         = wBitCount;
   bi.biCompression      = BI_RGB;
   bi.biSizeImage        = 0;
   bi.biXPelsPerMeter     = 0;
   bi.biYPelsPerMeter     = 0;
   bi.biClrUsed         = 0;
   bi.biClrImportant      = 0;

   dwBmBitsSize = ((Bitmap.bmWidth *
    wBitCount+31)/32)* 4
	 *Bitmap.bmHeight ;

   //为位图内容分配内存
   hDib  = GlobalAlloc(GHND,dwBmBitsSize+
	dwPaletteSize+sizeof(BITMAPINFOHEADER));
   lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
   *lpbi = bi;

   // 处理调色板   
   hPal = GetStockObject(DEFAULT_PALETTE);
   if (hPal)
   {
	   hDC  = ::GetDC(NULL);
	   hOldPal = SelectPalette(hDC, (HPALETTE)hPal, FALSE);
       RealizePalette(hDC);
   }

   // 获取该调色板下新的像素值
   GetDIBits(hDC, hBitmap, 0, (UINT) Bitmap.bmHeight,
	 (LPSTR)lpbi + sizeof(BITMAPINFOHEADER)+dwPaletteSize,
	 (LPBITMAPINFO)lpbi, DIB_RGB_COLORS);

   //恢复调色板   
   if (hOldPal)
   {
      SelectPalette(hDC, (HPALETTE)hOldPal, TRUE);
      RealizePalette(hDC);
      ::ReleaseDC(NULL, hDC);
   }

   //创建位图文件    
	fh = CreateFile(lpFileName, GENERIC_WRITE, 
		 0, NULL, CREATE_ALWAYS,
         FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);

   if (fh == INVALID_HANDLE_VALUE)
      return FALSE;

   // 设置位图文件头
   bmfHdr.bfType = 0x4D42;  // "BM"
   dwDIBSize    = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 
dwPaletteSize + dwBmBitsSize;  
   bmfHdr.bfSize = dwDIBSize;
   bmfHdr.bfReserved1 = 0;
   bmfHdr.bfReserved2 = 0;
   bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) 
      + (DWORD)sizeof(BITMAPINFOHEADER)
     + dwPaletteSize;
   // 写入位图文件头
   WriteFile(fh, (LPSTR)&#38;bmfHdr, sizeof
	(BITMAPFILEHEADER), &#38;dwWritten, NULL);
   // 写入位图文件其余内容
   WriteFile(fh, (LPSTR)lpbi, dwDIBSize, 
   &#38;dwWritten, NULL);
   //消除内存分配  
   GlobalUnlock(hDib);
   GlobalFree(hDib);
   CloseHandle(fh);
   return TRUE;
}

20.如何获取局域网上计算机名及它们的IP地址

● 连接ws2_32.lib和 mpr.lib库

● #include winsock2.h

CString strTemp;
struct hostent *host;
struct in_addr *ptr; // 检索IP地址 
DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WNetOpenEnum( dwScope, NULL, NULL, 
             NULL, &#38;hEnum );
WSADATA wsaData;
WSAStartup(MAKEWORD(1,1),&#38;wsaData);
if ( hEnum )
{
    DWORD Count = 0xFFFFFFFF;
    DWORD BufferSize = 2048;
    LPVOID Buffer = new char[2048];
    WNetEnumResource( hEnum, &#38;Count, 
        Buffer, &#38;BufferSize );
    NetResource = (NETRESOURCE*)Buffer;
    char szHostName[200];
    unsigned int i;
    for ( i = 0; 
        i < BufferSize/sizeof(NETRESOURCE); 
        i++, NetResource++ )
    {
        if ( NetResource->dwUsage == 
            RESOURCEUSAGE_CONTAINER &#38;&#38; 
            NetResource->dwType == 
            RESOURCETYPE_ANY )
        {
            if ( NetResource->lpRemoteName )
            {
                CString strFullName = 
                    NetResource->lpRemoteName;
                if ( 0 == 
                    strFullName.Left(2).Compare("\\\\") )   
                    strFullName = 
                        strFullName.Right(
                            strFullName.GetLength()-2);

                gethostname( szHostName, 
                    strlen( szHostName ) );
                host = gethostbyname(strFullName);

                if(host == NULL) continue; 
                ptr = (struct in_addr *) 
                    host->h_addr_list[0];                    

                // =. 分隔开IP:211.40.35.76.             
                int a = ptr->S_un.S_un_b.s_b1;  // 211           
                int b = ptr->S_un.S_un_b.s_b2;  // 40
                int c = ptr->S_un.S_un_b.s_b3;  // 35
                int d = ptr->S_un.S_un_b.s_b4;  // 76

                strTemp.Format("%s -->  %d.%d.%d.%d",
                    strFullName,a,b,c,d);
                AfxMessageBox(strTemp);
            }
        }
    }

    delete Buffer;
    WNetCloseEnum( hEnum ); 
}

WSACleanup();

阅读(423) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~