I re-write here an issue I posted into the General Help section, maybe here it is a better topic.
While opening an image (with GETFILE) and saving it on the disk (with BT_BitmapSaveFile),
and if the BT_BitmapSaveFile (hBitmap, cFileName, BT_FILEFORMAT_BMP) option is used, the RAM is correctly released.
On the contrary, if the BT_BitmapSaveFile (hBitmap, cFileName, BT_FILEFORMAT_JPG) option is used the RAM is NOT released and heavily charged. When doing repeated savings of images, specially with large dimensions, this causes a crash of the executable (RAM is completely filled).
If the nTypePicture (BT_FILEFORMAT_JPG or BT_FILEFORMAT_BMP) is omitted, the image could be saved with the .jpg extension, but it maintains a BMP format, and Photoshop won't open it.
So I can't find the way to solve this issue: is it due to a lack of mine knowledge or is it a bug (JPG uncorrectly managed regarding RAM reease)?
I attach here a sample .prg (see lines 143-151) to better explain the issue.
You can easily check the RAM behavior compiling the executable and trying to save an image as a JPG or as a BMP while conrtemporarily looking at the Task Manager.
Reading the topic at viewtopic.php?f=20&t=4882&start=10
I changed the original bt_SaveGDIPlusPicture function in C:\hmg.3.4.3\SOURCE\c_BosTaurus.c
(from line 684) adding: GlobalFree (hGlobalAlloc);
but it still doesn't work...
it works adding RELEASE MEMORY after each BT_BitmapSaveFile (hBitmap, cFileName, BT_FILEFORMAT_JPG) or at the end of the procedure, but I'm not sure it's the right solution (what is released? also variables?)...
Is there any better solution?
Many thanks for any help!
Code: Select all
#include "hmg.ch"
#include "SET_COMPILE_HMG_UNICODE.CH"
#include "BosTaurus.CH"
*******************************************************************************
* HMG - Harbour Win32 GUI library Demo
* Copyright 2002 Roberto Lopez <mail.box.hmg@gmail.com>
* http://sites.google.com/site/hmgweb/
*******************************************************************************
* Bos Taurus Graphic Library for HMG
* AUTOR: Dr. CLAUDIO SOTO
* PAIS: URUGUAY
* E-MAIL: srvet@adinet.com.uy
* BLOG: http://srvet.blogspot.com
*******************************************************************************
* test_pix by aarca 2017
* fatteio@yahoo.it
*******************************************************************************
*-----------------------------------------------------------------------------*
* PROCEDURE test_pix
*-----------------------------------------------------------------------------*
hBitmap := 0
pix_choose := 0
hBitmap_copy := 0
hBitmap_clone := 0
savebmp := .F.
savejpg := .F.
m_filename := ""
m_appdir = GetStartUpFolder()
SetCurrentFolder(m_appdir)
DEFINE WINDOW manage_pix_test ;
AT 0, 0 ;
WIDTH 770 ;
HEIGHT 680 ;
SIZABLE .T. ;
BACKCOLOR { 241, 255, 236} ;
TITLE "Test managing pictures" ;
NOMAXIMIZE ;
ON INIT pix_test_ini() ;
ON RELEASE BT_bitmaprelease(hBitmap) ;
ON PAINT pix_test_paint() ;
MAIN
* ON MOUSECLICK manage_pix_test.release ;
DEFINE STATUSBAR
STATUSITEM "Test managing pictures"
END STATUSBAR
END WINDOW
If !IsControlDefined (openimage , manage_pix_test )
@ 20,30 BUTTON openimage ;
PARENT manage_pix_test ;
CAPTION "OPEN an image" ;
WIDTH 140 ;
HEIGHT 28 ;
ACTION pix_test_draw_image() ;
TOOLTIP 'Select an image (.JPG only) to be used as a background'
ENDIF
If !IsControlDefined (savebmp , manage_pix_test )
@ 20,180 BUTTON savebmp ;
PARENT manage_pix_test ;
CAPTION "save BMP image" ;
WIDTH 145 ;
HEIGHT 28 ;
ACTION ( savebmp := .T. , pix_test_save_image() ) ;
TOOLTIP "Save the selected image as the default name 'test.bmp' - RAM is released"
ENDIF
If !IsControlDefined (savejpg , manage_pix_test )
@ 20,335 BUTTON savejpg ;
PARENT manage_pix_test ;
CAPTION "save JPG image" ;
WIDTH 145 ;
HEIGHT 28 ;
ACTION ( savejpg := .T. , pix_test_save_image() ) ;
TOOLTIP "Save the selected image as the default name 'test.jpg' - RAM is NOT released"
ENDIF
CENTER WINDOW manage_pix_test
ACTIVATE WINDOW manage_pix_test
RETURN
*-----------------------------------------------------------------------------*
PROCEDURE pix_test_ini()
*-----------------------------------------------------------------------------*
pix_choose := GetFile( { {'.JPG only','*.jpg'} } , 'Open .JPG image file', m_appdir , .F. , .T. )
hBitmap := BT_BitmapLoadFile (pix_choose)
BT_ClientAreaInvalidateAll ("manage_pix_test")
SetCurrentFolder(m_appdir)
RETURN
*-----------------------------------------------------------------------------*
PROCEDURE pix_test_paint()
*-----------------------------------------------------------------------------*
LOCAL hDC, BTstruct
hDC := BT_CreateDC ("manage_pix_test", BT_HDC_INVALIDCLIENTAREA, @BTstruct)
BT_DrawBitmap (hDC, 70, 30, 770, 520, BT_SCALE, hBitmap)
BT_DeleteDC (BTstruct)
* BT_bitmaprelease(hBitmap)
RETURN
*-----------------------------------------------------------------------------*
PROCEDURE pix_test_draw_image()
*-----------------------------------------------------------------------------*
BT_bitmaprelease(hBitmap)
manage_pix_test.savebmp.show
manage_pix_test.savejpg.show
If IsControlDefined (background1 , manage_pix_test )
manage_pix_test.background1.release
ENDIF
pix_choose := GetFile( { {'.JPG only','*.jpg'} } , 'Open .JPG image file', m_appdir , .F. , .T. )
hBitmap := BT_BitmapLoadFile (pix_choose)
ERASE WINDOW manage_pix_test
* BT_bitmaprelease(hBitmap)
RETURN
*-----------------------------------------------------------------------------*
PROCEDURE pix_test_save_image()
*-----------------------------------------------------------------------------*
SetCurrentFolder(m_appdir)
hBitmap_copy := BT_BitmapCopyAndResize (hBitmap, BT_BitmapWidth (hBitmap), BT_BitmapHeight (hBitmap), BT_SCALE, BT_RESIZE_HALFTONE)
hBitmap_clone := BT_BitmapClone (hBitmap, 0, 0, BT_BitmapWidth (hBitmap), BT_BitmapHeight (hBitmap), BT_SCALE)
IF savebmp
BT_BitmapSaveFile (hBitmap_copy, "test.bmp", BT_FILEFORMAT_BMP)
** with BT_FILEFORMAT_BMP RAM is NOT charged
manage_pix_test.savebmp.hide
manage_pix_test.savejpg.hide
ENDIF
IF savejpg
BT_BitmapSaveFile (hBitmap_copy, "test.jpg", BT_FILEFORMAT_JPG)
** with BT_FILEFORMAT_JPG RAM is HEAVILY charged
manage_pix_test.savebmp.hide
manage_pix_test.savejpg.hide
ENDIF
BT_bitmaprelease(hBitmap)
BT_bitmaprelease(hBitmap_copy)
BT_bitmaprelease(hBitmap_clone)
If IsControlDefined (background1 , manage_pix_test )
manage_pix_test.background1.release
ENDIF
ERASE WINDOW manage_pix_test
If IsControlDefined (background1 , manage_pix_test )
manage_pix_test.background1.release
ELSE
IF savebmp
m_filename := "test.bmp"
ELSE
m_filename := "test.jpg"
ENDIF
@ 70,30 LABEL background1 ;
PARENT manage_pix_test ;
VALUE m_filename+" SAVED. Click to 'OPEN an image' to repeat" ;
WIDTH 500 ;
HEIGHT 40 ;
FONT 'Verdana' SIZE 14
manage_pix_test.background1.BackColor := { 241, 255, 236 }
manage_pix_test.background1.FontColor := { 0, 0, 255 }
ENDIF
savebmp := .F.
savejpg := .F.
RETURN