DrawTransparentImage and clip it

Just starting out? Need help? Post your questions and find answers here.
porfirio
Enthusiast
Enthusiast
Posts: 111
Joined: Mon Nov 15, 2004 11:00 pm
Location: portugal

DrawTransparentImage and clip it

Post by porfirio »

Hi.

I want to know how do i draw a transparent image cliped
I know how to draw it and i tryed to clip the image with no sucess

have this :

Code: Select all

Procedure DrawTransparentImage(DC, Bitmap, x, y, Width, Height, TransparentColor)
  maskDC = CreateCompatibleDC_(DC)
  tempDC = CreateCompatibleDC_(DC)
  SourceDC = CreateCompatibleDC_(DC)
  SelectObject_(SourceDC, Bitmap)
  hMaskBmp = CreateBitmap_(Width, Height, 1, 1, 0)
  hTempBmp = CreateCompatibleBitmap_(DC, Width, Height)
  hMaskBmp = SelectObject_(maskDC, hMaskBmp)
  hTempBmp = SelectObject_(tempDC, hTempBmp)
  TransparentColor= SetBkColor_(SourceDC, TransparentColor)
  BitBlt_ (maskDC, 0, 0, Width, Height, SourceDC, 0, 0, #SrcCopy)
  SetBkColor_(SourceDC, TransparentColor)
  BitBlt_ (tempDC, 0, 0, Width, Height, maskDC, 0, 0, #SrcCopy)
  BitBlt_ (DC, x, y, Width, Height, tempDC, 0, 0, #MergePaint)
  BitBlt_ (maskDC, 0, 0, Width, Height, maskDC, 0, 0, #NotSrcCopy)
  BitBlt_ (tempDC, 0, 0, Width, Height, SourceDC, 0, 0, #SrcCopy)
  BitBlt_ (tempDC, 0, 0, Width, Height, maskDC, 0, 0, #MergePaint)
  BitBlt_ (DC, x, y, Width, Height, tempDC, 0, 0, #SrcAnd)
  DeleteObject_ (hMaskBmp)
  DeleteObject_ (hTempBmp)
  DeleteDC_ (maskDC)
  DeleteDC_ (tempDC)
  DeleteDC_ (SourceDC)
EndProcedure
and this :

Code: Select all

Procedure TransparentRGB(img,x,y,RGB)
HLC=ImageList_Create_(ImageWidth(),ImageHeight(),#ILC_COLOR32|#ILC_MASK,1,0)
  ImageList_AddMasked_(HLC,img,RGB)
  DC=StartDrawing(WindowOutput())
  ImageList_Draw_(HLC,0,DC,x,y,#ILD_TRANSPARENT)
  StopDrawing()
  ImageList_Destroy_(HLC)
EndProcedure 
then i tryed this with both:

Code: Select all

BitBlt_(DC,0,0,20,20,tempDC,20,0,#SrcCopy)
It clip it and show it good but i still see the first i drawed
Sadly theres no #SrcReplace

I hope you can help me!
Forgive-me for my english, i'm portuguese!
Fred
Administrator
Administrator
Posts: 18254
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

May be you can give a try to SelectClipRgn_() it should do what you need.
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

porfiro: if you've found a solution, can you give it to us all? I'm also working on this.
bye,
Daniel
porfirio
Enthusiast
Enthusiast
Posts: 111
Joined: Mon Nov 15, 2004 11:00 pm
Location: portugal

Post by porfirio »

I have been studing WIN32 API and i could get that :)

*Heres the image i used
Image

Code: Select all

Procedure DrawTransparentImage(DC, Bitmap, x, y, Width, Height, TransparentColor)
  maskDC = CreateCompatibleDC_(DC)
  tempDC = CreateCompatibleDC_(DC)
  SourceDC = CreateCompatibleDC_(DC)
  SelectObject_(SourceDC, Bitmap)
  hMaskBmp = CreateBitmap_(Width, Height, 1, 1, 0)
  hTempBmp = CreateCompatibleBitmap_(DC, Width, Height)
  hMaskBmp = SelectObject_(maskDC, hMaskBmp)
  hTempBmp = SelectObject_(tempDC, hTempBmp)
  TransparentColor= SetBkColor_(SourceDC, TransparentColor)
  BitBlt_ (maskDC, 0, 0, Width, Height, SourceDC, 0, 0, #SrcCopy)
  SetBkColor_(SourceDC, TransparentColor)
  BitBlt_ (tempDC, 0, 0, Width, Height, maskDC, 0, 0, #SrcCopy)
  BitBlt_ (DC, x, y, Width, Height, tempDC, 0, 0, #MergePaint)
  BitBlt_ (maskDC, 0, 0, Width, Height, maskDC, 0, 0, #NotSrcCopy)
  BitBlt_ (tempDC, 0, 0, Width, Height, SourceDC, 0, 0, #SrcCopy)
  BitBlt_ (tempDC, 0, 0, Width, Height, maskDC, 0, 0, #MergePaint)
  BitBlt_ (DC, x, y, Width, Height, tempDC, 0, 0, #SrcAnd)
  DeleteObject_ (hMaskBmp)
  DeleteObject_ (hTempBmp)
  DeleteDC_ (maskDC)
  DeleteDC_ (tempDC)
  DeleteDC_ (SourceDC)
EndProcedure

UsePNGImageDecoder()
h=OpenWindow(0,0,0,500,500,#PB_Window_ScreenCentered|#PB_Window_SystemMenu,"EM Lite")
CreateGadgetList(h)
ii=ImageGadget(0,10,10,20,20,0)
i=LoadImage(0,"emfaces.png")
;get the color
StartDrawing(ImageOutput()):c=Point(1,1):StopDrawing()

Structure _rect ;API stucture for rectangles !
  left.l 
  top.l
  right.l 
  bottom.l 
EndStructure 
;Create a new rect and set the values
rect._rect
rect\left=0
rect\top=0
rect\right=200
rect\bottom=20
;create a new brush for the rectangle
br=CreateSolidBrush_(RGB(255,255,255))
;I am using api for the DC StartDrawing isnt working :(
DC=GetDC_(ii)
;create a new DC for handle the transparent image
tDC=CreateCompatibleDC_(DC)
btm=CreateCompatibleBitmap_(DC, ImageWidth(),ImageHeight())
SelectObject_(tDC,btm)
;draw a background color on the new DC
FillRect_(tDC,rect,br)
;Draw the sprite on new DC
DrawTransparentImage(tDC,UseImage(0),0,0,ImageWidth(),ImageHeight(),c)
;Clip the image from new DC to old DC , 40 is the start cliping left i used 
BitBlt_ (DC,0,0,20,20,tDC,40,0,#SrcCopy)
;delete the new DC and Bitmap and release the DC
DeleteObject_ (btm)
DeleteDC_ (tDC)
ReleaseDC_(ii,DC)
Repeat:Until WaitWindowEvent()=16
when i use DC=StartDrawing(ImageOutput()) it doesnt work so i used GetDc api

I have been thinking in other think...
I could first clip the image and then draw it transparent like in PB 2d drawing

*Edit
That would be hard untill i get it working with StartDrawing(ImageOutput()) or i have to learn how to draw images with API....
Forgive-me for my english, i'm portuguese!
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

Well I also got that working with GetDIBits, but that's slower than my old method.
bye,
Daniel
Post Reply