首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

如何把剪贴板中的位图保存成文件

2012-04-10 
怎么把剪贴板中的位图保存成文件?怎么把剪贴板中的位图保存成文件?谢谢。[解决办法]C/C++ codeHWND hWnd

怎么把剪贴板中的位图保存成文件?
怎么把剪贴板中的位图保存成文件?

谢谢。

[解决办法]

C/C++ code
HWND hWnd = GetSafeHwnd(); // 获取安全窗口句柄::OpenClipboard(hWnd); // 打开剪贴板HANDLE hBitmap = ::GetClipboardData(CF_BITMAP); // 获取剪贴板数据句柄HDC hDC = ::GetDC(hWnd); // 获取设备环境句柄HDC hdcMem = CreateCompatibleDC(hDC); // 创建与设备相关的内存环境SelectObject(hdcMem, hBitmap); // 选择对象SetMapMode(hdcMem, GetMapMode(hDC)); // 设置映射模式BITMAP bm; // 得到位图对象GetObject(hBitmap, sizeof(BITMAP), &bm);BitBlt(hDC, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); //位图复制::ReleaseDC(hWnd, hDC); // 释放设备环境句柄DeleteDC(hdcMem); // 删除内存环境::CloseClipboard(); // 关闭剪贴板
[解决办法]
HANDLE hClip;
CString pBuf;
if(OpenClipboard())//打开剪贴板
{
if(IsClipboardFormatAvailable(CF_TEXT))//判断剪贴板上的数据
{
hClip = GetClipboardData(CF_TEXT);//获取剪贴板内容
pBuf = (char*)GlobalLock(hClip);
GlobalUnlock(hClip);
}
CloseClipboard();//关闭剪贴板
}
return pBuf;

[解决办法]
C/C++ code
// test_clip_bmp.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "afxwin.h"int SaveHBITMAP( HBITMAP hB, char * lpsz_FileName ) {        BITMAP csBitmap;    int nRetValue = GetObject(hB, sizeof(csBitmap), &csBitmap);    unsigned long n_BPP, n_Width, n_Height;        if (nRetValue) {        n_Width   = (long)csBitmap.bmWidth;        n_Height = (long)csBitmap.bmHeight;        n_BPP     = (long)csBitmap.bmBitsPixel;        long sz = csBitmap.bmWidth*csBitmap.bmHeight*(csBitmap.bmBitsPixel>>3);        csBitmap.bmBits = (void *) new BYTE[ sz ];        GetBitmapBits((HBITMAP)hB, sz, csBitmap.bmBits );                printf( "Proceeding Image %dx%d, BPP=%d", n_Width, n_Height, n_BPP, csBitmap.bmBits );    } else {        printf( "Invalid Object in Clipboard Buffer" ); return 1;    }        DWORD *lp_Canvas = new DWORD[ n_Width * n_Height];    if ( n_BPP == 32 ) {        for ( unsigned long y = 0; y < n_Height; y ++ ) {            for ( unsigned long x = 0; x < n_Width; x ++ ) {                RGBQUAD * rgb = ((RGBQUAD *) ((char*)(csBitmap.bmBits)                     + csBitmap.bmWidthBytes*y + x*sizeof(DWORD)) );                lp_Canvas[ (n_Height - 1 - y)*n_Width + x ] = *((DWORD *)rgb);            }        }    } else if ( n_BPP == 24 ) {        for ( unsigned long y = 0; y < n_Height; y ++ ) {            for ( unsigned long x = 0; x < n_Width; x ++ ) {                                RGBTRIPLE rgbi = *((RGBTRIPLE *) ((char*)(csBitmap.bmBits)                    + csBitmap.bmWidthBytes*y + x*3) );                                RGBQUAD rgbq;                rgbq.rgbRed = rgbi.rgbtRed;                rgbq.rgbGreen = rgbi.rgbtGreen;                rgbq.rgbBlue = rgbi.rgbtBlue;                lp_Canvas[ (n_Height - 1 - y)*n_Width + x ] = *((DWORD *)(&rgbq));            }        }    } else {        // here I could handle other resultions also, but I think it is         // too obvoius to add them here....     }        unsigned long n_Bits = 32;    FILE *pFile = fopen(lpsz_FileName, "wb");    if(pFile == NULL) { printf("File Cannot Be Written"); return 1; }        // save bitmap file header    BITMAPFILEHEADER fileHeader;    fileHeader.bfType = 0x4d42;    fileHeader.bfSize = 0;    fileHeader.bfReserved1 = 0;    fileHeader.bfReserved2 = 0;    fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);    fwrite( (char*)&fileHeader, sizeof(fileHeader), 1, pFile );        // save bitmap info header    BITMAPINFOHEADER infoHeader;    infoHeader.biSize = sizeof(infoHeader);    infoHeader.biWidth = n_Width;    infoHeader.biHeight = n_Height;    infoHeader.biPlanes = 1;    infoHeader.biBitCount = ( unsigned short )( n_Bits & 0xffff );    infoHeader.biCompression = BI_RGB;    infoHeader.biSizeImage = 0;    infoHeader.biXPelsPerMeter = 0;    infoHeader.biYPelsPerMeter = 0;    infoHeader.biClrUsed = 0;    infoHeader.biClrImportant = 0;    fwrite( (char*)&infoHeader, sizeof(infoHeader), 1, pFile );    fwrite( (char*)lp_Canvas, 1, (n_Bits >> 3)*n_Width*n_Height, pFile );    fclose( pFile );    return 0;}int main( int, char ** ) {    if (!::IsClipboardFormatAvailable(CF_BITMAP)) {        printf( "Nothing in BMP buffer" ); return 0;    }    if ( OpenClipboard ( 0 ) ) {        HBITMAP hBitmap = (HBITMAP)GetClipboardData( CF_BITMAP );        CloseClipboard();        if ( hBitmap ) {            GlobalLock( hBitmap );            char outfile[255] = "clipboard.bmp";            SaveHBITMAP( hBitmap, outfile );            GlobalUnlock( hBitmap );        } else {            printf( "Nothing in buffer" );        }    } else {        printf( "Windows Clipboard cannot be opened" );    }    return 0;} 

热点排行