PNG size

Loading a PNG size: PNG document - 3,1 MB
then saving as PNG results in an increment in its size: PNG image - 8,3 MB
Near 3 times the original size, is there something to avoid this?

The Finder show the original as “PNG document” and the duplicate as “PNG image”

Full test code:

[code]Sub Action() Handles Action

var f As FolderItem
var d As OpenDialog

d = New OpenDialog
f = d.ShowModal()
If f <> Nil Then
myPic = Picture.Open(f) // myPic is global
End If

if myPic <> nil then
var dlg as SaveAsDialog
dlg = new SaveAsDialog
dlg.Title = “Save PNG File”
dlg.SuggestedFileName = “duplicate.png”
dlg.Filter = FileTypeGroup1.Png
f = dlg.ShowModal
if f <> nil then
myPic.Save(f,Picture.SaveAsPNG)
end if
end if
End Sub
[/code]

You can try PNGWriterMBS class in MBS Xojo Images Plugin as it offers various settings like filter, interlace and compression.

Or PNGOptimizerMBS to optimize PNGs and shrink them. e.g. if it detects that alpha is not needed, it’ll store without alpha. Or makes a grayscale PNG.

MBS plugins are not free.

But certainly worth the money.

Otherwise you can use Shell class and some command line tool to do the same.

As a free option you could try ImageOptim after the PNG has been generated, it will greatly reduce PNG size without affecting quality. https://imageoptim.com

Thanks Tim but this do not match, the purpose is to avoid images growing when saved from my Xojo app.

Ah, then I would second Christian’s recommendation. I believe they offer a command line version of their image compressor, so that may be worth investigating. :slight_smile:

The problem may be that if you load a grayscale, palette or otherwise optimized PNG, it will be uncompressed in memory to a RGB picture with alpha.
When you save it, this blown up information will be preserved.

And we do have PNGWriterMBS class to write e.g. grayscale PNG.
Or check out the GMImageMBS class in MBS Xojo GraphicsMagick plugin, which can do a lot, too.

with a detour via binary file as text with .png suffix, the image is sometimes somewhat smaller

Var f As FolderItem
Var d As OpenDialog
Var mypic,neu As Picture
Var bildstring As String
d = New OpenDialog
f = d.ShowModal
If f <> Nil Then
myPic = Picture.Open(f) // myPic is global
bildstring= mypic.GetData(Picture.FormatPNG)
Messagebox "Picture lenght = "+bildstring.Length.tostring
End If

Var WriteFile As FolderItem = Folderitem.ShowsaveFileDialog(".png",“mein Dupli.png”)
If writeFile <> Nil Then
Var writeStream As BinaryStream = BinaryStream.Create(writeFile, True)
writeStream.Write bildstring
writestream.Close
End If

info
only with PNG - JPEG are about 50% larger

I expect (at least on the Mac) that Xojo is using Apple’s Image IO kit. Where the default is to create a 32-Bit per pixel image. It is possible with declares to create an 8-Bit image, but that requires some serious messing around with creating pixel tables and such, to which I’ve never done.

PNG compression tools are a dime a dozen, all with various techniques of doing this. My favorite is PNG Quant.

So the easiest solution is to do what @Tim Parnell suggests and export the image using standard Xojo code, then use a Shell class (or NSTask if you’re creating an application for the Mac App Store) and call one of the many PNG compression tools on the exported picture.