Discussion:
how to insert bitmap into an rtf file
(too old to reply)
Lcubed
2009-04-03 15:31:45 UTC
Permalink
This may not be just a pocket pc question, but since I'm generating
the RTF (Rich Text Format) file on my pocket PC, I'll ask it here.
Using C++, I'm creating an RTF file that contains a bitmap. However,
when I copy it from my pocket pc to my PC and look at the file with
WordPad, the bitmap is there but not visible. Word will only display
graphics in RTF files if they are formatted as metafiles.

I suspect the problem is that I'm not exactly sure how to calculate
picscalex, picscaley, picwgoal, and pichgoal
and have not found much information online about this. But I know if I
try to change those parameters in rtf files I have that work in
WordPad, the bitmaps become "unreadable" for WordPad.

Can anyone shed any light on this or point me in the right direction?

Thanks!
p***@gmail.com
2009-04-21 12:00:48 UTC
Permalink
Hi,

Here is the main part of a function I used to save a 2 color (black &
white) image into RTF document this should give you all the
information you need:



PBITMAPINFO pbmi = NULL;
WORD cClrBits = 1;
int nColors = 2;
RGBQUAD Colors2[] =
{
{0x00, 0x00, 0x00, 0x00},
{0xFF, 0xFF, 0xFF, 0x00}
};

int nBytesPerLine = ((nWidth * cClrBits + 31)&(~31)) / 8;
DWORD dwBitmapInfoSize = sizeof(BITMAPINFOHEADER) + nColors * sizeof
(RGBQUAD);
DWORD dwFileHeaderSize = dwBitmapInfoSize + sizeof(BITMAPFILEHEADER);
HBITMAP hBmp;
LPVOID ppvBits;
BYTE *lpBits, *lpBuf = new BYTE[dwBitmapInfoSize + 1];
BITMAPINFO* lpbi = (BITMAPINFO*) lpBuf;
BITMAPFILEHEADER bmpfhdr;
BITMAPINFOHEADER& bih = lpbi->bmiHeader;

// Initialize the fields in the BITMAPINFO structure.
memset(&bih, 0, sizeof(BITMAPINFOHEADER));
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = nWidth;
bih.biHeight = nHeight;
bih.biPlanes = 1;
bih.biBitCount = 1;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;

RGBQUAD* pColors = &Colors2[0];
memcpy(&lpbi->bmiColors[0], &pColors[0], sizeof(RGBQUAD));
memcpy(&lpbi->bmiColors[1], &pColors[1], sizeof(RGBQUAD));

bmpfhdr.bfType = 0x4d42; //'BM' file type
bmpfhdr.bfSize = dwFileHeaderSize + (nBytesPerLine * nHeight);
bmpfhdr.bfReserved1 = 0;
bmpfhdr.bfReserved2 = 0;
bmpfhdr.bfOffBits = dwFileHeaderSize;

// Then create our DIBSection using the structure manually created
from above...
HBITMAP hPictureBMP = CreateDIBSection(pictureDC, lpbi,
DIB_RGB_COLORS, &ppvBits, NULL, 0);
lpBits = (BYTE*)ppvBits;
HDC hMemDC = CreateCompatibleDC(pictureDC);

hBmp = (HBITMAP) SelectObject(hMemDC, hPictureBMP);
BitBlt(hMemDC,0,0,nWidth,nHeight,pictureDC,0,0,SRCCOPY);
SelectObject(hMemDC, hBmp);
DeleteObject(hPictureBMP);

DWORD dwNumBytes;

if (ppvBits)
{
char szBuffer[MAX_BUFFER];

// pichgoal = 1440 / DPI = 1440 / 96 = 15
int rtfHeaderSize = sprintf(szBuffer,"{\\pict\\dibitmap0\
\wbmbitspixel%u\\wbmplanes%u\\wbmwidthbytes%u\\picw%u\\pich%u\\picwgoal
%u\\pichgoal%u\\picscalex100\\picscaley100 ", bih.biBitCount,
bih.biPlanes, nBytesPerLine, nWidth, nHeight, nWidth*16, nHeight*16);
{
int offset;
for (offset=0; offset < (int)dwBitmapInfoSize; offset++)
sprintf(szBuffer+rtfHeaderSize+offset*2,"%02X",lpBuf[offset]);
for (offset=0; offset<nBytesPerLine * nHeight; ++offset)
sprintf(szBuffer+rtfHeaderSize+(dwBitmapInfoSize+offset)
*2,"%02X",lpBits[offset]);
}
szBuffer[rtfHeaderSize + (dwBitmapInfoSize + nBytesPerLine *
nHeight) * 2] = '}';
szBuffer[rtfHeaderSize + (dwBitmapInfoSize + nBytesPerLine *
nHeight) * 2 + 1] = 0;

// Insert bitmap into document (there is enough rooom!!)
memmove(documentSignature + strlen(szBuffer), documentSignature,
strlen(documentSignature) + 1);
memcpy (documentSignature, szBuffer, strlen(szBuffer));
}
delete[] lpBuf;
DeleteObject(hBmp);
DeleteDC(hMemDC);
Post by Lcubed
This may not be just a pocket pc question, but since I'm generating
the RTF (Rich Text Format) file on my pocket PC, I'll ask it here.
Using C++, I'm creating an RTF file that contains a bitmap. However,
when I copy it from my pocket pc to my PC and look at the file with
WordPad, the bitmap is there but not visible. Word will only display
graphics in RTF files if they are formatted as metafiles.
I suspect the problem is that I'm not exactly sure how to calculate
picscalex, picscaley, picwgoal, and pichgoal
and have not found much information online about this. But I know if I
try to change those parameters in rtf files I have that work in
WordPad, the bitmaps become "unreadable" for WordPad.
Can anyone shed any light on this or point me in the right direction?
Thanks!
Loading...