[C++/Visual C++] Copier contenu Clipboard dans un fichier

Copier contenu Clipboard dans un fichier [C++/Visual C++] - Programmation

Marsh Posté le 02-08-2001 à 13:41:44    

Je souhaite récupérer une image contenue dans le presse papier et la mettre dans un fichier .bmp.
Comment faire ?

Reply

Marsh Posté le 02-08-2001 à 13:41:44   

Reply

Marsh Posté le 02-08-2001 à 14:07:34    

J'essaie de retrouver cela ce soir dans mes archives.  
 
J'avais récupéré et adapté un source en C (16 bits) qui permet d'exporter le contenu du PrsPap en BMP !

Reply

Marsh Posté le 02-08-2001 à 14:47:04    

merci c super :-))

Reply

Marsh Posté le 02-08-2001 à 14:51:57    

:hello:

Reply

Marsh Posté le 03-08-2001 à 09:43:12    

Les différents messages regroupent une partie du travail de Larry Widing   Initial version for Win Tech Journal Article. 15 oct 1991  
 
En 32 bits, NE PAS OUBLIER DE REMPLACER LES int PAR DES short ! (entre autres)
-------------------------------------------------

 

BITMAPS.H
 
#ifndef __BITMAPS_H__
#define __BITMAPS_H__ 1
 
#ifndef RC_INVOKED
/*
** Macros
*/
#define NULLWND ((HWND)NULL)
 
/*
** Types
*/
typedef HANDLE HDIB;  /* Handle to a DIB */
 
#if !defined(__TSC__)
#define EXPORT _export
#else
#define EXPORT
#endif
 
/*
** Global Variables
*/
extern HWND MainWindow; /* Handle to the application's main window */
extern HANDLE AppInstance; /* Handle to application's instance */
extern char FileName[256]; /* Name of file returned by open file dialog */
extern HBITMAP BitmapHandle; /* Handle of currently loaded bitmap */
extern HDIB DIBitmapHandle; /* Handle of packed DI Bitmap */
 
#ifndef HUGE
#define HUGE huge
#endif
#endif
 
#endif /* !defined(__BITMAPS_H__) */

Reply

Marsh Posté le 03-08-2001 à 09:44:02    

Le MAIN avec la boucle des messages
 
HWND  MainWindow; /* Handle to the application's main window */
HANDLE  AppInstance; /* Handle to application's instance */
char  FileName[256]; /* Name of file returned by open file dialog */
HBITMAP  BitmapHandle; /* Handle of currently loaded bitmap */
 
// Messages
 
   case WM_COMMAND:
     switch (wParam)
     {
       case FILE_SAVE:
  SaveImageFile(wnd);
  break;
 
       case EDIT_PASTE:
  if (OpenClipboard(wnd))
  {
    HANDLE handle;
    int  fmt = 0, fbmp = FALSE;
 
    while ((fmt = EnumClipboardFormats(fmt)) != 0)
    {
      if (fmt == CF_BITMAP) fbmp = TRUE;
    }
    if (fbmp) fmt = CF_BITMAP;
 
                  if (fmt != 0)
    {
      handle = GetClipboardData(fmt);
      if (fmt == CF_BITMAP)
      {
        HBITMAP hbm;
 
                      hbm = CopyBitmap(handle);
        if (hbm != (HBITMAP)NULL)
        {
   ClearHandles();
   BitmapHandle = hbm;
   InvalidateRect(wnd, NULL, TRUE);
        }
      }
    }
    CloseClipboard();
  }
  break;
 
              case .....
            }
 
}
 
 
 
void ClearHandles(void)
{
    if (BitmapHandle != (HBITMAP)NULL)
    {
      DeleteObject(BitmapHandle);
      BitmapHandle = (HBITMAP)NULL;
    }
}

Reply

Marsh Posté le 03-08-2001 à 09:44:37    

Fonctions diverses
 
#include <windows.h>
#include <malloc.h>
#include "bitmaps.h"
 
 
/* int number of color entries in header
** DIBitmapColors(BITMAPINFO FAR *bmi); pointer to bitmap header
**
**    This function returns the number of colors in the color table of
** the specified Device-Independant Bitmap.  */
 
int DIBitmapColors(BITMAPINFO FAR *bmi)
{
    if (bmi->bmiHeader.biClrUsed == 0)
    {  /* Maximum number of entries */
       switch (bmi->bmiHeader.biBitCount)
       {
         case 1:  return 2; /* Monochrome bitmap -> 2 colors */
  case 4:  return 16; /* 4-bit image -> 16 colors */
  case 8:  return 256; /* 8-bit image -> 256 colors */
  case 24: return 0; /* 24-bt image -> 0 colors in color table */
       }
    }
    return 0;
}
 
