Q&A

  • 웹이미지 View 소스첨부
unit 2개가 있습니다. 하나는 unit1 이구요, 다른건 webimage 유닛입니다.
unit1에서 버튼을 누르면 웹서버에서 제공하는 이미지를 폼이나,Timage상에서 뿌리고 싶은데 생각처럼 나오질 않습니다.  webimage 유닛에서 LoadFromURL에 경로를 주고 컴파일은 이상없이 되는데 안나오네요? 뭐가 잘못인지 자세히 설명 부탁드립니다.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses WebImage;
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var WebImage1 : TWebImage;
begin
    WebImage1 := TWebImage.Create(image1);
    webimage1.LoadFromURL('http://192.168.0.70/WonbuFile/car_54969.jpg'); <--- 이미지 가져오는것
end;

end.

//------------- webimage unit
unit WebImage;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Jpeg;
type
  TWebImageThread = class;
  TWebImage = class(TImage)
  private
        { Private declarations }
  protected
        { Protected declarations }
        FWebImageThread: TWebImageThread;
  public
        { Public declarations }
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
    procedure LoadFromURL(URL: String);
  published
        { Published declarations }
  end;
  TWebImageThread = class(TThread)
  protected
        FFileExt: String;
        procedure View;
  public
        WebImage: TWebImage;
        URL: String;
        Stream: TStream;
        procedure Execute; override;
  end;

procedure Register;
function   GetHttpFile(StrUrl:String; Stream:TStream):Boolean;

implementation

uses WinINet;

procedure Register;
begin
  RegisterComponents('Misc', [TWebImage]);
end;

function GetHttpFile(StrUrl:String; Stream:TStream):Boolean;
var
  ISession, HttpFile: HINTERNET;
  UrlBuff: String;
  ByteRead, BuffSize, FileSize, Reserved: DWord;
  bSizeIsKnown: boolean;
  ms: TMemoryStream;
  FileSizeBuff: Pointer;
  Buffer: Pointer;
begin
  Stream.Position := 0;
  Result := false;

  ISession := InternetOpen(nil, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  HttpFile := InternetOpenUrl(ISession, PChar(StrUrl), nil, 0, INTERNET_FLAG_DONT_CACHE, 0);
  SetLength(UrlBuff, 1024);
  BuffSize := 1024;
  Reserved := 0;
  if HttpQueryInfo(HttpFile, HTTP_QUERY_CONTENT_LOCATION, PChar(UrlBuff), BuffSize, Reserved) = false then
  begin
         case(GetLastError()) of
                ERROR_INSUFFICIENT_BUFFER:
                begin
                   SetLength(UrlBuff, BuffSize+1);
                   HttpQueryInfo(HttpFile, HTTP_QUERY_CONTENT_LOCATION, PChar(UrlBuff), BuffSize, Reserved);
                end;
                ERROR_HTTP_HEADER_NOT_FOUND:
                   UrlBuff := StrUrl;
                else
                   InternetCloseHandle(HttpFile);
                   exit;
         end;
  end;
  GetMem(FileSizeBuff, 4);
  BuffSize := sizeof(FileSizeBuff);
  Reserved := 0;
  bSizeIsKnown := HttpQueryInfo(HttpFile, HTTP_QUERY_CONTENT_LENGTH, FileSizeBuff, BuffSize, Reserved);
  if bSizeIsKnown then
  begin
         FileSize := StrToInt(PChar(FileSizeBuff));
         if Stream is TMemoryStream then
                ms := Stream as TMemoryStream
         else
                ms := TMemoryStream.Create;
         ms.Size := FileSize;
         Result := InternetReadFile(HttpFile, ms.Memory, FileSize, ByteRead);
         if Result then Stream.CopyFrom(ms, 0);
         if ms <> Stream then ms.Free;
  end
  else
  begin
         GetMem(Buffer, 2048);
         FileSize := 2048;
         while true do
         begin
                Result := InternetReadFile(HttpFile, Buffer, FileSize, ByteRead);
                if ByteRead = 0 then break;
                Stream.Write(Buffer^, ByteRead);
         end;
         FreeMem(Buffer);
  end;
  FreeMem(FileSizeBuff);
  InternetCloseHandle(HttpFile);
  InternetCloseHandle(ISession);
end;

constructor TWebImage.Create(AOwner: TComponent);
begin
        inherited;
        FWebImageThread := nil;
end;

destructor TWebImage.Destroy;
begin
        if not (csDesigning in ComponentState) then
        begin
                if FWebImageThread <> nil then
                begin
                        FWebImageThread.Terminate;
                        FWebImageThread.WaitFor;
                        FWebImageThread := nil;
                end;
        end;
        inherited;
end;

procedure TWebImage.LoadFromURL(URL: String);
begin
        if FWebImageThread = nil then
        begin
                FWebImageThread := TWebImageThread.Create(true);
                FWebImageThread.FFileExt := UpperCase(ExtractFileExt(URL));
                FWebImageThread.WebImage := Self;
                FWebImageThread.URL := URL;
                FWebImageThread.Resume;
        end;
end;

procedure TWebImageThread.View;
var
        Jpeg: TJpegImage;
begin
        Stream.Seek(0, soFromBeginning);
        if (FFileExt = '.JPG') or (FFileExt = '.JPEG') then
        begin
                Jpeg := TJpegImage.Create;
                try
                        Jpeg.LoadFromStream(Stream);
                        WebImage.Picture.Assign(Jpeg);
                except
                end;
                Jpeg.Free;
        end;
        WebImage.Repaint;
end;

procedure TWebImageThread.Execute;
var
        Succeed: Boolean;
begin
        Stream := TMemoryStream.Create;
        Succeed := GetHttpFile(URL, Stream);
        if Succeed then
                Synchronize(View);
        Stream.Free;
        WebImage.FWebImageThread := nil;
end;

end.
1  COMMENTS
  • Profile
    nilriri™ 2003.06.24 00:54
    샬롬~

    아래쪽 웹이미지 소스를 보니 컴포넌트던데..

    해당 pas파일을 install component를 이용해서 등록하시면..

    misc탭에 컨트롤로 등록이 됩니다.

    그러면 새로운 폼에 해당 컨트롤을 배치하시고..

    버튼 클릭에서는..단순히 LoadFromURL 만 사용하면

    그림 잘 나오던데요..

    즐프~



    unit Unit1;

    interface

    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ExtCtrls;

    type
      TForm1 = class(TForm)
        Button1: TButton;
        Image1: TImage;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation
    uses WebImage; <-- 여기에 쓰는게 아니고.. 위쪽 uses절에 써 주세요..
    {$R *.DFM}

    procedure TForm1.Button1Click(Sender: TObject);
    var WebImage1 : TWebImage; <-- 이건 선언하실 필요가 없어 보입니다..
    begin
        WebImage1 := TWebImage.Create(image1); <-- 물론 여기도 필요가 없구요
        webimage1.LoadFromURL('http://192.168.0.70/WonbuFile/car_54969.jpg'); <--- 이미지 가져오는것
    end;

    end.