B4J Question Image dimension (on disk)

christiantric

Member
Licensed User
Hi all,
I load an image from disk via FileChooser in an ImageView in this way:

B4X:
Dim fc As FileChooser
    fc.Initialize
    fc.Title="Select the work image"
    fc.InitialDirectory = FileSystem.FindUserDocumentsFolder
    fc.SetExtensionFilter("Image", Array As String("*.jpg","*.png","*.bmp"))
    Dim fileChosen As String = fc.ShowOpen(ParentForm)
   
    If fileChosen.Length = 0 Then
        Return
    End If
   
    Dim filePath As String = File.GetFileParent(fileChosen)
    Dim fileName As String = File.GetName(fileChosen)
   
    Dim newImage As Image
    newImage.Initialize(filePath, fileName)
    imgWork.SetImage(newImage)

Later I save the image from ImageView to the disk in this way:

B4X:
Dim editImage As Image = imgWork.GetImage
If editImage.IsInitialized Then
    File.WriteBytes(filesPath, fileName, ImageUtils.ImageToBytes(editImage))
End If

Public Sub ImageToBytes(img As Image) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    img.WriteToStream(out)
    out.Close
    Return out.ToBytesArray
End Sub

The dimension of the original file is 1,45MB.
The dimension of the saved file is 10,6MB.

Why the file gets so big?

Christian
 

OliverA

Expert
Licensed User
Longtime User
According to the documentation (https://www.b4x.com/b4j/help/jfx.html#image_writetostream), Image's WriteToStream produces a PNG, not a JPG. Therefore, the image should be larger (due to lossless compression).
Edit: If the source image happens to be a JPG. If it was a PNG, than good question.
 
Upvote 0

christiantric

Member
Licensed User
Yes, you could be right because the source image is jpg.
If that's the case, how could I load and save image again keeping the same image quality and dimensions?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top