- Is this code thread safe
- // experimental code
- procedure TFormMain.MyThumbnailProvider( const Path: Unicodestring; Width,
- Height: Integer; out Bitmap: TBitmap );
- var
- AExtension: string;
- ARect: TRect;
- begin
- AExtension := LowerCase( ExtractFileExt( Path ) );
- if AExtension = '.wmf' then
- begin
- ARect.Left := 0;
- ARect.Top := 0;
- ARect.Right := Width;
- ARect.Bottom := Height;
- Image1.Picture.LoadFromFile( Path ); // added at design time to form
- Bitmap := TBitmap.Create;
- Bitmap.Width := Width;
- Bitmap.Height := Height;
- Bitmap.Canvas.StretchDraw( ARect, Image1.Picture.Graphic );
- end;
- end;
- procedure TFormMain.MyThumbnailProvider( const Path: Unicodestring; Width, Height: Integer; out Bitmap: TBitmap );
- var
- ARect: TRect;
- APicture: TPicture;
- AExtension: string;
- begin
- // experimental code
- if FileExists( Path ) then
- begin
- AExtension := LowerCase( ExtractFileExt( Path ) );
- if AExtension = '.wmf' then
- begin
- ARect.Left := 0;
- ARect.Top := 0;
- ARect.Right := Width;
- ARect.Bottom := Height;
- APicture := TPicture.Create;
- try
- APicture.LoadFromFile( Path );
- Bitmap := TBitmap.Create;
- Bitmap.SetSize( Width, Height );
- Bitmap.IgnorePalette := True;
- Bitmap.PixelFormat := pf24bit;
- Bitmap.Transparent := False;
- Bitmap.Canvas.Lock; **// New**
- try
- Bitmap.Canvas.StretchDraw( ARect, APicture.Graphic );
- finally
- Bitmap.Canvas.Unlock; **// New!**
- end;
- finally
- APicture.Free;
- end;
- end;
- end;
- end;
- Image1.Picture.LoadFromFile( Path );
- /// [...]
- Bitmap.Canvas.StretchDraw( ARect, Image1.Picture.Graphic );
- procedure TFormMain.MyThumbnailProvider( const Path: Unicodestring;
- Width, Height: Integer; out Bitmap: TBitmap );
- var
- LThumnailData: TThumbnailData; //Assuming an appropriately defined record
- begin
- LThumbnailData.FPath := Path;
- LThumbnailData.FWidth := Width;
- LThumbnailData.FHeight := Height;
- LThumbnailData.FBitmap := nil;
- SendMessage(Self.Handle, <Your Message Const>, 0, Longint(@LThumbnailData));
- Bitmap := LThumbnailData.FBitmap;
- end;
- const
- //Each distinct message must have its own unique ref number.
- //It's recommended to start at WM_APP for custom numbers.
- MSG_THUMBNAILINFO = WM_APP + 0;
- type
- PThumbnailData = ^TThumbnailData;
- TThumbnailData = record
- FPath: Unicodestring;
- FWidth, FHeight: Integer;
- FBitmap: TBitmap;
- end;
- procedure MSGThumbnailInfo(var Message: TMessage); message MSG_THUMBNAILINFO;
- procedure TForm3.MSGThumbnailInfo(var Message: TMessage);
- var
- LThumbnailData: PThumbnailData;
- begin
- LThumbnailData := Pointer(Message.LParam);
- //The rest of your code goes here.
- //Don't forget to set LThumbnailData^.FBitmap before done.
- Message.Result := 0;
- inherited;
- end;