现在的位置: 首页 > 综合 > 正文

保存减切板内容为BMP图片 && Clipboard save as bmp……

2013年08月02日 ⁄ 综合 ⁄ 共 8615字 ⁄ 字号 评论关闭

保存减切板内容为BMP图片.......

 

最近折腾保存减切板内容为BMP图片。从网上找了N多代码,其中从CSDN里边也找了几个,妈妈的昨天整的我郁闷的是,本来一个差不多1.5M的东西,结果保存成了665M大小的,而且没有大小,没有内容,哪个都不对。日。都是摆渡搞的最后没有办法去外国人那里。还是那里实在啊。找了一个WIN32 CONSOLE的。居然能用。

 

P.S:CSDN的兄弟,能不能不浮夸,不要跟发改委的屁股一样,做技术的贵在塌实,不塌实怎么搞技术。昨天看见一个BMP灰度化的程序,下了,一看狗屁,代码里边啥也没有啊,就这样骗分啊。还有一个说什么VC++的,下来一看是NET || C#的,浪费青春时间啊。不扯淡。GOEO。three mailes on. three mailes down.

the easy day is tomorrow...

 

不知道有没有时间,有时间想把这个作成一个DLL。保存剪切板图片成BMP. JPG. TNF. GIF等等图片,提供原始代码.DLL和相关开发文档。

 

THANKS MSDN,GOOGLE, vicf0kin(not sure).

 

 

Any if you want connect the orign code man please check down lines..

 

You can try to find me via ICQ 371178019
or write an email to
vicf0kin (at) gmail.com
( My Physical location is Ukraine, +2GMT )

http://f0kin.net/page/save-clipboard-bitmap-to-file/

 

 

 

基础知识:

有关的BMP文件格式。Clipboard的知识

参考MSDN,或者MSP出的那个VC6.0技术内幕。

 

 

 

 

//这个函数是响应一个按钮的,我所谓,唯一想说的就是关于里边关于剪切板的知识。

//首先使用剪切板要打开,然后确定内容是BMP格式,最后获得数据。然后使用玩在关闭。

//本着使用时候在申请,实用完关闭的原则,尽管现在机器速度提高了,但是不用的时候常时间占用资源也是

//可耻的行为。

 

 

 

// June 2 2009 AM:9:30........

// 测试系统WINXP  && VS2005.........

 

void CMyDlg::OnBnClickedButtonSaveimg()

{

              //Get the AVI format to clipboard
             if ( !capEditCopy( m_hCapture ) )
            {
                   MessageBox("falied to get window");
                   return;
             }

 

              if ( !::IsClipboardFormatAvailable(CF_BITMAP) )
             {
                    MessageBox("Nothing in BMP buffer");
                    return;
              }

            

              HBITMAP hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
 
              if ( hBitmap )
             {
                      GlobalLock(hBitmap);
                      char outfile[255] = "..//June_01.bmp";
                      SaveHBITMAP(hBitmap, outfile);
                      GlobalUnlock(hBitmap);
              }//_IF
             else
             {
  
                     OpenClipboard();
                    if (IsClipboardFormatAvailable(CF_BITMAP))
                   {
                             if ( hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP) )
                             {
                                     GlobalLock(hBitmap);
                                     char outfile[255] = "..//June_01.bmp";
                                     SaveHBITMAP(hBitmap, outfile);
                                     GlobalUnlock(hBitmap);
                              }//_IF
                   }//_IF
             // Failed to open clipboard return
            

            return;
             }//_ELSE

 

             return;
}

 

 

 

 

int CMyDlg::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 = n_Bits; 
 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;

}

 

 

 

原始程序是控制台的,从网上拷下来的时候,整理花了很长时间。VS2005下一个HOT KEY是ALT && F8....

 

/*
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 = n_Bits; 
 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;
}

//http://f0kin.net/page/save-clipboard-bitmap-to-file/

*/

 

 

 

现在还没有具体分析哪个保存代码。有关BMP,Cilpboard,File, 请参考MSDN。只能说一句话。相信MSDN比相信党的精神还有用,很和谐,很强大。

 

 

希望有时间能完成DLL化的工作。。

 

 

 

//-----------------------------------------------------------------

June_02_2009 AM: 9:33

 

Code by sealplusplus...

 

 

World is shit.....

 

                  ----------General Patton

 

 

 

 

 

// Update.......

DataSave( char* lpSaveData, int nDataSize )
{
        int nDataLen;
        if  ( !lpSaveData )
       {
              m_strStatus = " Save Data Invalid";
              m_EditState.SetWindowTextW( m_strStatus );
              return 0;
        }//_IF
 
        FILE *fDataSave;
        // Open for wriite
        if ( fopen_s( &fDataSave, "..//SaveDataLog.txt", "a+t") == NULL )
        {
              nDataLen = fwrite( "/n", sizeof(char), 1, fDataSave );
              nDataLen = fwrite( lpSaveData,sizeof(char), nDataSize,  fDataSave );
                if ( nDataLen == nDataSize )
               {
                       fclose( fDataSave );
                       return nDataLen;
                }//_IF
        }//_IF
        else
        {
                // Code error
         }//_ELSE
 
         fclose( fDataSave );
          return 0;
}
 
折腾一个下午才搞定,结果发现保存的地方不对,对一个备份文件折腾半天,我说怎么老没有变化。而且函数,突然就对了,早晨可能起猛了,一天都晕晕的。。。
 

 

抱歉!评论已关闭.