B4J Question better snapshot quality ?

le_toubib

Active Member
Licensed User
Longtime User
hi all .. i m using this code to take a snapshot of a form and then print it :
B4X:
Sub Button1_Action
   Dim out As OutputStream = File.OpenOutput(File.DirApp, "image.png", False)
   MainForm.RootPane.Snapshot.WriteToStream(out)
   out.Close
   Dim params As List
   params.Initialize
   'params.Add("printername=novapdf")
   params.Add("file=template.html")
   sh.Initialize("sh", "printhtml.exe", params)
   sh.WorkingDirectory = File.DirApp
   sh.Run(-1)
End Sub

is there a way to improve the resulted image quality ? it s toooo hazy


also is there a way to set the image size to fit A4 page size ??? should this be done on the form width/hight level ?? i.e before taking the snapshot ?
 
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
The snapshot code by itself creates a high quality image. I just tested it.
Where is the problem? In the saved image or when you print it?
It's hazy even before print ...
It s very readable ... but not sharp enough as compared to manually setting html text / controls
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
What is the size of the saved image?

What if you set your MainForm.WindowWidth = 4000 and MainForm.WindowHeight = 4000? Do you get a 4000x4000 px image?
If not, you need to do 2 subs:
B4X:
Sub SnapShot
    MainForm.WindowWidth = 4000
    MainForm.WindowHeight = 4000
    CallSubDelayed(Me, "SaveImage")
End sub

Sub SaveImage
    Dim out AsOutputStream = File.OpenOutput(File.DirApp, "image.png", False)
    MainForm.RootPane.Snapshot.WriteToStream(out)
    out.Close
End Sub
That will leave some time for the MainForm to scale up before taking the snapshot
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
are there specific formwidth and formheight that would b better fitting for an A4 page ? i know the proportions are 1:sqrt(2) (from numberphile :) )
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
A4 is 21 cm x 29.7 cm, but what's important is the DPI. You can see here:
https://forums.indigorose.com/forum...oper-s-den/13282-what-is-the-size-of-a4-in-px
A4 is a document format, as a screen image that's going to depend on the image resolution, for example an A4 document resized to:
72 dpi (web) = 595 X 842 pixels.
300 dpi (print) = 2480 X 3508 pixels (This is "A4" as I know it, i.e. "210mm X 297mm@ 300 dpi")
600 dpi (print) = 4960 X 7016 pixels.Oct 12, 2005

300 dpi is good enough for print, so resize your form to 2480x3508 px
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
B4X:
Sub SnapShot

    MainForm.WindowWidth = 1500
    MainForm.WindowHeight = 2121
   
        For Each n As Node In MainForm.RootPane.GetAllViewsRecursive
        Try
        n.Left=n.Left * 1.6
        n.PrefWidth=n.PrefWidth * 1.6
        n.top=n.top * 1.6
        n.Prefheight=n.Prefheight * 1.6       
'        If n Is Label or n is Then
            n.Style="-fx-font-size: 18; -fx-font-weight: bold"
'        End If
        Catch
        End Try       
    Next
   
    CallSubDelayed(Me, "SaveImage")
End Sub

that gave me much better results :)

i might have to rescale back to orginial sizes after the snapshot
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
try this, that could be easier:
B4X:
Sub SnapShot
    Dim jo As JavaObject = MainForm.RootPane
    jo.runMethod("setScaleX", Array(2.0))
    jo.runMethod("setScaleY", Array(2.0))
    CallSubDelayed(Me, "SaveImage")
End Sub
This code will "zoom" nodes (including fonts)

or something like this:
B4X:
For Each n As Node In MainForm.RootPane.GetAllViewsRecursive
    Dim jo As JavaObject = n
    jo.runMethod("setScaleX", Array(1.6))
    jo.runMethod("setScaleY", Array(1.6))
Next
 
Upvote 0
Top