CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Copy CImage to clipboard

    Code:
      
      CImage tmpImage = pDoc->m_imageArray[0];
         
        int w = tmpImage.GetWidth();
        int h = tmpImage.GetHeight();
        int Bpp = tmpImage.GetBPP();
         
        BITMAPINFOHEADER bmInfohdr;
        bmInfohdr.biSize = sizeof(BITMAPINFOHEADER);
        bmInfohdr.biWidth = w;
        bmInfohdr.biHeight = -h;
        bmInfohdr.biPlanes = 1;
        bmInfohdr.biBitCount = Bpp;
        bmInfohdr.biCompression = BI_RGB;
        bmInfohdr.biSizeImage = w*h*Bpp;
        bmInfohdr.biXPelsPerMeter = 0;
        bmInfohdr.biYPelsPerMeter = 0;
        bmInfohdr.biClrUsed = 0;
        bmInfohdr.biClrImportant = 0;
         
        BITMAPINFO bmInfo;
        bmInfo.bmiHeader = bmInfohdr;
        bmInfo.bmiColors[0].rgbBlue=255;
         
         
                void* pBits = tmpImage.GetBits();
                HANDLE hData = ::GlobalAlloc (GMEM_MOVEABLE, sizeof(BITMAPINFO) + w * h * 3);
                LPVOID pData = (LPVOID) ::GlobalLock (hData);
                LPBYTE p_imagebits;
                p_imagebits  = (LPBYTE)pData + sizeof(BITMAPINFO);
                memcpy(pData,&bmInfo,sizeof(BITMAPINFO));
         
               
                DWORD dwBytes = ((DWORD) w * Bpp) / 32;
                        if(((DWORD) w * Bpp) % 32) {
                                dwBytes++;
                        }
                        dwBytes *= 4;
                        unsigned long m_dwSizeImage = dwBytes * h; // no compression
         
         
                memcpy (p_imagebits, pBits, m_dwSizeImage);
                ::GlobalUnlock (hData);
         
        COleDataSource* pods = new COleDataSource;
        pods->CacheGlobalData (CF_DIB, hData);
        pods->SetClipboard ();

    I'm trying to copy the DIB from the CImage into the clipboard. I get an access read violation on the second memcpy. Can anyone explain what I've done wrong?.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Copy CImage to clipboard

    Quote Originally Posted by jcjc View Post
    Code:
      
                HANDLE hData = ::GlobalAlloc (GMEM_MOVEABLE, sizeof(BITMAPINFO) + w * h * 3);
                // ...
                DWORD dwBytes = ((DWORD) w * Bpp) / 32;
                if(((DWORD) w * Bpp) % 32) {
                        dwBytes++;
                }
                dwBytes *= 4;
                unsigned long m_dwSizeImage = dwBytes * h; // no compression
                // ...
                memcpy (p_imagebits, pBits, m_dwSizeImage);
    How many bytes did you allocate and how many are you copying?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Copy CImage to clipboard

    CImage::GetBits:
    Return Value

    A pointer to the bitmap buffer. If the bitmap is a bottom-up DIB, the pointer points near the end of the buffer. If the bitmap is a top-down DIB, the pointer points to the first byte of the buffer.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured