unit uDownloadPictureThread;

interface

uses

  Winapi.Windows,  Winapi.Messages,  System.SysUtils,  System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms,  Vcl.Dialogs,  Vcl.StdCtrls,
  Vcl.ComCtrls,  Vcl.ExtCtrls,  System.ImageList,  Vcl.ImgList,

  uVisualFrame,uDiminishPicSize,System.NetEncoding;

type
  TDownloadPictureThread = class(TThread)
  private
    { Private declarations }

    FVisualFrame_HTTP:TVisualFrame_HTTP;
    FDiminishPicSize:TDiminishPicSize;

    FDestBitMap:TBitMap;
    FBitmapDownLoaded:TBitMap;

    FLink:string;


    fEx : Exception;
    procedure QueryError;


    procedure DownloadPicture(RelativeLinkToPicture:String);


  protected
    procedure Execute; override;

  public

    property VisualFrame_HTTP:TVisualFrame_HTTP read FVisualFrame_HTTP
    write FVisualFrame_HTTP;

    property Link:String read FLink write FLink;

  end;

implementation

{
  Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TDownloadPictureThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end;

    or

    Synchronize(
      procedure
      begin
        Form1.Caption := 'Updated in thread via an anonymous method'
      end
      )
    );

  where an anonymous method is passed.

  Similarly, the developer can call the Queue method with similar parameters as
  above, instead passing another TThread class as the first parameter, putting
  the calling thread in a queue with the other thread.

}

{ TDownloadPictureThread }

procedure TDownloadPictureThread.DownloadPicture(RelativeLinkToPicture:String);

var Stream: TMemoryStream;
    twic:twicImage;

    EncodedRelativeLink:String;

begin

  Stream:=TMemoryStream.Create;
  twic:=twicImage.Create;

  try

    EncodedRelativeLink:=RelativeLinkToPicture.Substring(1); // removing first slash
    EncodedRelativeLink:=TNetEncoding.URL.Encode(EncodedRelativeLink);


     EncodedRelativeLink:=

     VisualFrame_HTTP.HTTPServerToRequest +'/'+ EncodedRelativeLink;

      with FVisualFrame_HTTP do begin

       IdHTTPDownloadPicture.Get(EncodedRelativeLink,Stream);
       Stream.Position:=0;

       twic.LoadFromStream(Stream);
       FBitmapDownLoaded.Assign(twic);


      end;




  finally


  FreeAndNil(Stream);
  FreeAndNil(Twic);

  end;


end;



procedure TDownloadPictureThread.Execute;

var imageDest:timage;

begin

  { Place thread code here }
  //


FVisualFrame_HTTP.CSDownloadPicture.Enter;

  FDestBitMap:=TBitmap.Create;
  FDiminishPicSize:=TDiminishPicSize.Create(nil);
  FBitmapDownLoaded:=TBitMap.Create;
  imageDest:=timage.Create(nil);

  try


  try


        if FVisualFrame_HTTP.FileType.IsFilePicture(Link) then begin

                 DownloadPicture(Link);

//                 FDiminishPicSize.
//                 ProportionalDiminishPicSize2(FBitmapDownLoaded,128,128,imageDest);

                      Synchronize(
                      procedure
                      begin


                           with FVisualFrame_HTTP do begin

                            //Assigning to image on Visual frame

//                            image.Proportional:=true;
//                            image.Stretch:=true;


                              image.Picture.Bitmap:=nil;
                              image.Picture.Bitmap.Assign(FBitmapDownLoaded);

                           if (image.Picture.Bitmap.Width<100) or (image.Picture.Bitmap.Height<100) then
                           image.Stretch:=False else image.Stretch:=true;



                              image.Hint:='Исходный размер '
                              +FBitmapDownLoaded.Width.ToString+' x '+FBitmapDownLoaded.Height.ToString;

                              image.ShowHint:=True;




                           end;



                      end
                      )



        end else

                   // Is Video ?
        if FVisualFrame_HTTP.FileType.IsFileVideo(Link) then

                         Synchronize(
                      procedure
                      begin


                           with FVisualFrame_HTTP do begin

                            image.Picture.Bitmap:=nil;
                            //Assigning to image on Visual frame
                            ImageList.GetBitmap(1,image.Picture.Bitmap);

                           end;



                      end
                      ) else

                      // Is Audio
              if FVisualFrame_HTTP.FileType.IsFileAudio(Link) then

                         Synchronize(
                      procedure
                      begin


                           with FVisualFrame_HTTP do begin

                            image.Picture.Bitmap:=nil;
                            //Assigning to image on Visual frame
                            ImageList.GetBitmap(2,image.Picture.Bitmap);

                           end;



                      end
                      ) else



        //Set Default Picture

                       Synchronize(
                      procedure
                      begin


                           with FVisualFrame_HTTP do begin

                            image.Picture.Bitmap:=nil;
                            //Assigning to image on Visual frame
                            ImageList.GetBitmap(0,image.Picture.Bitmap);

                           end;



                      end
                      );

     except

        VisualFrame_HTTP.ImageList.
        GetBitmap(0,VisualFrame_HTTP.Image.Picture.Bitmap);

        fEx := ExceptObject as Exception;
        Synchronize( QueryError );


     end;

  finally
  FDestBitMap.Free;
  FDiminishPicSize.Free;
  FBitmapDownLoaded.Free;
  imageDest.Free;

  end;


FVisualFrame_HTTP.CSDownloadPicture.Leave;

end;





procedure TDownloadPictureThread.QueryError;
begin

Application.OnException( Self, fEx );

end;

end.
