ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Image from Internet (Unknown graphic class)
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

Testomatic

3 Posts

Posted - Sep 11 2014 :  13:28:23  Show Profile  Reply
Hello,
I'm using TRichView and want to support copy&paste from the internet.
If the user paste a image from the internet it should be 50% smaller than its original size.
But I have no idea how to realize this.
Here is my existing code:
function GetGraphicClass(const ext: String): TGraphicClass;
begin
  if ext='.bmp' then
    Result := TBitmap
  else if ext='.ico' then
    Result := TIcon
  else if (ext='.wmf') or (ext='.emf') then
    Result := TMetafile
  else if (ext='.jpg') or (ext='.jpeg') then
    Result := TJpegImage
  else if (ext='.tif') or (ext='.tiff') then
    Result := TWICImage
  else if ext='.png' then
    Result := TPngImage
  else if ext='.gif' then
    Result := TGifImage
  else
    Result := nil;
end;

function DownloadImage(const URL: String): TGraphic;
var GrClass: TGraphicClass;
    Stream: TMemoryStream;
    IdHTTP: TIdHTTP;
begin
  Result := nil;
  if Copy(LowerCase(URL), 1, 7)<>'http://' then begin
    Application.MessageBox('"http://" is required', 'Error');
    exit;
  end;
  GrClass := GetGraphicClass(LowerCase(ExtractFileExt(URL)));
  if GrClass=nil then begin
    Application.MessageBox('Unknown picture format', 'Error');
    exit;
  end;
  Stream := TMemoryStream.Create;
  try
    IdHTTP:= TIdHTTP.Create(nil);
    try
      IdHTTP.Get(URL, Stream);
    except
      Application.MessageBox('Cannot download the image', 'Error');
      FreeAndNil(Stream);
    end;
    IdHTTP.Free;
    if Stream<>nil then
      try
        Result := GrClass.Create;
        Stream.Position := 0;
        Result.LoadFromStream(Stream);
      except
        Application.MessageBox('Invalid image', 'Error');
        FreeAndNil(Result);
      end;
  finally
    Stream.Free;
  end;
end;

As you can see I ll get a TGraphic as a return type.
Now I dont know what to do next.
How can I load this TGraphic into a TImageEn(IO?View?Proc? or what else?).
I hope you can help me.

Best regards
Testo

xequte

38616 Posts

Posted - Sep 11 2014 :  14:22:48  Show Profile  Reply
Hi Testo

Well using your existing method, you could just resize the image file, after downloading and before loading it into the TGraphic. See the IEResampleImageFile method in iexHelperFunctions for the code.

http://www.imageen.com/help/IEResampleImageFile.html


Another option is to use a TIEBitmap to download the image:

http://www.imageen.com/help/TIEBitmap.LoadFromURL.html

Then resize it:

http://www.imageen.com/help/TIEBitmap.Resample.html

And finally assign it to the TGraphic:

MyGraphic.Assign(MyIEBitmap.VclBitmap);

