Pelles C forum

C language => Windows questions => Topic started by: John Z on May 23, 2020, 05:44:59 pm

Title: GDI+ Question
Post by: John Z on May 23, 2020, 05:44:59 pm
Switching a program from OLELoadPicture to GDIPlus using Frankie's excellent (Thanks very much!) fGdiPlusFlat.h under PellesC 9.
This means I'm also trying to learn GDI+ on the go.
It is all working and I am able to display the images using GdipLoadImageFromFile then creating a compatible HDC then using GdipDrawImageRectI to draw the image to an image control.

In order to speed up redrawing the screen on resize events I retain the *gpimage created from GdipLoadImageFromFile so WM_PAINT just needs to do GdipDrawImageRectI each WM_PAINT but does not need to do GdipLoadImageFromFile each time.  This works quite well.  When the image is to be changed I first use GdipDisposeImage, then start over with the next image. All works well and very similar to the OLE stream methods.

Now my question:  Why does GdipLoadImageFromFile lock the file until GdipDisposeImage is used? The image has been read into the pointer and GdipDrawImageRectI can draw the image at any time using the pointer, to a control.  GdipDisposeImage unlocks the file but also releases the data stored at the pointer created from GdipLoadImageFromFile.  Is there a method to just unlock the file after GdipLoadImageFromFile completes? Just a curiosity mainly, but it could be convenient to release the file.

Title: Re: GDI+ Question
Post by: frankie on May 23, 2020, 06:59:21 pm
Not really clear the reason for this behavior, although you can found that a lot of people is asking the same question about the issue if you google around.
IMHO the reason is that the GDI+ library use file mapping to access the bitmap data and the image metadata. Mapping the file expose its contents as a standard memory area, but locks the file also as side effect.
If you load the image using a stream it allows you to close the file, but doing this will expose spuriously generated errors of type: "A generic error occurred in GDI+".
The only solution, and the also the only correct one, is to make an image copy from the loaded one and dispose the original image.
Title: Re: GDI+ Question
Post by: John Z on May 23, 2020, 08:36:28 pm
Thanks Frankie.  Also, again thanks for all the work you did to make GDI+ easier to use under C.

Title: Re: GDI+ Question
Post by: frankie on May 23, 2020, 11:04:25 pm