April 26, 2024, 06:20:11 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Still working on saveing bmps from handle

Started by TexasPete, December 11, 2010, 10:01:35 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

TexasPete

I have been playing around with the following code. It works fine for saving a screen image to bitmaps.  However, I am trying to modify it to save from the original bmp handle from the loadimage command. It works fine forsaving the image using the new handle from the image printed on the screen, Using "hdcWin = GetHDC(w1)": I have tried to substitute the original handle name "bitmap" and then Save from that handle name. The picture never saves. Since , Bitmap handle name was used to print the image to the screen using showimage. My question is this why can't the orignal handle be used to save the bitmap back  using "GetDIBits" api Call.
I am sure there is something I don't understand. If any one can explan it to me I am willing to learn.

Thanks
Texas Pete







SUB SaveBitmap()
'Modification of Paul Turley's bmp saving routine.
'This one saves the bmp whose handle is *bitmap*
'which will be the last one displayed by the program.
'(Paul's original code grabbed the image from the window.)

def hdcWin,quadsize:int
def info:BITMAPINFOHEADER
def fileheader:BITMAPFILEHEADER
def lpbits,lpbminfo:MEMORY
def file:BFILE
hdcWin = GetHDC(w1)
'fill in the BITMAPINFOHEADER and compute the color data
quadsize = CreateInfoStructure(w1,bitmap,info)
'allocate memory and get the 'bits' of the bitmap
allocmem lpbits,1,info.biSizeImage
allocmem lpbminfo,1,len(info) + quadsize
writemem lpbminfo,1,info
GetDIBits(hdcWin,bitmap,0,info.biHeight,lpbits,lpbminfo,0)
'open a binary file and write the header,color data and bitmap bits
if(openfile(file,savefilename,"W") = 0)
fileheader.bfType = 0x4d42
fileheader.bfSize = len(fileheader) + info.biSize + quadsize + info.biSizeImage
fileheader.bfOffBits = len(fileheader) + info.biSize + quadsize
write file,fileheader
write file,lpbminfo
write file,lpbits
closefile file
endif
freemem lpbminfo
freemem lpbits
ReleaseHDC w1,hdcWin
RETURN

SUB CreateInfoStructure(win:WINDOW,hbitmap:int,info:BITMAPINFOHEADER)
def bmp:BITMAP
def mem:MEMORY
def cClrBits:WORD
def quadsize:int
Allocmem mem,1,len(bmp)
GetObjectA(hbitmap,len(bmp),mem)
readmem mem,1,bmp
freemem mem
cClrBits = bmp.bmPlanes * bmp.bmBitsPixel

    if (cClrBits = 1)
        cClrBits = 1
    else
if (cClrBits <= 4)
      cClrBits = 4
    else
if (cClrBits <= 8)
        cClrBits = 8
    else
if (cClrBits <= 16)
        cClrBits = 16
    else
if (cClrBits <= 24)
        cClrBits = 24
    else
        cClrBits = 32
endif
endif
endif
endif
endif
    info.biSize = len(info)
    info.biWidth = bmp.bmWidth
    info.biHeight = bmp.bmHeight
    info.biPlanes = bmp.bmPlanes
    info.biBitCount = bmp.bmBitsPixel
    if (cClrBits < 24)
        info.biClrUsed = 2^cClrBits
endif
info.biCompression = 0
info.biSizeImage = (info.biWidth + 7) / 8 * info.biHeight * cClrBits
if(cClrBits <> 24)
quadsize = 4 * (2^cClrBits)
endif
RETURN quadsize






LarryMc

In the code you posted where is the "bitmap" variable being set?
The way your code is written it would have to be a global variable defined at the beginning of your program.
Just checking.

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

TexasPete

December 18, 2010, 08:20:34 AM #2 Last Edit: December 18, 2010, 08:27:15 AM by TexasPete
Larry the bitmap is set earlier in the program. When the image is first loaded. The Bitmap is assigned a handle. I am using that handle.  I am now looking at "GetObject" to see if it will return a handle that is savable. I have been
researching this all week. I am looking at the allapi today to see if I can find an example. I have found source code in c++ to write and save a bitmap. I am not familiar with using memory locations so this is difficult for me.
I have been looking thru my windows Graphics Book. I thing a need to understand "DIB" Device Independant Bitmaps.



Thanks
Texas Pete