⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dibitmap.cpp

📁 此次上传的使linux下的文件传输协议
💻 CPP
字号:
////////////////////////////////////////////////////////////////
// MSDN Magazine -- October 2001
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual C++ 6.0 for Windows 98 and probably Windows 2000 too.
// Set tabsize = 3 in your editor.
//
#include "StdAfx.h"
#include "Picture.h"
#include <afxpriv2.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

////////////////////////////////////////////////////////////////
// CDIBitmap implementation
//

CDIBitmap::CDIBitmap()
{
	m_bImage=FALSE;
}

CDIBitmap::~CDIBitmap()
{
}


//////////////////
// Load from path name.
//
BOOL CDIBitmap::Load(LPCTSTR sPath)
{
	CFile file;
	if (!file.Open(sPath, CFile::modeRead|CFile::shareDenyWrite))
		return FALSE;
	BOOL bRet = Load(file);
	file.Close();
	return bRet;
}

//////////////////
// Load from CFile
//
BOOL CDIBitmap::Load(CFile& file)
{
	CArchive ar(&file, CArchive::load | CArchive::bNoFlushOnDelete);
	return Load(ar);
}

//////////////////
// Load from archive--create stream and load from stream.
//
BOOL CDIBitmap::Load(CArchive& ar)
{
	CArchiveStream arcstream(&ar);
	return Load((IStream*)&arcstream);
}

//////////////////
// Load from stream (IStream). This is the one that really does it: call
// OleLoadPicture to do the work.
//
BOOL CDIBitmap::Load(IStream* pstm)
{
	Free();
	HRESULT hr = OleLoadPicture(pstm, 0, FALSE,
		IID_IPicture, (void**)&m_spIPicture);
	if(SUCCEEDED(hr) && m_spIPicture)
	{
		m_bImage=TRUE;
	}
	else
	{
		m_bImage=FALSE;
	}
	return TRUE;
}

//////////////////
// Render to device context. Covert to HIMETRIC for IPicture.
//
BOOL CDIBitmap::Render(CDC* pDC, CRect rc, LPCRECT prcMFBounds) const
{
	ASSERT(pDC);

	if (rc.IsRectNull()) {
		CSize sz = GetImageSize(pDC);
		rc.right = sz.cx;
		rc.bottom = sz.cy;
	}
	long hmWidth,hmHeight; // HIMETRIC units
	GetHIMETRICSize(hmWidth, hmHeight);
	m_spIPicture->Render(*pDC, rc.left, rc.top, rc.Width(), rc.Height(),
		0, hmHeight, hmWidth, -hmHeight, prcMFBounds);

	return TRUE;
}

//////////////////
// Get image size in pixels. Converts from HIMETRIC to device coords.
//
CSize CDIBitmap::GetImageSize(CDC* pDC) const
{
	if (!m_spIPicture)
		return CSize(0,0);
	
	LONG hmWidth, hmHeight; // HIMETRIC units
	m_spIPicture->get_Width(&hmWidth);
	m_spIPicture->get_Height(&hmHeight);
	CSize sz(hmWidth,hmHeight);
	if (pDC==NULL) {
		CWindowDC dc(NULL);
		dc.HIMETRICtoDP(&sz); // convert to pixels
	} else {
		pDC->HIMETRICtoDP(&sz);
	}
	return sz;
}

BOOL CDIBitmap::Load(BYTE *lpSrc,DWORD len)
{
	if (!lpSrc)
		return FALSE;
	CMemFile file(lpSrc, len);
	BOOL bRet = Load(file);
	return bRet;
}

BOOL CDIBitmap::Load(UINT nIDRes)
{
	HINSTANCE hInst = AfxGetResourceHandle();
	HRSRC hRsrc = ::FindResource(hInst,MAKEINTRESOURCE(nIDRes),"JPG");
	if(hRsrc) //说明是Jpg类型的
	{
		DWORD len = SizeofResource(hInst, hRsrc);
		BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
		if (!lpRsrc)
			return FALSE;
		CMemFile file(lpRsrc, len);
		BOOL bRet = Load(file);
		FreeResource(hRsrc);
		return TRUE;
	}
	else
	{
		HBITMAP hBmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(nIDRes), IMAGE_BITMAP,0,0, LR_CREATEDIBSECTION); //获得位图句柄
		if( hBmp == 0 )  //如果句柄为空,就返回
			return FALSE;
		return Load(hBmp);
	}
	return FALSE;
}

int CDIBitmap::GetWidth()
{
	CSize size=GetImageSize();
	return size.cx;
}

int CDIBitmap::GetHeight()
{
	CSize size=GetImageSize();
	return size.cy;
}

void CDIBitmap::DrawDIB(CDC* pDC,int x,int y)
{
	DrawDIB(pDC, x, y, GetWidth(), GetHeight() );
}

void CDIBitmap::DrawDIB( CDC * pDC, int x, int y, int width, int height )
{
	CRect rect;
	rect.SetRect(x,y,x+width,y+height);
	Render(pDC,rect);
}

int  CDIBitmap::DrawDIB( CDC * pDC, CRect & rectDC, CRect & rectDIB )
{
	return Render(pDC,rectDC,rectDIB);
}

BOOL CDIBitmap::Load(HBITMAP hBitmap)
{
/*    CPalette m_Palette;
    unsigned char *m_pDib, *m_pDibBits;
    DWORD m_dwDibSize;
    BITMAPINFOHEADER *m_pBIH;
    RGBQUAD *m_pPalette;
    int m_nPaletteEntries;

    dwDibSize = cf.GetLength() - sizeof( BITMAPFILEHEADER );

    // Attempt to allocate the Dib memory.
    unsigned char *pDib;
    pDib = new unsigned char [dwDibSize];
    if( pDib == NULL )
        return( FALSE );

    BITMAPFILEHEADER BFH;

    // Read in the Dib header and data.
    try{

        // Did we read in the entire BITMAPFILEHEADER?
        if( cf.Read( &BFH, sizeof( BITMAPFILEHEADER ) )
            != sizeof( BITMAPFILEHEADER ) ||

            // Is the type 'MB'?
            BFH.bfType != 'MB' ||

            // Did we read in the remaining data?
            cf.Read( pDib, dwDibSize ) != dwDibSize ){

            // Delete the memory if we had any
            // errors and return FALSE.
            delete [] pDib;
            return( FALSE );
            }
        }

    // If we catch an exception, delete the
    // exception, the temporary Dib memory,
    // and return FALSE.
    catch( CFileException *e ){
        e->Delete();
        delete [] pDib;
        return( FALSE );
        }
        // If we got to this point, the Dib has been
    // loaded. If a Dib was already loaded into
    // this class, we must now delete it.
    if( m_pDib != NULL )
        delete m_pDib;
    // Store the local Dib data pointer and
    // Dib size variables in the class member
    // variables.
    m_pDib = pDib;
    m_dwDibSize = dwDibSize;

    // Pointer our BITMAPINFOHEADER and RGBQUAD
    // variables to the correct place in the Dib data.
    m_pBIH = (BITMAPINFOHEADER *) m_pDib;
    m_pPalette =
        (RGBQUAD *) &m_pDib[sizeof(BITMAPINFOHEADER)];

    // Calculate the number of palette entries.
    m_nPaletteEntries = 1 << m_pBIH->biBitCount;
    if( m_pBIH->biBitCount > 8 )
        m_nPaletteEntries = 0;
    else if( m_pBIH->biClrUsed != 0 )
        m_nPaletteEntries = m_pBIH->biClrUsed;

    // Point m_pDibBits to the actual Dib bits data.
    m_pDibBits =
        &m_pDib[sizeof(BITMAPINFOHEADER)+
            m_nPaletteEntries*sizeof(RGBQUAD)];

    // If we have a valid palette, delete it.
    if( m_Palette.GetSafeHandle() != NULL )
        m_Palette.DeleteObject();

    // If there are palette entries, we'll need
    // to create a LOGPALETTE then create the
    // CPalette palette.
    if( m_nPaletteEntries != 0 ){

        // Allocate the LOGPALETTE structure.
        LOGPALETTE *pLogPal = (LOGPALETTE *) new char
                [sizeof(LOGPALETTE)+
                m_nPaletteEntries*sizeof(PALETTEENTRY)];

        if( pLogPal != NULL ){

            // Set the LOGPALETTE to version 0x300
            // and store the number of palette
            // entries.
            pLogPal->palVersion = 0x300;
            pLogPal->palNumEntries = m_nPaletteEntries;

            // Store the RGB values into each
            // PALETTEENTRY element.
            for( int i=0; i<m_nPaletteEntries; i++ ){
                pLogPal->palPalEntry[i].peRed =
                    m_pPalette[i].rgbRed;
                pLogPal->palPalEntry[i].peGreen =
                    m_pPalette[i].rgbGreen;
                pLogPal->palPalEntry[i].peBlue =
                    m_pPalette[i].rgbBlue;
                }

            // Create the CPalette object and
            // delete the LOGPALETTE memory.
            m_Palette.CreatePalette( pLogPal );
            delete [] pLogPal;
            }
        }
    
    return( TRUE );









   // If we have no data, we can't save.
    if( m_pDib == NULL )
        return( FALSE );

    CFile cf;

    // Attempt to create the file.
    if( !cf.Open( pszFilename,
        CFile::modeCreate | CFile::modeWrite ) )
        return( FALSE );
    
    // Write the data.
    try{

        // First, create a BITMAPFILEHEADER
        // with the correct data.
        BITMAPFILEHEADER BFH;
        memset( &BFH, 0, sizeof( BITMAPFILEHEADER ) );
        BFH.bfType = 'MB';
        BFH.bfSize = sizeof( BITMAPFILEHEADER ) + m_dwDibSize;
        BFH.bfOffBits = sizeof( BITMAPFILEHEADER ) +
            sizeof( BITMAPINFOHEADER ) +
            m_nPaletteEntries * sizeof( RGBQUAD );

        // Write the BITMAPFILEHEADER and the
        // Dib data.
        cf.Write( &BFH, sizeof( BITMAPFILEHEADER ) );
        cf.Write( m_pDib, m_dwDibSize );
        }

    // If we get an exception, delete the exception and
    // return FALSE.
    catch( CFileException *e ){
        e->Delete();
        return( FALSE );
        }

    return( TRUE );



































	BITMAP Bm;
	BITMAPINFO BitInfo;
	ZeroMemory(&BitInfo, sizeof(BITMAPINFO));
	BitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	BitInfo.bmiHeader.biBitCount = 0;

	if(!::GetDIBits(hDC, hBitmap, 0, 0, NULL, &BitInfo, DIB_RGB_COLORS))
		return (false);

	Bm.bmHeight = BitInfo.bmiHeader.biHeight;
	Bm.bmWidth  = BitInfo.bmiHeader.biWidth;

	BITMAPFILEHEADER	BmHdr;
	
	BmHdr.bfType		= 0x4d42;   // 'BM' WINDOWS_BITMAP_SIGNATURE
	BmHdr.bfSize		= (((3 * Bm.bmWidth + 3) & ~3) * Bm.bmHeight) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
	BmHdr.bfReserved1	= BmHdr.bfReserved2 = 0;
	BmHdr.bfOffBits		= (DWORD) sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
	
	BitInfo.bmiHeader.biCompression = 0;
	// Writing Bitmap File Header ////
	fwrite(&BmHdr,sizeof(BITMAPFILEHEADER),1,fp);

	fwrite(&BitInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);

	BYTE *pData = new BYTE[BitInfo.bmiHeader.biSizeImage + 5];
	if(!::GetDIBits(hDC, hBitmap, 0, Bm.bmHeight, pData, &BitInfo, DIB_RGB_COLORS))
		return (false);
	if(pData != NULL)
		fwrite(pData,1,BitInfo.bmiHeader.biSizeImage,fp);

	fclose(fp);
	delete (pData);

















*/

	//设备描述表  
	HDC  hDC;  
	//当前分辨率下每象素所占字节数  
	int  iBits;  
	//位图中每象素所占字节数  
	WORD  wBitCount;  
	//定义调色板大小,  位图中像素字节大小  ,位图文件大小  ,  写入文件字节数    
	DWORD  dwPaletteSize=0,  dwBmBitsSize=0,  dwDIBSize=0,  dwWritten=0;    
	//位图属性结构    
	BITMAP  Bitmap;      
	//位图文件头结构  
	BITMAPFILEHEADER  bmfHdr;      
	//位图信息头结构    
	BITMAPINFOHEADER  bi;      
	//指向位图信息头结构      
	LPBITMAPINFOHEADER  lpbi;      
	//定义文件,分配内存句柄,调色板句柄    
	HANDLE  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            
	{
		wBitCount=24;
	}
	
	GetObject(hBitmap, sizeof(Bitmap), (LPSTR)&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.biClrImportant=0;  
	bi.biClrUsed=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,(BITMAPINFO*)lpbi,DIB_RGB_COLORS);
	
	//恢复调色板      
	if(hOldPal)    
	{
		::SelectPalette(hDC,(HPALETTE)hOldPal,TRUE);
		RealizePalette(hDC);
		::ReleaseDC(NULL,hDC);
	}
	//  设置位图文件头    
	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;    
	//  写入位图文件头    
	long nLenght=sizeof(BITMAPFILEHEADER)+dwDIBSize;
	BYTE* lpSrc=new BYTE[nLenght];
	memcpy(lpSrc,&bmfHdr,sizeof(BITMAPFILEHEADER));
	memcpy(lpSrc+sizeof(BITMAPFILEHEADER),lpbi,dwDIBSize);
	BOOL bSet=Load(lpSrc,nLenght);
	GlobalUnlock(hDib);    
	GlobalFree(hDib);   
	delete []lpSrc;
	return bSet;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -