Blank image as Layer

For everything that's not in any way related to PureBasic. General chat etc...
sidegame
New User
New User
Posts: 7
Joined: Wed Feb 22, 2012 2:38 pm

Blank image as Layer

Post by sidegame »

Hi there! :D
I am user of both game maker (Good) and purebasic (beginner + learning…)
I am creating a paint program in game maker and I need a DLL for creating a blank image as layer in game maker does anyone know how to create a DLL using Purebasic so that it create blank images as layers in game maker, because I am beginner in purebasic so I can’t do this by my own self can anyone help me with this plz?

Any help is appreciate :wink:
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Blank image as Layer

Post by netmaestro »

You could try this, seems like it ought to work, though I know nothing of GameMaker's image layers. This creates a 32bit transparent image that should serve. An alternative would be to use PB's EncodeImage command to create a PNG or JPG if GM has the decoder for that.

Code: Select all

Procedure SaveImage2Memory(hBmp) 
  
  GetObject_(hBmp, SizeOf(BITMAP), @Bmp.BITMAP)
  
  With bmiInfo.BITMAPINFOHEADER
    \biSize         = SizeOf(BITMAPINFOHEADER)
    \biWidth        = bmp\bmWidth
    \biHeight       = bmp\bmHeight
    \biPlanes       = 1
    \biBitCount     = 32
    \biCompression  = #BI_RGB 
  EndWith
  
  *pPixels = AllocateMemory(bmp\bmWidthBytes*bmp\bmHeight)
  hDC = GetWindowDC_(#Null)
  iRes = GetDIBits_(hDC, hBmp, 0, bmp\bmHeight, *pPixels, @bmiInfo, #DIB_RGB_COLORS)
  ReleaseDC_(#Null, hDC)
  
  ImageSize = SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER) +  MemorySize(*pPixels)
  
  *rawimage = AllocateMemory(ImageSize)
  *fileheader.BITMAPFILEHEADER = *rawimage
  *header.BITMAPINFOHEADER = *rawimage + SizeOf(BITMAPFILEHEADER)
  
  With *fileheader
    \bfType = 19778 ; word containing 2 bytes, 'BM' for 'BIT' 'MAP' ;)
    \bfSize = ImageSize
    \bfOffBits = SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER)
  EndWith
  
  CopyMemory(@BmiInfo, *header, SizeOf(BITMAPINFOHEADER))
  
  CopyMemory(*pPixels, *rawimage + SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER), MemorySize(*pPixels)) 
  FreeMemory(*pPixels)
  ProcedureReturn *rawimage
EndProcedure

ProcedureDLL CreateBlankImage(szw, szh)
  hImage = CreateImage(#PB_Any, szw, szh, 32|#PB_Image_Transparent)
  If hImage
    *this = SaveImage2Memory(ImageID(hImage))
    FreeImage(hImage)
    ProcedureReturn *this
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

ProcedureDLL FreeBlankImage(*address)
  If FreeMemory(*address)
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
EndProcedure
One thing to watch for in this code is that you will probably cause a crash if you try to free the same image address twice. The first time will succeed but there is no IsMemory function to test if the memory is valid so a subsequent call with the same address might crash. I didn't test it but it seems likely.
BERESHEIT
sidegame
New User
New User
Posts: 7
Joined: Wed Feb 22, 2012 2:38 pm

Re: Blank image as Layer

Post by sidegame »

netmaestro wrote:You could try this, seems like it ought to work, though I know nothing of GameMaker's image layers. This creates a 32bit transparent image that should serve. An alternative would be to use PB's EncodeImage command to create a PNG or JPG if GM has the decoder for that.

Code: Select all

Procedure SaveImage2Memory(hBmp) 
  
  GetObject_(hBmp, SizeOf(BITMAP), @Bmp.BITMAP)
  
  With bmiInfo.BITMAPINFOHEADER
    \biSize         = SizeOf(BITMAPINFOHEADER)
    \biWidth        = bmp\bmWidth
    \biHeight       = bmp\bmHeight
    \biPlanes       = 1
    \biBitCount     = 32
    \biCompression  = #BI_RGB 
  EndWith
  
  *pPixels = AllocateMemory(bmp\bmWidthBytes*bmp\bmHeight)
  hDC = GetWindowDC_(#Null)
  iRes = GetDIBits_(hDC, hBmp, 0, bmp\bmHeight, *pPixels, @bmiInfo, #DIB_RGB_COLORS)
  ReleaseDC_(#Null, hDC)
  
  ImageSize = SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER) +  MemorySize(*pPixels)
  
  *rawimage = AllocateMemory(ImageSize)
  *fileheader.BITMAPFILEHEADER = *rawimage
  *header.BITMAPINFOHEADER = *rawimage + SizeOf(BITMAPFILEHEADER)
  
  With *fileheader
    \bfType = 19778 ; word containing 2 bytes, 'BM' for 'BIT' 'MAP' ;)
    \bfSize = ImageSize
    \bfOffBits = SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER)
  EndWith
  
  CopyMemory(@BmiInfo, *header, SizeOf(BITMAPINFOHEADER))
  
  CopyMemory(*pPixels, *rawimage + SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER), MemorySize(*pPixels)) 
  FreeMemory(*pPixels)
  ProcedureReturn *rawimage
EndProcedure

ProcedureDLL CreateBlankImage(szw, szh)
  hImage = CreateImage(#PB_Any, szw, szh, 32|#PB_Image_Transparent)
  If hImage
    *this = SaveImage2Memory(ImageID(hImage))
    FreeImage(hImage)
    ProcedureReturn *this
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

ProcedureDLL FreeBlankImage(*address)
  If FreeMemory(*address)
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
EndProcedure
One thing to watch for in this code is that you will probably cause a crash if you try to free the same image address twice. The first time will succeed but there is no IsMemory function to test if the memory is valid so a subsequent call with the same address might crash. I didn't test it but it seems likely.

Thanks :) i will check it out see if it work or not
Post Reply