Thema: Delphi Bitmap in Thread laden

Einzelnen Beitrag anzeigen

Gruber_Hans_12345

Registriert seit: 14. Aug 2004
1.439 Beiträge
 
Delphi 2007 Professional
 
#9

Re: Bitmap in Thread laden

  Alt 17. Mär 2009, 09:55
Hmmm, ich weiss was synchronisation ist, und "meine" teile isnd ja durchaus synchron ... nur weiss ich nicht wie ichs mit der VCL machen soll.
Ich kann ja nicht absolut alles synchronisieren, dann bringt mir der thread ja nix?!?!?!?

Thread Code
Delphi-Quellcode:
{$DEFINE USE_THREADLOADING}
procedure TLoadImage.DoLoadImage(FileName : string);
var
    picRect : TRect;
    pWidth : integer;
    pHeight : integer;
    jp : TJPEGImage;
    pic : TPicture;
begin
    EnterCriticalSection(fCS);
    try
        if FileName = 'then exit;

        OutputDebugString(PChar('Loading image ' +FileName));
        fInLoad := TRUE;
        fLoadedImage := '';

        jp := TJPEGImage.Create;
        jp.LoadFromFile(FileName);
        pWidth := jp.Width;
        pHeight := jp.Height;

        if assigned(fBitmap) then fBitmap.Free;

        fBitmap := TBitmap.Create;
        fBitmap.Width := 1280;
        fBitmap.Height := 1024;
        fBitmap.PixelFormat := pf32bit;
        fBitmap.Canvas.Brush.Color := clBlack;
        Windows.FillRect(fBitmap.Canvas.Handle, Rect(0,0,fBitmap.Width, fBitmap.Height), fBitmap.Canvas.Brush.Handle);

        if (pWidth / pHeight) > (1280 / 1024) then begin
            picRect := Rect(0, (1024 - round((1280 / pWidth) * pHeight)) div 2, 1280, 0);
            picRect.Bottom := fBitmap.Height - picRect.Top;
        end
        else begin
            picRect := Rect(1280 - (round((1024 / pHeight) * pWidth)) div 2, 0, 0, 1024);
            picRect.Right := fBitmap.Width - picRect.Left;
        end;

        fBitmap.Canvas.StretchDraw(picRect, jp);

        fLoadedImage := FileName;
        fInLoad := FALSE;
        fFileName := '';

        OutputDebugString(PChar('Finish Loading image ' +fLoadedImage));
    finally
        jp.Free;
        LeaveCriticalSection(fCS);
    end;
end;
Code im main, der auf den Thread zugreift (der restliche code arbeitet mit dem Picshow Element)

Delphi-Quellcode:
procedure TMainForm.LoadNextImage;
begin
{$IFDEF USE_THREADLOADING}
        if not TryEnterCriticalSection(imgLoader.fCS) then begin
            OutputDebugString('Bild noch nicht vollständig geladen Recheck in 100ms');
            trCheckLoadImage.Interval := 100;
            trCheckLoadImage.Enabled := TRUE;
            exit;
        end;
        try
            if imgLoader.LoadedImage = 'then begin
                OutputDebugString('No image loaded Recheck in 1000ms');
                trCheckLoadImage.Interval := 1000;
                trCheckLoadImage.Enabled := TRUE;
                imgLoader.LoadImage(PicturesPath + '\' + GetNextImageName(LoadedImage));
                exit;
            end;

            LoadedImage := ExtractFileName(imgLoader.LoadedImage);
            OutputDebugString(PChar('Image loaded : Using image : '+LoadedImage));
{$ELSE}
            imgLoader.DoLoadImage(PicturesPath + '\' + GetNextImageName(LoadedImage));
{$ENDIF}
            Picshow.Picture.Bitmap.Width := imgLoader.Bitmap.Width;
            Picshow.Picture.Bitmap.Height := imgLoader.Bitmap.Height;
            Picshow.Picture.Bitmap.PixelFormat := imgLoader.Bitmap.PixelFormat;
            BitBlt(PicShow.Picture.Bitmap.Canvas.Handle, 0, 0, Picshow.Picture.Bitmap.Width, Picshow.Picture.Bitmap.Height
                    , imgLoader.Bitmap.Canvas.Handle, 0, 0, SRCCOPY);

{$IFDEF USE_THREADLOADING}
            imgLoader.LoadImage(PicturesPath + '\' + GetNextImageName(LoadedImage));
        finally
            LeaveCriticalSection(imgLoader.fCS);

            trCheckLoadImage.Interval := 2000;
            trCheckLoadImage.Enabled := TRUE;
        end;
{$ENDIF}
end;
Gruss Hans

2B or not 2B, that is FF
  Mit Zitat antworten Zitat