Home > Software engineering >  VC image processing, after the SetDIBits getdibits again after only the last in the text, the origin
VC image processing, after the SetDIBits getdibits again after only the last in the text, the origin

Time:09-26


11. BMP FILE * fp=fopen (" ", "rb");
If (fp==0)
return;

Fseek (fp, sizeof (BITMAPFILEHEADER), 0).
BITMAPINFOHEADER head;
Fread (& amp; The head, sizeof (BITMAPINFOHEADER), 1, fp);
Int bmpHeight=head. BiHeight;
Int bmpWidth=head. BiWidth;
Int biBitCount=head. BiBitCount;
//int lineByte=(bmpWidth * biBitCount/8 + 3)/4 * 4;
Int nBpp=head. BiBitCount;
//int lineByte=(bmpWidth * nBpp + 31)/32 * 4;//BMP 4 bytes align
Int lineByte=4 * (bmpWidth * biBitCount + 31)/32;


Unsigned char * pBmpBuf=new unsigned char [lineByte * bmpHeight];
Fread (pBmpBuf, 1, lineByte * bmpHeight, fp);


The CDC * m_pMemDC;
M_pMemDC=new CDC ();
The CDC * pDC.
PDC=GetDC ();
M_pMemDC - & gt; CreateCompatibleDC (pDC);
CBitmap * m_pMemBmp=new CBitmap ();//according to the size of the image to create a compatible bitmap
M_pMemBmp - & gt; CreateCompatibleBitmap (pDC, bmpWidth, bmpHeight);
M_pMemDC - & gt; SelectObject (m_pMemBmp);


////the image data to the compatible bitmap
Int ret=SetDIBits (pDC - & gt; GetSafeHdc (), (HBITMAP) m_pMemBmp - & gt; GetSafeHandle (),
0, bmpHeight, (LPVOID) pBmpBuf, (LPBITMAPINFO) & amp; The head, DIB_RGB_COLORS);


////////////////////////////////
M_pMemDC - & gt; SetBkMode (TRANSPARENT);
M_pMemDC - & gt; SetTextColor (RGB (255, 255, 255));


//set the font
CFont font;
Font. CreatePointFont (5000, "tahoma");//font size + font name

M_pMemDC - & gt; SelectObject (& amp; The font);
//to add text to the specified location
M_pMemDC - & gt; Hello1111 TextOut (100, 100, "");

/////////////////////only "hello111
"

Unsigned char * pTemp=new unsigned char [lineByte * bmpHeight];


BITMAPINFO * m_pBMI;

BITMAPINFOHEADER bi;
BITMAP BM.


//CClientDC dc (this);
Bi. BiSize=sizeof (BITMAPINFOHEADER);
Bi. BiWidth=bmpWidth;
Bi. BiHeight=bmpHeight;
Bi. BiPlanes=1;
Bi. BiBitCount=1;
Bi. BiCompression=0;
Bi. BiSizeImage=0;
Bi. BiXPelsPerMeter=0;
Bi. BiYPelsPerMeter=0;
Bi. The biClrUsed=0;
Bi. BiClrImportant=0;
//bi. BiSizeImage=head. BiSizeImage;

M_pBMI=(BITMAPINFO *) new char [sizeof (BITMAPINFO)];

Memcpy (m_pBMI, & amp; Bi, sizeof (BITMAPINFOHEADER));

Ret=GetDIBits (m_pMemDC - & gt; GetSafeHdc (), (HBITMAP) m_pMemBmp - & gt; GetSafeHandle (), 0 l, (DWORD) bmpHeight,
(LPVOID pTemp), (LPBITMAPINFO) & amp; The head (DWORD) DIB_RGB_COLORS);


Int dw=GetLastError ();


///////////////////////////the following written tiff
Uint32 width, height;
Uint16 the depth=1;

TIFF * out=TIFFOpen (" 21112 tif ", "w");//open the TIFF file

TIFFSetField (out, TIFFTAG_SUBFILETYPE FILETYPE_PAGE);//that figure is more frames of
Int nCur;//the number of frames
Int nTotal;//the total number of frames
TIFFSetField (out, TIFFTAG_PAGENUMBER nCur, nTotal);//set the frame attribute

Width=bmpWidth;
Height=bmpHeight;

TIFFSetField (out, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField (out, TIFFTAG_IMAGELENGTH, height);
TIFFSetField (out, TIFFTAG_BITSPERSAMPLE, 1);
TIFFSetField (out, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField (out, TIFFTAG_ROWSPERSTRIP, height);

TIFFSetField (out, TIFFTAG_COMPRESSION COMPRESSION_CCITTFAX4);
TIFFSetField (out, TIFFTAG_PHOTOMETRIC PHOTOMETRIC_MINISWHITE);
TIFFSetField (out, TIFFTAG_FILLORDER FILLORDER_MSB2LSB);
TIFFSetField (out, TIFFTAG_PLANARCONFIG PLANARCONFIG_CONTIG);

TIFFSetField (out, TIFFTAG_XRESOLUTION, 300);
TIFFSetField (out, TIFFTAG_YRESOLUTION, 300);
TIFFSetField (out, TIFFTAG_RESOLUTIONUNIT RESUNIT_INCH);


//TIFF by LZH compression

Uint32 offset, size;
Char * scanbuf;
Size=((width * biBitCount + 31)/32) * 4;
Scanbuf=(char *) _TIFFmalloc (size);
Int nBitsPerPixel=biBitCount;
For (int nLines=0; NLines & lt; Height; NLines++)
{
DWORD dwWidthBytes=((width * biBitCount + 31)/32) * 4;
LPBYTE p=new BYTE [dwWidthBytes];
Memcpy (p, pTemp + (height - 1 - nLines) * dwWidthBytes, dwWidthBytes);
//from BGR to RGB
If (nBitsPerPixel==24 | | nBitsPerPixel==32)
{
LPBYTE pTempData=https://bbs.csdn.net/topics/p;
for(int i=0; I & lt; Width; I++)
{
BYTE temp=* pTempData;//R
* * pTempData=https://bbs.csdn.net/topics/(pTempData + 2);
* (pTempData + 2)=temp;
//m_dwBitsPerPixel may be 32 or 24
PTempData +=nBitsPerPixel/8;
}
}
TIFFWriteScanline (out, p, nLines, 0);
delete p;
P=NULL;
}

_TIFFfree (scanbuf);
TIFFWriteDirectory (out);

//here, to the TIFF file added a picture, if add the next cycle the
//after all, be sure to
TIFFClose (out);


Before they don't have a Set of background, only the last of a line and a whiteboard

CodePudding user response:

Put the memDC content is copied to the clipboard and see,
  • Related