largo_winch
28-05-2012, 11:59
How I can make a picture (*.bmp, *jpg) transparent in canvas mode? It's possible with current thinbasic version to store two pictures over one place (with magenta trick?) ? bye, largo
ErosOlmi
29-05-2012, 06:43
Hi Largo,
no I'm sorry but at the moment there is no a native command able to do that job.
I will see what I can do in next thinBasic version.
Eros
Charles Pegge
29-05-2012, 07:03
I think this is a job for GDIplus. Will you support a GDIp canvas in thinBasic 2.0, Eros?
ErosOlmi
29-05-2012, 07:52
GdiPlus module sure. A pseudo basic version has always been there in thinbasic distribution.
A GdiPlus Canvas native control ... maybe
Petr Schreiber
29-05-2012, 08:13
If you don't need ultrafast solution, you can have a look at:
/SampleScripts/UI/Canvas/Image_Background/Image_Background.tBasic
...which shows approach with magenta masked images on canvas.
If you need solid speed for thousands of transparent, I would recommend to jump over GDI Plus straight to the realm of GPUs and try alpha masking / alpha blending.
In ThinBASIC, you can use pure OpenGL for this, or for better comfort TBGL. When you look at TBGL_LoadTexture command, you will see you can specify optional transparent color.
Other approach would be using Mike's sprite functions, have a look at examples under:
/SampleScripts/TBGL/Sprites/
Petr
ErosOlmi
29-05-2012, 10:12
Hi Petr,
thanks and sorry: I forgot about the "Quick and dirty approach" example you developed :D
Ciao
Eros
Lionheart008
29-05-2012, 12:15
hello largo, this could be a solution:
pixelColor = GDIP_ARGB(0, 255, 0, 255) '=> transparence modus !!!
' Empty GUI script created on 10-31-2011 + 11-03-2011, 15:32:15 by largo_winch (ThinAIR)
'--------------> GDIPLUS TEST FOR THINBASIC -------------------->
'-- modified by frank for largo about question of "transparence modus" for images, 29.mai.2012 :)
Uses "UI", "console"
'---------------------------->
#INCLUDE "GDIPLUS_LW1a.INC"
'---------------------------->
Begin ControlID
%Canvas_Gdip
%bClose
End ControlID
Type BGRA
B As Byte
G As Byte
R As Byte
A As Byte
End Type
' ========================================================================================
' ========================================================================================
' Main
' ========================================================================================
Function TBMain() As Long
Local hr As Long
Local hDlg As DWord
Local hdc As DWord
Local token As DWord
Local StartupInput As GdiplusStartupInput
' Initialize GDI+
StartupInput.GdiplusVersion = 1
hr = GdiplusStartup(token, StartupInput, ByVal %NULL)
If hr Then
MsgBox "Error initializing GDI+"
Exit Function
End If
Dialog New 0, "tb_gdiplus DrawImage",-1,-1, 420, 270, _
%WS_POPUP Or %WS_VISIBLE Or _
%WS_CLIPCHILDREN Or %WS_CAPTION Or _
%WS_SYSMENU Or %WS_MINIMIZEBOX, 0 To hDlg
Dialog Show Modal hDlg, Call dlgProc
' Shutdown GDI+
GdiplusShutdown token
End Function
' ========================================================================================
' ========================================================================================
' Main Dialog procedure
' ========================================================================================
CallBack Function DlgProc() As Long
Local cx, cy As Long, hDc As Long
' -- Test for messages
Select Case CBMSG
Case %WM_INITDIALOG
' -- Place controls here
Dialog Pixels CBHNDL, 400, 250 To Units cx, cy
Control Add Canvas, CBHNDL, %Canvas_Gdip, "", 5, 5, cx+100, cy+100
Control Add Button, CBHNDL, %bClose, "Close", 80+cx, cy+5-84, 50, 18, Call bCloseProc
Canvas_Attach CBHNDL, %Canvas_Gdip, %TRUE
Canvas_Color Rgb(128, 255, 0), Rgb(0, 0, 0)
'Canvas_Clear(Rgb(0,0,0))
Canvas_Scale Pixels
Case %WM_PAINT
GDIP_DrawImage()
Canvas_Redraw
Case %WM_COMMAND
Select Case CBCTL
Case %IDCANCEL
If CBCTLMSG = %BN_CLICKED Then Dialog End CBHNDL, 0
End Select
End Select
End Function
' -- Callback for close button
CallBack Function bCloseProc()
If CBMSG = %WM_COMMAND Then
If CBCTLMSG = %BN_CLICKED Then
' -- Closes the dialog
Dialog End CBHNDL
End If
End If
End Function
'--------------------> ~~~~~~~~~
Sub GDIP_DrawImage() ' SETPIXEL
'--------------------> ~~~~~~~~~
Local hStatus As Long
Local pGraphics As DWord
Local pImage As DWord
Local strFileName As String
Local nWidth As Long
Local nHeight As Long
Local rows As Long
Local cols As Long
Local pixelColor As Long
hStatus = GdipCreateFromHDC(Canvas_GetDC, pGraphics)
' // Create the Image object
strFileName = Ucode$("Metallica1a.jpg")
hStatus = GdipLoadImageFromFile(StrPtr(strFileName), pImage)
' // Draw the image
hStatus = GdipDrawImage(pGraphics, pImage, 10, 10)
' // Get the width and height of the bitmap
hStatus = GdipGetImageWidth(pImage, nWidth ) '(varptr(nWidth))
hStatus = GdipGetImageHeight(pImage, nHeight ) '(varptr(nHeigth))
'--------- // that's important! ------------ //
'pixelColor = GDIP_ARGB(255, 0, 255, 0) 'green pixeled
pixelColor = GDIP_ARGB(0, 255, 0, 255) '=> transparence modus !!!
'--------- // that's important! ------------ //
' // Create a checkered pattern with black pixels.
For rows = 1 To nWidth - 1 Step 2
For cols = 1 To nHeight Step 2
hStatus = GdipBitmapSetPixel(pImage, rows, cols, pixelColor)
Next
Next
' // Draw the altered bitmap.
hStatus = GdipDrawImage(pGraphics, pImage, 200, 200)
' // Cleanup
If pImage Then GdipDisposeImage(pImage)
If pGraphics Then GdipDeleteGraphics(pGraphics)
End Sub
you find solution in zip folder with your own gdip include file.
best regards, frank
largo_winch
30-05-2012, 14:44
thanks a lot for replies and good examples and infos! (petr, eros, charles, frank)
1) first thing:
/SampleScripts/UI/Canvas/Image_Background/Image_Background.tBasic
the example Image_Background causes error code 30 (something with "pointapi" isn't correct here (new thinbasic beta version 1.9.0.0)
@petr: at this example how I can add new color background/foreground to the image (ferrari) ?
2) many thanks @lionheart, that example with my gdip include files will make my day, it's nearly the approach I was looking for, great :) the color argb function is exactly what I need for my idea. I will try to build little klick-a-picture game for next weeks.
3) 2d tbgl features is quite new thing for me, I will test the module from michael next days, thanks for that important link too.
bye, largo
ErosOlmi
30-05-2012, 16:16
this is a problem I need to check
Positive is that also here in my current beta version I get same error.
I will let you know and publish as soon as possible an update
In thinBasic version 1.8.9.0 problem is not present