/*=========================================================================== Copy right : Pravarakhya ?2003 Project/Product : Convert EMF to BMP Version number : 1.0 File name : ConvertEMFToBMP Author : Pravarakhya Date of creation : 17 Oct 2003 // This code may be used in compiled form in any way you desire. This // file may be redistributed unmodified by any means PROVIDING it is // not sold for profit without the authors written consent, and // providing that this notice and the authors name is included. If // the source code in this file is used in any commercial application // then a simple email would be nice. // // This file is provided "as is" with no expressed or implied warranty. // The author accepts no liability if it causes any damage whatsoever.============================================================================*/// ConvertEMFToBMP.h : header file // #if !defined(AFX_EMF2BMP_H__12044F2C_BFFB_411B_BED2_586BD35C67B1__INCLUDED_) #define AFX_EMF2BMP_H__12044F2C_BFFB_411B_BED2_586BD35C67B1__INCLUDED_#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000class CConvertEMFToBMP { public: BOOL ConvertEMFToBMP(const char* pszEMFFile,const char* pszBMPFile,BOOL bScaleImage=false); BOOL ConvertBMPToEMF(const char*pszBMPFile,const char* pszEMFFile); private: BOOL ConvertToBMP(const char* strFileName,const char* strBMPFile,BOOL bScaleImage); BOOL SetBackColorToWhite(HDC pDC); BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB); HANDLE DDBToDIB( HBITMAP bitmap, DWORD dwCompression, HPALETTE pPal ); };#endif // !defined(AFX_EMF2BMP_H__12044F2C_BFFB_411B_BED2_586BD35C67B1__INCLUDED_)
cpp第一部分 /*=========================================================================== Copy right : Pravarakhya ?2003 Project/Product : Convert EMF to BMP Version number : 1.0 File name : ConvertEMFToBMP Author : Pravarakhya Date of creation : 17 Oct 2003 // This code may be used in compiled form in any way you desire. This // file may be redistributed unmodified by any means PROVIDING it is // not sold for profit without the authors written consent, and // providing that this notice and the authors name is included. If // the source code in this file is used in any commercial application // then a simple email would be nice. // // This file is provided "as is" with no expressed or implied warranty. // The author accepts no liability if it causes any damage whatsoever.Modifications: 24/10/2003 added function ConvertBMPToEMF based on code sent by Gernot Frisch ============================================================================*/#include "stdafx.h" #include "ConvertEMFToBMP.h"#define X_MINIATUREFRAME 550 #define Y_MINIATUREFRAME 550 #define BUFFSIZE 500 /*========================================================================== Name :DDBToDIB Purpose :Converts the Device Dependant Bitmap to Device Independant Bitmap DDBToDIB - Creates a DIB from a DDB bitmap - Device dependent bitmap dwCompression - Type of compression - see BITMAPINFOHEADER pPal - Logical paletteArguments :HBITMAP bitmap, DWORD dwCompression, HPALETTE pPal Return values :HANDLE to the DIB ============================================================================*/HANDLE CConvertEMFToBMP::DDBToDIB( HBITMAP bitmap, DWORD dwCompression, HPALETTE pPal ) { BITMAP bm; BITMAPINFOHEADER bi; LPBITMAPINFOHEADER lpbi; DWORD dwLen; HANDLE hDIB; HANDLE handle; HDC hDC; HPALETTE hPal; // The function has no arg for bitfields if( dwCompression == BI_BITFIELDS ) return NULL; // If a palette has not been supplied use defaul palette hPal = (HPALETTE) pPal; if (hPal==NULL) hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE); // Get bitmap information ::GetObject(bitmap,sizeof(bm),(LPSTR)&bm); // Initialize the bitmapinfoheader bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth = bm.bmWidth; bi.biHeight = bm.bmHeight; bi.biPlanes = 1; bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel; bi.biCompression = dwCompression; bi.biSizeImage = 0; bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 0; bi.biClrImportant = 0; // Compute the size of the infoheader and the color table int nColors = (1 << bi.biBitCount); if( nColors > 256 ) nColors = 0; dwLen = bi.biSize + nColors * sizeof(RGBQUAD); // We need a device context to get the DIB from hDC = ::GetDC(NULL); hPal = SelectPalette(hDC,hPal,FALSE); RealizePalette(hDC); // Allocate enough memory to hold bitmapinfoheader and color table hDIB = GlobalAlloc(GMEM_FIXED,dwLen); if (!hDIB) { SelectPalette(hDC,hPal,FALSE); ::ReleaseDC(NULL,hDC); return NULL; } lpbi = (LPBITMAPINFOHEADER)hDIB; *lpbi = bi; // Call GetDIBits with a NULL lpBits param, so the device driver // will calculate the biSizeImage field GetDIBits(hDC, (HBITMAP)bitmap, 0L, (DWORD)bi.biHeight, (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS); bi = *lpbi; // If the driver did not fill in the biSizeImage field, then compute it // Each scan line of the image is aligned on a DWORD (32bit) boundary if (bi.biSizeImage == 0) { bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) * bi.biHeight; // If a compression scheme is used the result may infact be larger // Increase the size to account for this. if (dwCompression != BI_RGB) bi.biSizeImage = (bi.biSizeImage * 3) / 2; } // Realloc the buffer so that it can hold all the bits dwLen += bi.biSizeImage; if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE)) { hDIB = handle; } else { GlobalFree(hDIB);
// Reselect the original palette SelectPalette(hDC,hPal,FALSE); ::ReleaseDC(NULL,hDC); return NULL; }
// Get the bitmap bits lpbi = (LPBITMAPINFOHEADER)hDIB;
// FINALLY get the DIB BOOL bGotBits = GetDIBits( hDC, bitmap, 0L, // Start scan line (DWORD)bi.biHeight, // # of scan lines (LPBYTE)lpbi // address for bitmap bits + (bi.biSize + nColors * sizeof(RGBQUAD)), (LPBITMAPINFO)lpbi, // address of bitmapinfo (DWORD)DIB_RGB_COLORS); // Use RGB for color table
SelectPalette(hDC,hPal,FALSE); ::ReleaseDC(NULL,hDC); return hDIB; } /*========================================================================== Name :WriteDIB Purpose :Writes the DIB information to the file szFile - Name of file to write to hDIB - Handle of the DIB Arguments :LPTSTR szFile, HANDLE hDIB Return values :TRUE on success ============================================================================*/BOOL CConvertEMFToBMP::WriteDIB( LPTSTR szFile, HANDLE hDIB) { BITMAPFILEHEADER hdr; LPBITMAPINFOHEADER lpbi;
// Fill in the fields of the file header hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM" hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr ); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize + nColors * sizeof(RGBQUAD));
// Write the file header fwrite( &hdr, sizeof(hdr),1,file);
// Write the DIB header and the bits fwrite( lpbi, GlobalSize(hDIB),1,file);
//Close the file and return fclose(file);
return TRUE; }BOOL CConvertEMFToBMP::SetBackColorToWhite(HDC pDC) { // Set brush to desired background color HBRUSH backBrush= (HBRUSH)(RGB(255, 255, 255));
// Save old brush HBRUSH pOldBrush = (HBRUSH)::SelectObject(pDC,backBrush);
RECT rect ; ::GetClipBox(pDC,&rect); // Erase the area needed
//paint the given rectangle using the brush that is currently selected //into the specified device context ::PatBlt(pDC,rect.left, rect.top, abs(rect.left - rect.right),abs(rect.top-rect.bottom ),PATCOPY); //Select back the old brush ::SelectObject(pDC,pOldBrush); return TRUE; }
ondraw可以用Textout或Drawtext
只要指定字体就行了
BOOL bOpenFileDialog=false;
UINT nIDSTitle=AFX_IDS_OPENFILE;
CFileDialog dlgFile(bOpenFileDialog); CString title="选择文件";
DWORD lFlags=OFN_HIDEREADONLY | OFN_LONGNAMES|OFN_EXTENSIONDIFFERENT;
dlgFile.m_ofn.Flags |= lFlags;
CString strFilter;
CString strDefault;
CString fileName; strFilter+="EMF文件(*.EMF)";
strFilter+= (TCHAR)'\0';
strFilter+=_T("*.EMF");
strFilter+= (TCHAR)'\0'; strFilter+="位图文件(*.bmp)";
strFilter+= (TCHAR)'\0';
strFilter+=_T("*.bmp");
strFilter+= (TCHAR)'\0'; CString allFilter;
VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
strFilter += allFilter;
dlgFile.m_ofn.nMaxCustFilter=2;
strFilter += (TCHAR)'\0';
strFilter += _T("*.*");
strFilter += (TCHAR)'\0';
strFilter += (TCHAR)'\0'; dlgFile.m_ofn.lpstrFilter = strFilter;
dlgFile.m_ofn.lpstrTitle = title;
dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
CString m_bmpfile;
if(dlgFile.DoModal() != IDOK) return;
if (dlgFile.m_ofn.nFilterIndex==1)
{
if(fileName.Find(".emf")<0&&fileName.Find(".EMF")<0)
{
if(fileName.Find(".")>0)
{
CString info;
info=fileName;
info +=" 后缀不是.EMF,您确认它是EMF文件吗?";
if(MessageBox(info,"提示",MB_ICONINFORMATION|MB_YESNO)==IDNO)
return;
m_bmpfile=dlgFile.m_ofn.lpstrFile; }
else
m_bmpfile.Format("%s%s",fileName,".EMF");
}
else
m_bmpfile=dlgFile.m_ofn.lpstrFile;
char bmpfile[256];
strcpy(bmpfile,m_bmpfile);
// CreatePlotEMF(m_bmpfile);
CreatePageEMF();
CopyEnhMetaFile(m_hPageEMF, bmpfile);
HENHMETAFILE hMF;
DWORD dwError;
HANDLE hCBData; //bitmap
CDC* pScreenDC;
HBITMAP hBitmap;
HDC hdcCompatible;
hCBData=SetClipboardData(CF_ENHMETAFILE, hMF);
if(hCBData==NULL)
{
dwError=GetLastError();
}
CloseClipboard();
}
else if (dlgFile.m_ofn.nFilterIndex==2)
{ if(fileName.Find(".bmp")<0&&fileName.Find(".BMP")<0)
{
if(fileName.Find(".")>0)
{
CString info;
info=fileName;
info +=" 后缀不是.bmp,您确认它是位图文件吗?";
if(MessageBox(info,"提示",MB_ICONINFORMATION|MB_YESNO)==IDNO)
return;
m_bmpfile=dlgFile.m_ofn.lpstrFile; }
else
m_bmpfile.Format("%s%s",fileName,".bmp");
}
else
m_bmpfile=dlgFile.m_ofn.lpstrFile; int k= m_bmpfile.Find('.');
CString m_emffile;
m_emffile.Format("%s%s",m_bmpfile.Left(k),".EMF");
char bmpfile[256];
strcpy(bmpfile,m_emffile);
// CreatePlotEMF(m_bmpfile);
CreatePageEMF(); CopyEnhMetaFile(m_hPageEMF, bmpfile); CConvertEMFToBMP conv;
BOOL bRet = conv.ConvertEMFToBMP(m_emffile,m_bmpfile,false);
HENHMETAFILE hMF;
DWORD dwError;
HANDLE hCBData; //bitmap
HDC hdcCompatible;
hCBData=SetClipboardData(CF_ENHMETAFILE, hMF);
if(hCBData==NULL)
{
dwError=GetLastError();
}
CloseClipboard();
DeleteFile(m_emffile);
}
Copy right : Pravarakhya ?2003
Project/Product : Convert EMF to BMP
Version number : 1.0
File name : ConvertEMFToBMP
Author : Pravarakhya
Date of creation : 17 Oct 2003
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in this file is used in any commercial application
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage whatsoever.============================================================================*/// ConvertEMFToBMP.h : header file
//
#if !defined(AFX_EMF2BMP_H__12044F2C_BFFB_411B_BED2_586BD35C67B1__INCLUDED_)
#define AFX_EMF2BMP_H__12044F2C_BFFB_411B_BED2_586BD35C67B1__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000class CConvertEMFToBMP
{
public:
BOOL ConvertEMFToBMP(const char* pszEMFFile,const char* pszBMPFile,BOOL bScaleImage=false);
BOOL ConvertBMPToEMF(const char*pszBMPFile,const char* pszEMFFile);
private:
BOOL ConvertToBMP(const char* strFileName,const char* strBMPFile,BOOL bScaleImage);
BOOL SetBackColorToWhite(HDC pDC);
BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB);
HANDLE DDBToDIB( HBITMAP bitmap, DWORD dwCompression, HPALETTE pPal );
};#endif // !defined(AFX_EMF2BMP_H__12044F2C_BFFB_411B_BED2_586BD35C67B1__INCLUDED_)
/*===========================================================================
Copy right : Pravarakhya ?2003
Project/Product : Convert EMF to BMP
Version number : 1.0
File name : ConvertEMFToBMP
Author : Pravarakhya
Date of creation : 17 Oct 2003
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in this file is used in any commercial application
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage whatsoever.Modifications:
24/10/2003 added function ConvertBMPToEMF based on code sent by Gernot Frisch
============================================================================*/#include "stdafx.h"
#include "ConvertEMFToBMP.h"#define X_MINIATUREFRAME 550
#define Y_MINIATUREFRAME 550
#define BUFFSIZE 500
/*==========================================================================
Name :DDBToDIB
Purpose :Converts the Device Dependant Bitmap to Device Independant Bitmap
DDBToDIB - Creates a DIB from a DDB
bitmap - Device dependent bitmap
dwCompression - Type of compression - see BITMAPINFOHEADER
pPal - Logical paletteArguments :HBITMAP bitmap, DWORD dwCompression, HPALETTE pPal
Return values :HANDLE to the DIB
============================================================================*/HANDLE CConvertEMFToBMP::DDBToDIB( HBITMAP bitmap, DWORD dwCompression, HPALETTE pPal )
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal;
// The function has no arg for bitfields
if( dwCompression == BI_BITFIELDS )
return NULL; // If a palette has not been supplied use defaul palette
hPal = (HPALETTE) pPal;
if (hPal==NULL)
hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE); // Get bitmap information
::GetObject(bitmap,sizeof(bm),(LPSTR)&bm); // Initialize the bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0; // Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if( nColors > 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD); // We need a device context to get the DIB from
hDC = ::GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC); // Allocate enough memory to hold bitmapinfoheader and color table
hDIB = GlobalAlloc(GMEM_FIXED,dwLen); if (!hDIB)
{
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
} lpbi = (LPBITMAPINFOHEADER)hDIB; *lpbi = bi; // Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, (HBITMAP)bitmap, 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS); bi = *lpbi; // If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if (bi.biSizeImage == 0)
{
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight; // If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
} // Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
{
hDIB = handle;
}
else
{
GlobalFree(hDIB);
// Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}
// Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB;
// FINALLY get the DIB
BOOL bGotBits = GetDIBits( hDC, bitmap,
0L, // Start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)DIB_RGB_COLORS); // Use RGB for color table
if( !bGotBits )
{
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return hDIB;
}
/*==========================================================================
Name :WriteDIB
Purpose :Writes the DIB information to the file
szFile - Name of file to write to
hDIB - Handle of the DIB
Arguments :LPTSTR szFile, HANDLE hDIB
Return values :TRUE on success
============================================================================*/BOOL CConvertEMFToBMP::WriteDIB( LPTSTR szFile, HANDLE hDIB)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
if (!hDIB)
return FALSE;
FILE* file;
file = fopen(szFile,"wb");
if(file == NULL)
return FALSE;
lpbi = (LPBITMAPINFOHEADER)hDIB;
int nColors = 0;
if(lpbi->biBitCount <= 8)
{
nColors = (1 << lpbi->biBitCount);
}
// Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD));
// Write the file header
fwrite( &hdr, sizeof(hdr),1,file);
// Write the DIB header and the bits
fwrite( lpbi, GlobalSize(hDIB),1,file);
//Close the file and return
fclose(file);
return TRUE;
}BOOL CConvertEMFToBMP::SetBackColorToWhite(HDC pDC)
{
// Set brush to desired background color
HBRUSH backBrush= (HBRUSH)(RGB(255, 255, 255));
// Save old brush
HBRUSH pOldBrush = (HBRUSH)::SelectObject(pDC,backBrush);
RECT rect ;
::GetClipBox(pDC,&rect); // Erase the area needed
//paint the given rectangle using the brush that is currently selected
//into the specified device context
::PatBlt(pDC,rect.left, rect.top, abs(rect.left - rect.right),abs(rect.top-rect.bottom ),PATCOPY); //Select back the old brush
::SelectObject(pDC,pOldBrush); return TRUE;
}