1. 程式人生 > >c++將剪貼簿內容儲存為bmp圖

c++將剪貼簿內容儲存為bmp圖

bool CTestClipBoardDlg::WriteDIB(char * 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 CTestClipBoardDlg::SetBackColorToWhite(HDC pDC)
{
	// Set brush to desired background color     
	HBRUSH backBrush= (HBRUSH)::GetStockObject(WHITE_BRUSH);
	// 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;       
}

bool CTestClipBoardDlg::ConvertToBMP(const char * strFileName,const char* strBMPFile,BOOL bScaleImage)
{
	//Declartions   
	HENHMETAFILE hemf;   
	HBITMAP     bitmap;   
	HDC         memDC;   
	ENHMETAHEADER   emh;   

	//Get the DC of the Window   
	HDC dc = ::GetDC(NULL);   

	//Get the Handle from the enhanced metafile   
	hemf = GetEnhMetaFile(strFileName);

	// Get the header from the enhanced metafile.   
	ZeroMemory( &emh, sizeof(ENHMETAHEADER) );   
	emh.nSize = sizeof(ENHMETAHEADER);   

	if( GetEnhMetaFileHeader( hemf, sizeof( ENHMETAHEADER ), &emh ) == 0 )   
	{   
		DeleteEnhMetaFile( hemf );   
		return FALSE;   
	}   

	//Declare variables for calculation of metafile rect   
	RECT    rect;   
	float   PixelsX, PixelsY, MMX, MMY;   
	float fAspectRatio;   
	long lWidth,lHeight;   

	// Get the characteristics of the output device.   
	PixelsX = (float)GetDeviceCaps( dc, HORZRES );   
	PixelsY = (float)GetDeviceCaps( dc, VERTRES );   
	MMX = (float)GetDeviceCaps( dc, HORZSIZE );   
	MMY = (float)GetDeviceCaps( dc, VERTSIZE );   


	// Calculate the rect in which to draw the metafile based on the   
	// intended size and the current output device resolution.   
	// Remember that the intended size is given in 0.01 mm units, so   
	// convert those to device units on the target device.   
	rect.top = (int)((float)(emh.rclFrame.top) * PixelsY / (MMY*100.0f));   
	rect.left = (int)((float)(emh.rclFrame.left) * PixelsX / (MMX*100.0f));   
	rect.right = (int)((float)(emh.rclFrame.right) * PixelsX / (MMX*100.0f));   
	rect.bottom = (int)((float)(emh.rclFrame.bottom) * PixelsY / (MMY*100.0f));   


	//Calculate the Width and Height of the metafile   
	lWidth = (long)((float)(abs(rect.left - rect.right)));   
	lHeight =(long)((float)(abs(rect.top-rect.bottom )));   

	fAspectRatio = (float)lWidth/(float)lHeight;   


	if(bScaleImage) //If miniature option is ON, change the width and height accordingly   
	{   
		if(fAspectRatio > 1 ) //width is more than height    
		{   
			//Make width as constant and calculate Height   
// 			lWidth = X_MINIATUREFRAME;   
// 			lHeight = (long)((float)Y_MINIATUREFRAME / fAspectRatio);   
		}   
		else //width is less than height(or equal to height)   
		{   
			//Make Height as constant and calculate Width   
// 			lHeight = Y_MINIATUREFRAME;   
// 			lWidth = (long)((float)X_MINIATUREFRAME * fAspectRatio);   
		}   

	}   

	//Populate the rect structure   
	rect.left = 0;   
	rect.top = 0;   
	rect.right = lWidth;   
	rect.bottom = lHeight;   

	//Create a Memory DC compatible to WindowDC   
	memDC=::CreateCompatibleDC(dc);    


	//Create a bitmap compatible to Window DC   
	bitmap = ::CreateCompatibleBitmap(dc,lWidth,lHeight);   

	DWORD dwRetError = GetLastError();   


	//Select the bitmap into the Mem DC   
	::SelectObject(memDC,bitmap);   


	//Paint the background of the DC to White   
	SetBackColorToWhite(memDC);   


	//Now play the enhanced metafile into the memory DC; ignore its return value   
	//it may be false even if successful   
	PlayEnhMetaFile(memDC,hemf,&rect);     
	DWORD dwRet = GetLastError();   


	// Create logical palette if device support a palette   
	HPALETTE pal;   
	if( GetDeviceCaps(dc,RASTERCAPS) & RC_PALETTE )   
	{   
		UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);   
		LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];   
		pLP->palVersion = 0x300;   
		pLP->palNumEntries = GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );   

		// Create the palette   
		pal = ::CreatePalette(pLP );   
		delete[] pLP;   

	}   

	// Convert the bitmap to a DIB   
	HANDLE hDIB = DDBToDIB(bitmap, BI_RGB, NULL );   
	if( hDIB == NULL )   
	{   
		DeleteEnhMetaFile( hemf );   
		return FALSE;   
	}   

	// Write it to file   
	WriteDIB((char*)strBMPFile, hDIB );   

	// Free the memory allocated by DDBToDIB for the DIB   
	::GlobalFree( hDIB );   
	::DeleteEnhMetaFile( hemf );   
	::ReleaseDC(NULL,dc);   

	return TRUE;   
}


3.使用
SaveAsEMF("d:\\aaa.emf");//把剪貼簿內容存為EMF檔案
ConvertEMFToBMP("d:\\aaa.emf","d:\\aaa.bmp",false);//將EMF檔案存為BMP檔案


相關推薦

c++剪貼簿內容儲存bmp

bool CTestClipBoardDlg::WriteDIB(char * szFile, HANDLE hDIB) { BITMAPFILEHEADER hdr; LPBITMAPINFOHEADER lpbi; if (!hDIB) return FALSE;

C++程式碼實現畫素矩陣儲存bmp圖片

       用C++程式碼將畫素矩陣儲存為圖片,這裡以讀取yuv序列視訊幀為例進行分析,假設4:2:0yuv序列有300幀,則首先需要將每一視訊幀儲存在一個畫素矩陣中,然後將每一個矩陣儲存為圖片,最終會有300個bmp圖片。       純C++程式碼如下:       s

C#實戰小技巧(八):剪下板中的內容儲存圖片

進行C#開發時,可以將複製到剪下板中的內容轉為HTML檔案,再將HTML頁面轉為圖片進行儲存,示例效果如下。 被複制的Excel表格: 生成的圖片: 實現上述功能的主要程式碼如下,能夠將從Word、Excel、網頁等地方複製的內容匯出,並儲存為圖片。 程式碼:

C#Panel裡面的內容儲存影象

2種方式 第一種: Bitmap bmp = new Bitmap(panel1.Width, panel1.Height); panel1.DrawToBitmap(bmp,new Rectangle(panel1.Location,panel1.Size)); bmp.Save("le

C#如何儲存剪貼簿內容,在使用後恢復。

   C# clipboard類封裝了對剪貼簿的操作,一般使用沒有問題。但由於clipboard封裝的資料型別有限,對於一些自定義型別的剪貼簿資料,如果想佔用剪貼簿並在使用後原樣恢復剪貼簿的資料就會產生問題。試驗了很多方法後,嘗試學習別人C++的思路。使用winapi來處理

C++HBITMAP儲存bmp圖片

BOOL CBMP2ArrayMultiplyDlg::SaveBitmapToFile(HBITMAP hBitmap, CString szfilename) { HDC hDC; //當前解析度下每象素所佔位元組數

頁面內容儲存圖片顯示,長按儲存至本地(html2canvas)

載入的html2canvas為官網上的新版本。   style樣式: *{ margin: 0;padding: 0; font-family: "微軟雅黑"; } html,body{ width: 100%; } #capture,#imgDiv{ width: 100%; } .imgD

C語言實現有一個字元陣列的內容:"student a am i",請你陣列的內容"i am a student"。

//有一個字元陣列的內容為:"student a am i",請你將陣列的內容改為"i am a student"。    要求:不能使用庫函式。               只能開闢有限個空間(空間個數和字串的長度無關)。   解題思路:               

androidLinearLayout中的內容儲存Bitmap。

前幾天,和別人探討問題的時候,突然瞭解到還有這個功能。他是要合成LinearLayout中的多個圖片,如果圖片合成,就很麻煩,所以直接儲存一個LinearLayout。 現在想起來,儲存LinearLayout也那樣,因為LinearLayout也是一個view,我儲存這

如何點陣物件儲存BMP檔案

  GDI中點陣圖物件是很常見的GDI物件,但是無論是SDK,還是MFC都沒有提供現在的函式或是方法來將一個位圖物件儲存為一個BMP檔案,這裡介紹一下儲存方法。 點陣圖檔案格式: DIB檔案有四個主要部分: 檔案表頭(BITMAPFILEHEADER) 資訊表頭  (BITM

HTML5 Canvas的內容儲存圖片

主要思想是藉助Canvas自己的API - toDataURL()來實現,整個實現 HTML + JavaScript的程式碼很簡單。 <html> <meta http-equiv="X-UA-Compatible" content="chrome=1

如何一個UIView物件的內容儲存UIImage

在iPhone程式開發中有可能我們會需要將一個View的內容轉換成圖片。我自己對UIImage進行類擴充套件,寫了一個方法: + (UIImage*)imageFromView:(UIView*)view{ UIGraphicsBeginImageContextWithO

C語言: 有一個字元陣列的內容:"student a am i", 請你陣列的內容"i am a student".

題目:有一個字元陣列的內容為:"student a am i", 請你將陣列的內容改為"i am a student".     要求:不能使用庫函式。只能開闢有限個空間(空間個數和字串的長度無關)。 分析:分為兩大部分:①把整個字元陣列逆置                

HTML5 Canvas的內容儲存圖片藉助toDataURL實現

主要思想是藉助Canvas自己的API - toDataURL()來實現,整個實現 HTML + JavaScript的程式碼很簡單。  複製程式碼 程式碼如下: <html>  <meta http-equiv="X-UA-Compatible"

使用ffmpegBMP圖片編碼x264視訊檔案,H264視訊儲存BMP圖片,yuv視訊檔案儲存圖片的程式碼

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #ifdef __cplusplus extern "C" { #endif #include

[轉]C# 類的內容寫成JSON格式的字符串

json格式 true namespace reading bject mar ole 程序代碼 img 將類的內容寫入到JSON格式的字符串中 本例中建立了Person類,賦值後將類中內容寫入到字符串中 運行本代碼需要添加引用動態庫Newtonsoft.Json 程

C# 獲得剪貼內容和 richTextBox部分文本設置顏色

記錄 sele ida 顏色 bytecode nco 剪貼板 form 之前 try { MemoryStream vMemoryStream = iData.GetData("Html Fo

C++一個十進位制數轉化二進位制數

#include"iostream" #include"string" #include"algorithm" #define MAXSIZE 500 int main() { using namespace std; int number,flag=0; int arry

有一個字元陣列的內容:"student a am i", 陣列的內容"i am a student"

有一個字元陣列的內容為:"student a am i", 將陣列的內容改為"i am a student" 要求:  不能使用庫函式 只能開闢有限個空間(空間個數和字串的長度無關) eg: student a am i  i ma a tneduts  i

PHP資料庫查詢內容轉換JSON格式且顯示中文

最近寫android,開始寫伺服器了。最終伺服器的語言就先定為PHP,因為以前寫過一陣子網頁,所以就先用PHP試試水。 那麼首先為了實現android前端與伺服器互動,那麼我們伺服器傳輸的資料就要先變為JSON格式。 那麼如何將資料庫中的內容查詢出來,並且使其轉變為JSON格式呢?程式碼如下