(Actually OTOH I can't remember if you can assign a TBitmap directly to a TGraphic).



Nigel
Xequte Software
www.xequte.com
nigel@xequte.com
Go to Top of Page

Testomatic

3 Posts

Posted - Sep 11 2014 :  14:37:12  Show Profile  Reply
That sounds nice, thank you for the fast answer.
Won't be there problems if the image is a png or jpeg?
Go to Top of Page

w2m

USA
1990 Posts

Posted - Sep 11 2014 :  14:49:07  Show Profile  Reply
No. because MyIEBitmap.VclBitmap is a TBitmap regardless of the file type that is loaded. Although Nigel pointed out that you may not be able to assign a TBitmap to TGraphic, but you could also try to assign to TPicture or TBitmap. TBitmap will work for sure.

Here is some tested code:
function DownloadUrl(AUrl: string): TBitmap;
{ Download a bitmap from a url. }
var
  iIEBitmap: TieBitmap;
begin
  iIEBitmap := TieBitmap.Create;
  try
    if iIEBitmap.LoadFromURL(AUrl, '', '', '') then
    begin
      { Resample the TIEBitmap to 50% }
      iIEBitmap.Resample(iIEBitmap.Width div 2, -1, rfNone);
      Result := TBitmap.Create;
      iIEBitmap.CopyToTBitmap(Result);
    end
    else
      MessageBox(0, 'Error downloading image.', 'Warning',
        MB_ICONWARNING or MB_OK);
  finally
    iIEBitmap.Free;
  end;
end;

procedure TForm1.DownloadClick(Sender: TObject);
{ Download a bitmap from a url and assign the bitmap to TImageEnView. }
var
  iBitmap: TBitmap;
begin
  Screen.Cursor := crHourGlass;
  try
    iBitmap := TBitmap.Create;
    try
      iBitmap := DownloadUrl(Url1.Text);
      ImageEnView1.IEBitmap.Assign(iBitmap);
      ImageEnView1.Update;
    finally
      iBitmap.Free;
    end; 
  finally
    Screen.Cursor := crDefault;
  end;
end;


If you really need a TGraphic use a TPicture instead.
When the type of graphic is known, store the graphic in its specific type object. Otherwise, use a TPicture object that can hold any type of TGraphic:
function DownloadUrl2(AUrl: string): TPicture;
{ Download a TPicture from a url. }
var
  iIEBitmap: TieBitmap;
begin
  iIEBitmap := TieBitmap.Create;
  try
    { Load an image from a url }
    if iIEBitmap.LoadFromURL(AUrl, '', '', '') then
    begin
      { Resample the TIEBitmap to 50% }
      iIEBitmap.Resample(iIEBitmap.Width div 2, -1, rfNone);
      Result := TPicture.Create;
      { Copy the TIEBitmap to TBitmap }
      iIEBitmap.CopyToTBitmap(Result.Bitmap);
    end
    else
      MessageBox(0, 'Error downloading image.', 'Warning',
        MB_ICONWARNING or MB_OK);
  finally
    iIEBitmap.Free;
  end;
end;

procedure TForm1.DownloadClick(Sender: TObject);
{ Download a picture from a url and assign the bitmap to TImageEnView. }
var
  iPicture: TPicture;
begin
  Screen.Cursor := crHourGlass;
  try
    iPicture := TPicture.Create;
    try
      { Get the TPicture }
      iPicture := DownloadUrl2(Url1.Text);
      { Display the picture }
      ImageEnView1.IEBitmap.Assign(iPicture.Bitmap);
      ImageEnView1.Update;
    finally
      iPicture.Free;
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page

Testomatic

3 Posts

Posted - Sep 11 2014 :  15:48:50  Show Profile  Reply
Okay, that works fine.
Here is my code
procedure TF_Main.RvHtmlImporter1ImageRequired2(Sender: TObject;
  const src: string; var Width, Height: Integer; var Img: TGraphic);
var bmp: TBitmap;
begin
 bmp:= TBitmap.Create;
 ImageEnIO1.LoadFromURL(src);
 ImageEnProc1.AttachedIEBitmap:=ImageEnIO1.IEBitmap;
 ImageEnProc1.Resample(300,300);
 Width:= 300;
 Height:= 300;

 bmp.Assign(ImageEnProc1.AttachedIEBitmap.VclBitmap);
 Img:= bmp;
end;

But now I have the problem that I'm losing the transparency. If the image is a png I want to keep the transparency :/

EDIT:
Thank you very much for the example.
The graphic class is known, but
iIEBitmap.CopyToTBitmap(Result.Bitmap);
will remove the transparency anyway.
Go to Top of Page

w2m

USA
1990 Posts

Posted - Sep 11 2014 :  16:34:59  Show Profile  Reply
In your last example... Resample converts the bitmap to 24-bit so you probably have to convert it back to 32-bit after resampling.

procedure TForm1.DownloadClick(Sender: TObject);
{ Download a bitmap from a url and assign the bitmap to TImageEnView. }
var
  iPicture: TPicture;
  iRGB: TRGB;
begin
  Screen.Cursor := crHourGlass;
  try
    iPicture := TPicture.Create;
    try
      { Get the TPicture }
      iPicture := DownloadUrl2(Url1.Text);
      { Display the picture }
      ImageEnView1.IEBitmap.Assign(iPicture.Bitmap);
      { Make transparent }
      iRGB := ImageEnView1.IEBitmap.Pixels[0, ImageEnView1.IEBitmap.Height - 1];
      ImageEnView1.Proc.SetTransparentColors(iRGB, iRGB, 0);
      ImageEnView1.Update;
    finally
      iPicture.Free;
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: