NO

Author Topic: GDI+ Question  (Read 2789 times)

Offline John Z

  • Member
  • *
  • Posts: 860
GDI+ Question
« 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.

Thanks

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: GDI+ Question
« Reply #1 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.
« Last Edit: May 23, 2020, 07:03:14 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline John Z

  • Member
  • *
  • Posts: 860
Re: GDI+ Question
« Reply #2 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.

John

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: GDI+ Question
« Reply #3 on: May 23, 2020, 11:04:25 PM »
 :)
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide