Programming Delphi Hot to get the image properties in TWebbrowser in Delphi?
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

This example shows how to get the image properties using TWebbrowser in delphi.

Create a new project in Delphi. Put a webbrowser, a button, a listbox onto the form. Then put the codes to the proper position.

uses
  MSHTML_TLB;

//In the FormCreate Handler, assign the following codes to
//make the browser navigate to a web page
procedure TForm1.FormCreate(Sender: TObject);
begin
  Webbrowser1.Navigate('www.codeback.net');
end;

//Double click button1 to assign the following code:
procedure TForm1.Button1Click(Sender: TObject);
var
  ImgWidth, ImgHeight: Integer;
  ImgHref, ImgFileSize, ImgAltText: string;
  doc: IHtmldoc2;
  i: integer;
begin
  //Getting all images in the Webbrowser1
  for i := 0 to WebBrowser1.OleObject.doc.Images.Length - 1 do
  begin
    doc := WebBrowser1.doc as IHtmldoc2;

    //Getting image width.
    ImgWidth := WebBrowser1.OleObject.doc.Images.Item(i).Width;

    //Getting image height.
    ImgHeight := WebBrowser1.OleObject.doc.Images.Item(i).Height;

    //Getting image file size.
    ImgFileSize := (doc.Images.Item(i, 0) as IHTMLImgElement).FileSize;

    //Getting URL of the image
    ImgHref := (doc.Images.Item(i, 0) as IHTMLImgElement).Href;

    //Getting alternative text of this image.
    ImgAltText := (doc.Images.Item(i, 0) as IHTMLImgElement).alt;

    //Display all information in a TListbox
    ListBox1.Items.Add(Format('%s : %d x %d Pixels; %s Bytes; %s',
      [ImgHref, ImgWidth, ImgHeight, ImgFileSize, ImgAltText]));
  end;
end;

Run the program, see what happen.


Add comment


Security code
Refresh

Programming - Delphi