Rick wrote:
> "Rick" <rickcool22@hotmail.com> wrote in message
> news:420b258e$0$200$edfadb0f@dread11.news.tele.dk...
>
>>Hvordan får jeg det i 24bit for eksempel?
>
>
> Jeg har selv fundet ud af det nu
>
> Ved at lave en BITMAPINFO og i stedet oprette bitmappet med
> CreateDIBSection.
>
Man kan også løse problemet ved at bruge den oprindelige dc
i kaldet til CreateCompatibleBitmap. Hvis man bruger en memorydc,
som i mit eksempel bliver det altid et monokromt bitmap.
Husk at hvis din kode kører ved WM_PAINT skal den kalde BeginPaint
og EndPaint.
Jeg arbejdede lidt videre med koden, nu skriver den billedet
til en fil, første gang den er kaldt:
BITMAPINFOHEADER WriteHeader(FILE *aFile, int aWidth, int aHeight, WORD
aBitPixel)
{
BITMAPFILEHEADER BitmapFileHeader;
BITMAPINFOHEADER BitmapInfoHeader;
unsigned int WordsPerLine = (aWidth*aBitPixel + 31)/32;
BitmapFileHeader.bfType = (('M' << 8) | 'B');
BitmapFileHeader.bfSize = sizeof(BitmapFileHeader) +
sizeof(BitmapInfoHeader) + 4*WordsPerLine*aHeight;
BitmapFileHeader.bfReserved1 = 0;
BitmapFileHeader.bfReserved2 = 0;
BitmapFileHeader.bfOffBits = 0x36 + 8;
BitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfoHeader.biWidth = aWidth;
BitmapInfoHeader.biHeight = aHeight;
BitmapInfoHeader.biPlanes = 1;
BitmapInfoHeader.biBitCount = aBitPixel;
BitmapInfoHeader.biCompression = BI_RGB;
BitmapInfoHeader.biSizeImage = 0;
BitmapInfoHeader.biXPelsPerMeter = 0;
BitmapInfoHeader.biYPelsPerMeter = 0;
BitmapInfoHeader.biClrUsed = 0;
BitmapInfoHeader.biClrImportant = 0;
fwrite(&BitmapFileHeader, sizeof(BitmapFileHeader), 1, aFile);
fwrite(&BitmapInfoHeader, sizeof(BitmapInfoHeader), 1, aFile);
return BitmapInfoHeader;
};
void OnDraw(HWND hwnd)
{
PAINTSTRUCT PaintStruct;
BeginPaint(hwnd, &PaintStruct);
HFONT Font = CreateFont(16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
"Arial");
HDC dc = PaintStruct.hdc;
HDC MemDc = CreateCompatibleDC(dc);
SelectObject(MemDc, Font);
HBITMAP Bitmap = CreateCompatibleBitmap(dc, 100, 20);
HGDIOBJ OldBitmap = SelectObject(MemDc, Bitmap);
RECT Rect = {0, 0, 100, 20};
HBRUSH Brush = CreateSolidBrush(RGB(255, 128, 128));
FillRect(MemDc, &Rect, Brush);
SetBkMode(MemDc, TRANSPARENT);
TextOut(MemDc, 0, 0, "Whatever", 8);
BitBlt(dc, 10, 10, 100, 20, MemDc, 0, 0, SRCCOPY);
SelectObject(MemDc, OldBitmap);
if(First)
{
First = false;
BITMAP Bm;
GetObject(Bitmap, sizeof(Bm), &Bm);
FILE *f = fopen("whatever.bmp", "wb");
if(f)
{
BITMAPINFOHEADER Header = WriteHeader(f, 100, 20, Bm.bmBitsPixel);
char *Buffer = new char [100*20*Bm.bmBitsPixel/8];
GetDIBits(dc, Bitmap, 0, 20, Buffer, (BITMAPINFO *)&Header,
DIB_RGB_COLORS);
fwrite(Buffer, 100*20*Bm.bmBitsPixel/8, 1, f);
fclose(f);
}
}
EndPaint(hwnd, &PaintStruct);
DeleteDC(MemDc);
DeleteObject(Font);
DeleteObject(Brush);
DeleteObject(Bitmap);
}
--
Absolutely not the best homepage on the net:
http://home20.inet.tele.dk/midgaard
But it's mine - Bertel