#2 GDI+ » Some methods of SynGdiPlus are not thread-safe » 2018-09-06 05:14:42

BugsDigger
Replies: 1

Hi, Synopse and Arnaud!
Many thanks for your highly useful GDI unit!

During my development I've found that using SynGdiPlus (I use only LoadFromFile/ToBitmap/SaveToFile to work with tif files) in background thread sometimes leads to "black squares" instead of normal image in bitmaps.
Usually it is symptom of multithreading problems with Canvas.

SynGdiPlus uses Canvas.Draw without Lock/Unlock.
I've added the following changes:

begin
  if not fHasContent then
    ...
  else begin
    ...
    result.Canvas.Lock;
    try
       result.Canvas.Draw(0,0,self);
    finally
       result.Canvas.Unlock;
    end;
  end;
end;

procedure SaveAs(...)
...
        Bmp := TBitmap.Create;
        try
          ...
          Bmp.Canvas.Lock;
          try
             Pic.Draw(Bmp.Canvas,R);
          finally
             Bmp.Canvas.Unlock;
          end;
          SynGdiPlus.SaveAs(Bmp,Stream,Format,CompressionQuality,0,BitmapSetResolution);
        finally
          Bmp.Free;
        end;

After that I see no problems in intensive tests.

(Also for all who want to use SynGdiPlus in background thread: don't forget to use CoInitializeEx/CoUninitialize.)

Board footer

Powered by FluxBB