U E D R , A S I H C RSS

MFC/HBitmap ToBMP

HBITMAP ์—์„œ BMP ํŒŒ์ผ๋กœ ์ถ”์ถœ

~cpp
//*********************************************************************

// CImageTool::ExportAsBMP() 

// Purpose: [CBitmap์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ์ด๋ฏธ์ง€๋ฅผ BmpํŒŒ์ผ๋กœ ๋งŒ๋“ ๋‹ค.] 

// Parameters:     

// [type] : 1 ํ”ฝ์…€๋‹น Bit ์ˆ˜ 

// [*filename] : ๋งŒ๋“ค์–ด์งˆ bmpํŒŒ์ผ์ด๋ฆ„ 

// [*pDC] : Device Context 

// [hBitmap] : ์ด๋ฏธ์ง€๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” CBitmap (HBITMAP์œผ๋กœ ๋ณ€ํ™”ํ•˜์—ฌ ์‚ฌ์šฉ) 

// [nWidth] : ์ด๋ฏธ์ง€์˜ ๋„“์ด 

// [nHeight] : ์ด๋ฏธ์ง€์˜ ๋†’์ด 

// Returns:    BOOL 

//*********************************************************************

BOOL CImageTool::ExportAsBMP(int type, char *filename, CDC *pDC, 

        HBITMAP hBitmap, int nWidth, int nHeight) 

{ 

    BYTE *bmp; 

    BITMAPFILEHEADER header; 

    FILE *fp; 

    unsigned int size, palsize; 

    BOOL r = FALSE; 



    if ((bmp=GetBMP(type, nWidth, nHeight, hBitmap, pDC->m_hDC, 

        &size)) == NULL) 

      return FALSE; 

    if (type != 3) palsize = 1<<bit[type]; 

    else palsize = 0; 

    header.bfType = 0x4D42; 

    header.bfSize = size+sizeof(BITMAPFILEHEADER); 

    header.bfReserved1 = header.bfReserved2=0; 

    header.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ 

                    sizeof(RGBQUAD)*palsize; 

    if ((fp=fopen(filename,"wb")) == NULL) 

    { 

    free(bmp); 

    return FALSE; 

    } 

    if (fwrite(&header,sizeof(BITMAPFILEHEADER),1,fp)!=0) 

    if (fwrite(bmp,size,1,fp)!=0) r=TRUE; 

    free(bmp); 

    fclose(fp); 



    return r; 

} 



//*******************************************************************

// *CImageTool::GetBMP() 

// Purpose: [์ฃผ์–ด์ง„ HBITMAP์—์„œ ์ด๋ฏธ์ง€ ์ •๋ณด๋ฅผ ์ฝ์–ด ์˜ค๋Š” ํ•จ์ˆ˜] 

// Parameters:     

// [type] : 1ํ”ฝ์…€๋‹น Bit์ˆ˜ 

// [w] : ์ด๋ฏธ์ง€ ๋„“์ด 

// [h] : ์ด๋ฏธ์ง€ ๋†’์ด 

// [hBitmap] : ์ด๋ฏธ์ง€๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ๋Š” CBtimap 

// [hdc] : Device Context 

// [*size] : File Size (๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’) 

// Returns:    BYTE 

//*******************************************************************

BYTE *CImageTool::GetBMP(int type, int w, int h, HBITMAP hBitmap, 

        HDC hdc, unsigned int *size) 

{ 

    LPBITMAPINFO lpvBits; 

    BYTE *lpbi; 

    int width,palsize; 



    if (type!=3) palsize = 1<<bit[type]; 

    else palsize = 0; 

    width = ((((w*bit[type]+7)/8+3)/4)*4); 



    *size = sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*palsize+width*h; 

    lpvBits = (BITMAPINFO *)malloc(*size); 



    lpbi = (BYTE *)lpvBits+sizeof(BITMAPINFOHEADER) + 

                    sizeof(RGBQUAD)*palsize; 

    lpvBits->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 

    lpvBits->bmiHeader.biWidth = w; 

    lpvBits->bmiHeader.biHeight = h; 

    lpvBits->bmiHeader.biPlanes = 1; 

    lpvBits->bmiHeader.biBitCount = bit[type]; 

    lpvBits->bmiHeader.biCompression = BI_RGB; 

    lpvBits->bmiHeader.biSizeImage = width*h; 

    lpvBits->bmiHeader.biXPelsPerMeter = 0; 

    lpvBits->bmiHeader.biYPelsPerMeter = 0; 

    lpvBits->bmiHeader.biClrUsed = 1<<bit[type]; 

    lpvBits->bmiHeader.biClrImportant = 1<<bit[type]; 



    GetDIBits(hdc,hBitmap,0,h,lpbi,lpvBits,DIB_RGB_COLORS); 



    return (BYTE *)lpvBits; 

}
์ถœ์ฒ˜:GOSU.NET

thread

๋Œ์•„๋Œ•๊ธฐ๋‹ค๋ณด๋ฉด ์“ธ๋งŒํ•œ ์†Œ์Šค๋“ค์ด ๋„˜์ณ๋‚˜๋Š”๊ตฐ.. -_-; ๋‚œ ์กฐ๋ฆฝ๋งŒ ํ•˜๋ฉด๋œ๋‹ค. ใ…‹ใ…‹ใ…‹ - eternalbleu
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:42
Processing time 0.0066 sec