/* LPSTR pointer to bitmap bits
** DIBitmapBits(BITMAPINFO FAR *bmi); pointer to bitmap header
**
**    This function returns a pointer to the bits in a packed Device-
** Independant Bitmap.   */
 
LPSTR DIBitmapBits(BITMAPINFO FAR *bmi)
{
      LPSTR bits;
      int colors = DIBitmapColors(bmi);
 
      bits = ((LPSTR)bmi) + (unsigned int)bmi->bmiHeader.biSize;
 
      if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
        bits += colors * sizeof(RGBTRIPLE);
      else
        bits += colors * sizeof(RGBQUAD);
 
      return bits;
}
 
 
/* HBITMAP Handle of duplicate if it could be created
** CopyBitmap(HBITMAP hbm); Handle of bitmap to copy
**
**    This function will create a duplicate of the passed bitmap, returning
** the handle of the new bitmap if successful, and NULL if it could not
** perform the copy.  */
 
HBITMAP CopyBitmap(HBITMAP hbm)
{
 HBITMAP hbmNew = (HBITMAP)NULL;
 HBITMAP oldBm;
 HDC hdcMem;
 BITMAP bm;
 
 GetObject(hbm, sizeof(bm), (LPSTR)&bm);
 hdcMem = CreateCompatibleDC(NULL);
 if (hdcMem != (HDC)NULL)
 {
   oldBm = SelectObject(hdcMem, hbm);
   hbmNew = CreateCompatibleBitmap(hdcMem, bm.bmWidth, bm.bmHeight);
   if (hbmNew != (HBITMAP)NULL)
   {
     SelectObject(hdcMem, hbmNew);
     DrawBitmap(hdcMem, 0, 0, hbm);
   }
   SelectObject(hdcMem, oldBm);
   DeleteDC(hdcMem);
 }
 return hbmNew;
}
 
/* HDIB handle of new packed DIB (NULL if an error occurred)
** BitmapToDIB(
**   HBITMAP hbm, handle of logical bitmap to convert
**   int mode);   mode to use when converting
**
**    This function will convert a logical bitmap into a unencoded packed Device
**    Independant Bitmap */
 
HDIB BitmapToDIB(HBITMAP hbm)
{
 int  i;
 HDC  hdc;
 HDIB  result = (HDIB)NULL;
 BITMAPINFO *bmi;
 LPSTR  ptr, sptr, dptr;
 int  hdrSize;
 int  bitsPerPixel;
 BITMAP  bm;
 
 /* Get bitmap information */
 GetObject(hbm, sizeof(bm), (LPSTR)&bm);
 if (bm.bmPlanes == 1)
   bitsPerPixel = bm.bmBitsPixel;
 else
   bitsPerPixel = bm.bmPlanes;
 
 /* Building a Windows compatible Bitmap */
 hdrSize = sizeof(BITMAPINFOHEADER);
 
 switch (bitsPerPixel)
 {
   case 1:
     hdrSize += 2 * sizeof(RGBQUAD);
     break;
 
   case 3:
     ++ bitsPerPixel;
   case 4:
     hdrSize += 16 * sizeof(RGBQUAD);
     break;
 
   case 8:
     hdrSize += 256 * sizeof(RGBQUAD);
     break;
 }
 
 bmi = (BITMAPINFO *)malloc(hdrSize);
 
 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 bmi->bmiHeader.biWidth = bm.bmWidth;
 bmi->bmiHeader.biHeight = bm.bmHeight;
 bmi->bmiHeader.biPlanes = 1;
 bmi->bmiHeader.biBitCount = bitsPerPixel;
 bmi->bmiHeader.biCompression = BI_RGB;
 bmi->bmiHeader.biSizeImage = 0;
 bmi->bmiHeader.biXPelsPerMeter = 0;
 bmi->bmiHeader.biYPelsPerMeter = 0;
 bmi->bmiHeader.biClrUsed = 0;
 bmi->bmiHeader.biClrImportant = 0;
 
 /*  Get a DC to use */
 hdc = GetDC(MainWindow);
 if (hdc != (HDC)NULL)
 {
   if (GetDIBits(hdc, hbm, 0, bm.bmHeight, NULL, (LPBITMAPINFO)bmi, DIB_RGB_COLORS))
   {
     result = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
     (long)hdrSize + bmi->bmiHeader.biSizeImage);
     if (result != (HANDLE)NULL)
     {
       ptr = (LPSTR)GlobalLock(result);
       if (ptr == NULL)
       {
         GlobalFree(result);
         result = (HANDLE)NULL;
         ErrorBox("BitmapToDIB(): Unable to lock DIB memory" );
       }
       else
         sptr = (LPSTR)bmi;
     }
   }
 
   if (result)
   { /* Copy header */
     dptr = ptr;
     for (i = 0 ; i < hdrSize ; ++ i) *dptr ++ = *sptr ++;
 
     /* Get the bits */
     if (! GetDIBits(hdc, hbm, 0, bm.bmHeight, dptr, (LPBITMAPINFO)ptr, DIB_RGB_COLORS))
     {
       GlobalUnlock(result);
       GlobalFree(result);
       result = (HANDLE)NULL;
     }
     else
       GlobalUnlock(result);
   }
   ReleaseDC(MainWindow, hdc);
 }
 else
   ErrorBox("BitmapToDIB(): Unable to get DC from main window" );
 
 if (bmi != NULL) free(bmi);
 
 return result;
}

