B4J Question Text graphic

David Riley

Member
Licensed User
Longtime User
I want to write text and save it as .png.

This seems to work with B4A. Although there's a warning about the InitializeMutable units.

B4X:
'create image
    Dim Bitmap1 As Bitmap
    Bitmap1.InitializeMutable(264, 176)
    Dim Canvas1 As Canvas
    Canvas1.Initialize2(Bitmap1)
    Canvas1.DrawText("Hello", 65dip, 50dip, Typeface.DEFAULT_BOLD, 15, Colors.White, "CENTER")
'save image to file
    Dim Out As OutputStream
    Out = File.OpenOutput(File.DirRootExternal, "Hello.png",  False)
    Bitmap1.WriteToStream(Out, 100, "PNG")
    Out.Close




I cannot get it to work with B4J. (Warning Bitmap1 not initialized.)

B4X:
'create image
    Dim Bitmap1 As Image
    Dim Canvas1 As Canvas
    Canvas1.Width = 264
    Canvas1.Height = 176
    Canvas1.Initialize("Bitmap1")
    Canvas1.DrawText("Hello", 65, 50, fx.DefaultFont(15), fx.Colors.White, "CENTER")
'save image to file
    Dim Out As OutputStream
    Out = File.OpenOutput("D:\", "Hello.png",  False)
    Bitmap1.WriteToStream(Out)
    Out.Close

Please tell me where I've gone wrong.
 
Last edited:

David Riley

Member
Licensed User
Longtime User
Thanks Erel. Sorry I didn't post the code properly.

This is doing what I want now.

B4X:
Sub Process_Globals
    Private fx As JFX
End Sub

Sub AppStart (Form1 As Form, Args() As String)
'create image
    Dim Canvas1 As Canvas
    Canvas1.Initialize("Canvas1")
    Canvas1.Width = 264
    Canvas1.Height = 176
    Canvas1.DrawText("Hello",132,88,fx.DefaultFont(15),fx.Colors.Black,"CENTER")
'save image to file
    Dim Out As OutputStream
    Out = File.OpenOutput("D:\","Hello.png",False)
    Canvas1.Snapshot.WriteToStream(Out)
    Out.Close
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…