News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Bitmap in memory to file question

Started by georgek01, March 14, 2007, 01:22:49 PM

Previous topic - Next topic

georgek01

Hello,

This relates to the following topic: http://www.masm32.com/board/index.php?topic=2026.0

I've created a bitmap using CreateCompatibleBitmap and need to persist the information. How would I write it to a file? I've searched the platform SDK but do not find any assistance there.
What we have to learn to do, we learn by doing.

- ARISTOTLE (384-322 BC)

TNick


georgek01

I got the following code (from Iczelion's site http://win32assembly.online.fr/) and customized it a bit. Bitmap is created, but the image comes out black.



.386
.model flat,stdcall

option casemap:none

include windows.inc
include user32.inc
include kernel32.inc

include gdi32.inc

includelib user32.lib
includelib kernel32.lib

includelib gdi32.lib

StringToBMP proto :DWORD

.const


.data?

hInstance dd ?

.data

szText db 'Test string out',0

szFileName db 'c:\test.bmp',0

szDisplay     db "DISPLAY",0
szNoDC        db "Couldn't create device context.",0
szNoMemDC     db "Couldn't create compatible device context.",0
szNoBMP       db "Couldn't create compatible bitmap.",0
szNoObj       db "Couldn't select bitmap.",0
szNoCopy      db "Couldn't copy bitmap.",0
szNoFile      db "Couldn't write file to disk.",0
szDone        db "Bitmap captured to disk file.",0

.code

start:

invoke GetModuleHandle,0
mov hInstance, eax

invoke StringToBMP,addr szFileName

xor eax,eax

invoke ExitProcess,0


StringToBMP Proc lpFileName:DWORD

    LOCAL hdc:HDC
    LOCAL memdc:HDC
    LOCAL hFile:HANDLE
    LOCAL dwBytes:DWORD
    LOCAL bitmapfileheader:BITMAPFILEHEADER
    LOCAL bitmapinfoheader:BITMAPINFOHEADER
    LOCAL colors[256]:RGBQUAD
    LOCAL bmpinfo:BITMAPINFO
    LOCAL hBitmap:HBITMAP
    LOCAL pBits:DWORD
    LOCAL dwWidth:DWORD
    LOCAL dwHeight:DWORD
    LOCAL dwNumColors:DWORD
    LOCAL dwBPP:DWORD
    LOCAL ColorSize:DWORD
   
    LOCAL oldBitmap :DWORD
    LOCAL hBrush :DWORD
   

            invoke CreateSolidBrush, 0FFFFFFh
            mov hBrush, eax
           
            invoke GetDC,NULL
            mov hdc,eax
            .IF (eax==NULL)
                invoke MessageBox, 0, addr szNoDC, NULL, 0
                jmp ExitFunc
            .ENDIF
           
            ;invoke GetDeviceCaps, hdc, HORZRES
            mov dwWidth,200
            ;invoke GetDeviceCaps, hdc, VERTRES
            mov dwHeight,100
            invoke GetDeviceCaps, hdc, BITSPIXEL
            mov dwBPP,eax
            .IF (eax<=8)
                invoke GetDeviceCaps, hdc, NUMCOLORS
                mov dwNumColors,eax
                mov dwNumColors,256 ;this one looks bad
            .ELSE
                mov dwNumColors,0
            .ENDIF
           
            invoke CreateCompatibleDC, hdc
            mov memdc,eax
           
            .IF (eax==NULL)
                invoke DeleteDC, hdc
                invoke MessageBox, 0, addr szNoMemDC, NULL, 0
                jmp ExitFunc
            .ENDIF
           
            invoke SetBkColor,hdc,0FFFFFFH
            invoke SelectObject,hdc,hBrush
            invoke TextOut,hdc,10,10,addr szText,sizeof szText
                       
            mov bmpinfo.bmiHeader.biSize,sizeof BITMAPINFOHEADER
            mov eax,dwWidth
            mov bmpinfo.bmiHeader.biWidth,eax
            mov eax,dwHeight
            mov bmpinfo.bmiHeader.biHeight,eax
            mov bmpinfo.bmiHeader.biPlanes,1
            mov ax,word ptr [dwBPP]
            mov bmpinfo.bmiHeader.biBitCount,ax
            mov bmpinfo.bmiHeader.biCompression,BI_RGB
            mov bmpinfo.bmiHeader.biSizeImage,0
            mov bmpinfo.bmiHeader.biXPelsPerMeter,0
            mov bmpinfo.bmiHeader.biYPelsPerMeter,0
            mov eax,dwNumColors
            mov bmpinfo.bmiHeader.biClrUsed,eax
            mov bmpinfo.bmiHeader.biClrImportant,eax
           
            invoke CreateDIBSection,hdc,addr bmpinfo, DIB_PAL_COLORS,addr pBits, NULL, 0
            mov hBitmap,eax
            .IF (eax==NULL)
                invoke DeleteDC, hdc
                invoke DeleteDC, memdc
                invoke MessageBox, 0, addr szNoBMP, NULL, 0
                jmp ExitFunc
            .ENDIF
                       
            invoke BitBlt, memdc, 0,0, dwWidth, dwHeight, hdc, 0,0, SRCCOPY
            .IF (!eax)
                invoke DeleteDC, hdc
                invoke DeleteDC, memdc
                invoke MessageBox, 0, addr szNoCopy, NULL, 0
                jmp ExitFunc
            .ENDIF
 
            mov eax,dwNumColors
            .IF (eax!=0)
                invoke GetDIBColorTable, memdc, 0, dwNumColors, addr colors
                mov dwNumColors,eax
            .ENDIF
           
            mov bitmapfileheader.bfType,4D42h
            mov eax,dwNumColors
            xor edx,edx
            mov ecx,sizeof RGBQUAD
            mul ecx
            mov ColorSize,eax
            mov eax,dwWidth
            xor edx,edx
            mov ecx,dwHeight
            mul ecx
            xor edx,edx
            mov ecx,dwBPP
            mul ecx
            shr eax,3
            add eax,ColorSize
            add eax,sizeof BITMAPFILEHEADER
            add eax,sizeof BITMAPINFOHEADER
            mov bitmapfileheader.bfSize,eax
            mov bitmapfileheader.bfReserved1,0
            mov bitmapfileheader.bfReserved2,0
            mov eax,ColorSize
            add eax,sizeof BITMAPFILEHEADER
            add eax,sizeof BITMAPINFOHEADER
            mov bitmapfileheader.bfOffBits,eax
            mov bitmapinfoheader.biSize,sizeof BITMAPINFOHEADER
            mov eax,dwWidth
            mov bitmapinfoheader.biWidth,eax
            mov eax,dwHeight
            mov bitmapinfoheader.biHeight,eax
            mov bitmapinfoheader.biPlanes,1
            mov ax,word ptr [dwBPP]
            mov bitmapinfoheader.biBitCount,ax
            mov bitmapinfoheader.biCompression,BI_RGB
            mov bitmapinfoheader.biSizeImage,0
            mov bitmapinfoheader.biXPelsPerMeter,0
            mov bitmapinfoheader.biYPelsPerMeter,0
            mov eax,dwNumColors
            mov bitmapinfoheader.biClrUsed,eax
            mov bitmapinfoheader.biClrImportant,0
           
            invoke CreateFile, lpFileName,GENERIC_WRITE,0,\
                    NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
            mov hFile,eax
           
            .IF (eax==INVALID_HANDLE_VALUE)
                invoke DeleteObject, hBitmap
                invoke DeleteDC, memdc
                invoke DeleteDC, hdc
                invoke MessageBox, 0, addr szNoFile, NULL, 0
                jmp ExitFunc
            .ENDIF
           
            invoke WriteFile, hFile, addr bitmapfileheader, sizeof BITMAPFILEHEADER, addr dwBytes, NULL
            invoke WriteFile, hFile, addr bitmapinfoheader, sizeof BITMAPINFOHEADER, addr dwBytes, NULL
           
            mov eax,dwNumColors
            .IF (eax!=0)
                invoke WriteFile, hFile, addr colors, ColorSize, addr dwBytes,NULL
            .ENDIF
           
            mov eax,dwWidth
            xor edx,edx
            mov ecx,dwHeight
            mul ecx
            xor edx,edx
            mov ecx,dwBPP
            mul ecx
            shr eax,3
            mov ColorSize,eax
           
            invoke WriteFile, hFile, pBits, ColorSize, addr dwBytes,NULL
            invoke CloseHandle, hFile
            invoke MessageBox, 0, addr szDone, NULL, 0
           
            invoke SelectObject,hdc,oldBitmap
           
            invoke ReleaseDC,NULL,hdc
           
            invoke DeleteObject ,hBitmap
            invoke DeleteDC, memdc
            invoke DeleteDC, hdc
ExitFunc:
xor eax, eax
            ret
StringToBMP endp


end start

What we have to learn to do, we learn by doing.

- ARISTOTLE (384-322 BC)

TNick

Try to send to Grincheux a PM and, if he has time, I'm shure he can help. His tool - I-View - is made in assembly and you can download sources from here.

Nick


georgek01

I managed to get the app working. I'm using an INI file to specify the contents of the image (e.g. header & footer information, colour and text styles). Preferable I want to use an XML document defining all the image parts, so I'll add this functionality once I have some time.

Thank you all for your assistance.
What we have to learn to do, we learn by doing.

- ARISTOTLE (384-322 BC)