Reply

Marsh Posté le 03-08-2001 à 10:06:42    

La fin, squizzée par le serveur (pas plus de trois envois en moins de 10 minutes, je voulais éviter de faire déborder la page en fragmentant  :D )
 
Sauve le fichier au format BMP non RLE encodé sous le nom "FileName"
 
extern HDIB BitmapToDIB(HBITMAP);
extern int WriteBitmapFile(const char *, const HANDLE);
 
   HANDLE handle;
   HCURSOR oldCursor;
 
// Demande nom de fichier  
...
//
   oldCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
   handle = BitmapToDIB(BitmapHandle);
   if (handle != (HANDLE)NULL)
   {
     WriteBitmapFile(FileName, handle);
     GlobalFree(handle);
   }
   SetCursor(oldCursor);
 
--------------------------------------------------
--------
WriteBitmapFile()
 
#include <windows.h>
#include <malloc.h>
#include <dos.h>
#include "bitmaps.h"
 
extern LPSTR DIBitmapBits(BITMAPINFO FAR *);
 
/* int WriteBitmapFile(
**  const char *filename, name of file to load
**  const HANDLE hbm); handle to packed DIB
**
**    This function will write the passed packed Device Independant Bitmap
** to the specified file. */
 
int WriteBitmapFile(const char *filename, const HANDLE hbm)
{
 int file;
 int rc = -1;
 int block;
 long size;
 char HUGE *ptr;
 BITMAPFILEHEADER bfHdr;
 LPBITMAPINFO bmi;
 OFSTRUCT ofs;
 
 /* 1. Open output file  */
 file = OpenFile((LPSTR)filename, (LPOFSTRUCT)&ofs, OF_CREATE | OF_WRITE);
 if (file != -1)
 { /*  2. Lock memory resource */
   bmi = (LPBITMAPINFO)GlobalLock(hbm);
   if (bmi != NULL)
   { /* 3. Create BITMAPFILEHEADER and write to file */
     bfHdr.bfType = ('B' + ('M' << 8));
     bfHdr.bfSize = sizeof(BITMAPFILEHEADER) + GlobalSize(hbm);
     bfHdr.bfReserved1 = 0;
     bfHdr.bfReserved2 = 0;
     bfHdr.bfOffBits = sizeof(BITMAPFILEHEADER)
   + (DWORD)(DIBitmapBits(bmi) - (LPSTR)bmi);
     if (_lwrite(file, (LPSTR)&bfHdr, sizeof(bfHdr)) == sizeof(bfHdr))
     { /* 4. Write out DIB header and packed bits to file */
       size = GlobalSize(hbm);
       ptr = (char HUGE *)bmi;
       block = 16 * 1024;   /* size of chunks to write out */
 
       while (size > 0)
       {
  if (size < (long)block) block = (int)size;
  if (_lwrite(file, (LPSTR)ptr, block) != block)
  {
    ErrorBox("WriteBitmapFiile(): Error writing DIB" );
    break;
  }
  size -= (long)block;
  ptr += block;
       }
       if (size == 0) rc = 0;
     }
     else
       ErrorBox("WriteBitmapFile(): Error writing BITMAPFILEHEADER" );
 
     GlobalUnlock(hbm);
   }
   else
     ErrorBox("WriteBitmapFile(): Error locking bitmap into memory" );
   _lclose(file);
 }
 else
   ErrorBox("WriteBitmapFile(): Error opening output file" );
 if (rc != 0) unlink(filename);
 return rc;
}

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed