- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
CSBitmap.cpp
资源名称:eDraw2 [点击查看]
上传用户:fjzzwyy
上传日期:2007-01-14
资源大小:244k
文件大小:8k
源码类别:
绘图程序
开发平台:
Visual C++
- #include "stdafx.h"
- //#include "CMyBit.h"
- #include "CSBitmap.h"
- #include "math.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CSBitmap::CSBitmap()
- {
- m_bHavePal=false;
- m_hDIB=NULL;
- }
- CSBitmap::~CSBitmap()
- {
- }
- BOOL CSBitmap::SaveBMP(CDC* pDC,int x,int y,int cx,int cy,LPCTSTR sBMPFile)
- {
- BITMAPINFO* bmpInfo = (BITMAPINFO *) new BYTE[sizeof(BITMAPINFOHEADER)];
- int size = m_Width*m_Height*3;
- BITMAPFILEHEADER hdr;
- CFile file(sBMPFile,CFile::modeWrite|CFile::modeCreate);
- hdr.bfType = ((WORD)'M'<<8) + 'B';
- hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +size+sizeof(BITMAPINFO));
- hdr.bfReserved1 = 0;
- hdr.bfReserved2 = 0;
- // Compute the offset to the array of color indices.
- hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO);
- //写入文件头
- file.Write((LPVOID) &hdr, sizeof(BITMAPFILEHEADER));
- //写入信息头
- file.Write((LPVOID) bmpInfo, sizeof(BITMAPINFOHEADER));
- // Copy the array of color indices into the .BMP file.
- //写入数据
- int nTotal = size;
- file.WriteHuge((LPSTR)m_hDIB,nTotal);
- //关闭文件
- file.Close();
- delete bmpInfo;
- return true;
- }
- BOOL CSBitmap::LoadBMP(LPCTSTR sBMPFile)
- {
- //, HGLOBAL *phDIB, CPalette *pPal
- CFile file;
- if( !file.Open( sBMPFile, CFile::modeRead))
- return FALSE;
- BITMAPFILEHEADER bmfHeader;
- long nFileLen;
- nFileLen = file.GetLength();
- // Read file header
- if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
- return FALSE;
- // File type should be 'BM'
- if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B'))
- return FALSE;
- HGLOBAL hDIB = ::GlobalAlloc(GMEM_FIXED, nFileLen);
- if (hDIB == 0)
- return FALSE;
- m_DataSize = nFileLen - sizeof(BITMAPFILEHEADER);
- // Read the remainder of the bitmap file.
- if (file.ReadHuge((LPSTR)hDIB, nFileLen - sizeof(BITMAPFILEHEADER)) !=
- nFileLen - sizeof(BITMAPFILEHEADER) )
- {
- ::GlobalFree(hDIB);
- return FALSE;
- }
- BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
- m_Height=bmInfo.bmiHeader.biHeight;
- m_Width=bmInfo.bmiHeader.biWidth;
- int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed :
- 1 << bmInfo.bmiHeader.biBitCount;
- // Create the palette
- if(m_bHavePal==true)
- {
- m_Pal.DeleteObject();
- };
- /*
- if( nColors <= 256 )
- {
- m_bHavePal=true;
- UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY)*nColors);
- LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
- pLP->palVersion = 0x300;
- pLP->palNumEntries = nColors;
- for( int i=0; i < nColors; i++)
- {
- pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
- pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
- pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
- pLP->palPalEntry[i].peFlags = 0;
- }
- m_Pal.CreatePalette( pLP );
- delete[] pLP;
- } */
- m_hDIB = hDIB;
- return TRUE;
- }
- void CSBitmap::DrawImg(CDC *pDC, int x, int y, int cx, int cy)
- {
- CPalette* pPal;
- // TODO: add draw code for native data here
- BITMAPINFO& bi=*(LPBITMAPINFO)m_hDIB;
- LPVOID lpBits;
- //double Magnify=0;
- //Magnify=(double(cx)/bi.bmiHeader.biWidth-double(cy)/bi.bmiHeader.biHeight>0)?
- // double(cy)/bi.bmiHeader.biHeight:double(cx)/bi.bmiHeader.biWidth
- //lpBits=(LPVOID)((BYTE*)m_hglobal+sizeof(BITMAPINFO)+256*4);
- //+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256);
- if(m_bHavePal==true)
- {
- pPal=pDC->SelectPalette(&m_Pal,FALSE);
- pDC->RealizePalette();
- if(bi.bmiHeader.biClrUsed!=0)
- lpBits=(LPVOID)((BYTE*)m_hDIB+sizeof(BITMAPINFOHEADER)+bi.bmiHeader. biClrUsed*4);//+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256);
- else
- lpBits=(LPVOID)((BYTE*)m_hDIB+sizeof(BITMAPINFOHEADER)+(int)pow(2,bi.bmiHeader.biBitCount)*4);
- }
- else
- {
- lpBits=(LPVOID)((BYTE*)m_hDIB+sizeof(BITMAPINFOHEADER));
- };
- SetStretchBltMode(pDC->GetSafeHdc(),HALFTONE);
- StretchDIBits(pDC->GetSafeHdc(),
- x,y,cx,cy,
- // x,y,int(bi.bmiHeader.biWidth*Magnify),int(bi.bmiHeader.biHeight*agnify),
- 0,0,bi.bmiHeader.biWidth,bi.bmiHeader.biHeight,
- lpBits,&bi,DIB_RGB_COLORS,SRCCOPY);
- if(m_bHavePal==true)
- pDC->SelectPalette(pPal,FALSE);
- }
- void CSBitmap::ReleaseMem()
- {
- if(m_hDIB!=NULL)
- ::GlobalFree(m_hDIB);
- m_hDIB=NULL;
- m_bHavePal=false;
- m_Pal.DeleteObject();
- }
- // function : GetRotatedBitmap - Create a new bitmap with rotatd image
- // input : Returns - Returns new bitmap with rotated image
- // hDIB - Device-independent bitmap to rtate
- // radians - Angle of rotation in radians
- // clrBack - Color of pixels in the resulti g bitmap that do
- // not get covered by sourc pixels
- //Add by CQX:
- BOOL CSBitmap::LoadBMP( BYTE * bmpdata,long size)
- {
- HGLOBAL hDIB = (HGLOBAL)bmpdata;
- if (hDIB == 0)
- return FALSE;
- // Read the remainder of the bitmap file.
- BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
- int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed :
- 1 << bmInfo.bmiHeader.biBitCount;
- // Create the palette
- if(m_bHavePal==true)
- {
- m_Pal.DeleteObject();
- };
- if( nColors <= 256 )
- {
- m_bHavePal=true;
- UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
- LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
- pLP->palVersion = 0x300;
- pLP->palNumEntries = nColors;
- for( int i=0; i < nColors; i++)
- {
- pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
- pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
- pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
- pLP->palPalEntry[i].peFlags = 0;
- }
- m_Pal.CreatePalette( pLP );
- delete[] pLP;
- }
- m_hDIB = hDIB;
- return TRUE;
- }