【发布时间】:2011-04-26 12:48:21
【问题描述】:
// 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;
这似乎完全解决了绘图问题!显然,在使用 Draw 或 StretchDraw 时,您必须锁定和解锁画布,因为在线程中,由于 graphics.pas 中的 GDI 对象缓存机制,有时会清除其 Bitmap.canvas 的 DC。
【问题讨论】:
-
您在询问线程安全,但我在您的问题中看不到并发线程。你为什么要关心线程安全?
-
你听说过 Jim Kueneman 的 VirtualShellTools library 吗?它在显示文件方面做得很好,就像资源管理器一样,它甚至可以处理缩略图视图。
-
罗布。是的,我多年来一直使用 Jim Kueneman 的 VirtualShellTools 库。它做得非常好,直到大约 Delphi 2009 或 2010。从那以后我无法安装它,据我所知,它已经有一段时间没有更新了。甚至 Jim 的用户组在一年多的时间里也几乎没有任何活动。我认为 Jim 现在正忙于其他事情...... Plasmatech 似乎也停止了 shell 开发,只剩下 JamShellBrower 作为唯一可行的 vcl shell ......顺便说一句非常好,有很好的支持以及最近更新。
标签: multithreading delphi thread-safety vcl