Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Общие вопросы > download image and show


Автор: muratboy31 4.3.2010, 14:43
i am trying to download an image file on the internet and show in in image1 object but it doesnt work!!! what am i doing wrong ?

Код

procedure TForm1.btnRefreshClick(Sender: TObject);
var
  ADPLocalFile : TFileName;
  StartItemNode, StartItemNode2 : IXMLNode;
  ANode, ANode2 : IXMLNode;
  STitle, sDesc, sLink, ADPXMLBLOG, imgdown : widestring;
  Num:integer;
  OleGraphic: TOleGraphic;
  fs: TFileStream;

begin
  ADPXMLBLOG := Combobox1.Text;
  if ADPXMLBLOG <> '' then
 begin
  ADPLocalFile := 'rss.xml';
  try
    if not DownloadURLFile(ADPXMLBLOG, ADPLocalFile)  then
    begin
      Raise Exception.CreateFmt('Adresin Rss Linki Olduğuna Emin Olun !',[]);
      Exit;
    end;
     if not FileExists(ADPLocalFile) then
    begin
      raise exception.Create('Başlıklar Alınamadı !');
      Exit;
    end;
    lv.Clear;

    XMLDoc.FileName := ADPLocalFile;
    XMLDoc.Active:=True;

    StartItemNode:=XMLDoc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('item');
    ANode := StartItemNode;
    repeat
      STitle := ANode.ChildNodes['title'].Text;
      sLink := ANode.ChildNodes['link'].Text;
      sDesc := ANode.ChildNodes['description'].Text;
      with LV.Items.Add do
      begin
        Caption := STitle;
        SubItems.Add(sLink);
        SubItems.Add(sDesc);
      end;
       ANode := ANode.NextSibling;
    until ANode = nil;

    StartItemNode2:=XMLDoc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('image');
    ANode2 := StartItemNode2;
      imgdown := ANode.ChildNodes['url'].Text;
      DownloadURLFile(imgdown, 'c:\img.gif');
      finally
      try
    OleGraphic := TOleGraphic.Create;
    fs := TFileStream.Create('C:\img.gif', fmOpenRead or fmSharedenyNone);
    OleGraphic.LoadFromStream(fs);
    Image1.Picture.Assign(OleGraphic);
    finally
  
    fs.Free;
    OleGraphic.Free;

    DeleteFile(ADPLocalFile);
     XMLDoc.Active:=false;
    end;
  end; 
end;

Автор: muratboy31 4.3.2010, 19:04
how can i download an image from internet and show it in the project ?

Автор: Frees 4.3.2010, 20:37
Код


uses Wininet

function GetInetFile(const fileURL, FileName: String): boolean;
const BufferSize = 1024;
var hSession, hURL: HInternet;
Buffer: array[1..BufferSize] of Byte;
BufferLen: DWORD;
f: File;
sAppName: string;
begin
   Result:=False;
   sAppName := ExtractFileName(Application.ExeName);
   hSession := InternetOpen(sAppName, INTERNET_OPEN_TYPE_PRECONFIG,
         nil, nil, 0);
   try
      hURL := InternetOpenURL(hSession,
          PChar(fileURL),nil,0,0,0);
      if hURL = nil then
        Exit;
      try
         AssignFile(f, FileName);
         Rewrite(f,1);
         repeat
            InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
            BlockWrite(f, Buffer, BufferLen)
         until BufferLen = 0;
         CloseFile(f);
         Result:=True;
      finally
      InternetCloseHandle(hURL)
      end
   finally
   InternetCloseHandle(hSession)
   end
end;

function GetTempDir: String;
var
  Buf: array[0..1023] of Char;
begin
  SetString(Result, Buf, GetTempPath(Sizeof(Buf)-1, Buf));
end;

procedure TForm1.Button1Click(Sender: TObject);
var s: string;
begin
  Randomize;
  s := GetTempDir+IntToStr(Random(10000)) + '.bmp';
  if GetInetFile('url',s) then
  begin
    Image1.Picture.LoadFromFile(s);
    DeleteFile(s);
  end;
end;

Автор: muratboy31 4.3.2010, 21:11
thanks for answer frees
but i got error 


user posted image

Автор: Frees 4.3.2010, 21:20
Код

 hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG,
         nil, nil, 0);

Автор: muratboy31 4.3.2010, 21:43
thanks for the code 
can i download gif or png or jpeg or anything else and show it
or just bmp files ???

Автор: Proxin 7.3.2010, 19:06
anything.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)