本文介绍了我如何将CImage DIB复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.我试图将CImage复制到剪贴板.这就是我所拥有的.我在第二个memcpy上遇到了读取访问冲突(应该是在复制图像的位).如何将CImage复制到剪贴板?


hi. im trying to copy a CImage to the clipboard. this is what I''ve got. I get a read access violation on the second memcpy (which is supposed to be copying the bits of the image). how do i copy a CImage to the clipboard?


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 ();

推荐答案


SIZE_T dwColTableLen = (Bpp <= 8) ? (1 << Bpp) * sizeof(RGBQUAD) : 0;
HANDLE hData = ::GlobalAlloc(GMEM_MOVEABLE, 
    sizeof(BITMAPINFOHEADER) + dwColTableLen + m_dwSizeImage);
...
if (dwColTableLen)
{
    LPBYTE lpColorTable = (LPBYTE)pData + sizeof(BITMAPINFOHEADER);
    // pColors must point to the source color table
    memcpy(lpColorTable, pColors, dwColTableLen);
}
p_imagebits  = (LPBYTE)pData + sizeof(BITMAPINFOHEADER) + dwColTableLen;


这篇关于我如何将CImage DIB复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:32