Android Question How to Draw on a Bitmap and Save

TML

New Member
Licensed User
Longtime User
Hello,
I am able to use Getpixel. I am looking for the opposite and cannot find a Setpixel. As I understand it, I have to make the bitmap mutable and use canvas. I am unable to find an example code for this. I would also like to be able to save the modified bitmap but one step at a time. I have attempted to research this on my own but have not been able to get it. Thanks.

So, can anyone direct me to an example of code that draws on a bitmap?
 

JordiCP

Expert
Licensed User
Longtime User
One possible way (with the proper file and dir names) could be

B4X:
Dim Dir_orig As String = File.DirAssets
Dim Filename_orig As String = "original_bitmap.jpg"
Dim Dir_dest As String = File.DirRootExternal
Dim Filename_dest As String = "modified_bitmap.jpg"

Dim btmp_orig As Bitmap
btmp_orig.Initialize( Dir_orig, Filename_orig )

Dim btmp_m As Bitmap
btmp_m.InitializeMutable( btmp_orig.Width, btmp_orig.Height )    ' 1:1 scale
  
Dim cv As Canvas
cv.Initialize2( btmp_m )
 
Dim Rect1 As Rect
Rect1.Initialize( 0, 0, btmp_m.Width, btmp_m.Height )   
cv.DrawBitmap( btmp_orig, Null, Rect1 )
  
' ...   
' Now draw whatever you want with the canvas
' ...
           
' Save
Dim o As OutputStream
o=File.OpenOutput( Dir_dest, Filename_dest,False)
cv.Bitmap.WriteToStream(o,100,"JPEG")    'or "PNG"   
o.Close
 
  • Like
Reactions: TML
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
oops! stevel05 was faster than me!o_O

(BTW still pending to clean my code and send you the modifications for your inputslder class. Net week will have time for it :))
 
Upvote 0

TML

New Member
Licensed User
Longtime User
Great, thanks for the quick responses. I haven't tried the saving part yet, but the drawing on the bitmap worked perfect. Thanks again for the help.
 
Upvote 0
Top