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
 PDF stream to Image conversion
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

Oribi

Netherlands
17 Posts

Posted - Jul 17 2017 :  09:14:26  Show Profile  Reply
I can successfully convert a PDF to an image using the LoadFromFile (using the code below)

procedure TFrmMain.btnLoadPDFFromFileClick(Sender: TObject);
begin
  if not FImageMagick then
  begin
    TIEMiscPluginsImageMagick.RegisterPlugin;
    FImageMagick := True;
  end;

  if dlgOpen.Execute(Self.Handle) then
  begin
    imgView.IO.Params.ImageIndex := 0;
    imgView.IO.Params.Dict.Insert('PDF:Density', 300);
    imgView.IO.LoadFromFile(dlgOpen.FileName);
  end;
end;


However, I need to be able to convert a PDF stream directly to an image, without having to save it to a file first.

I tried the following code:

procedure TFrmMain.btnLoadPDFFromStreamClick(Sender: TObject);
var
  fs: TFileStream;
begin
  if not FImageMagick then
  begin
    TIEMiscPluginsImageMagick.RegisterPlugin;
    FImageMagick := True;
  end;

  if dlgOpen.Execute(Self.Handle) then
  begin
    fs := TFileStream.Create(dlgOpen.FileName, fmShareDenyNone);
    try
      imgView.IO.Params.ImageIndex := 0;
      imgView.IO.Params.Dict.Insert('PDF:Density', 300);
      imgView.IO.LoadFromStream(fs);
    finally
      fs.Free;
    end;
  end;
end;

but that doesn't work.

Should this work? Or do I have to use a different method to convert the PDF to an image?

Oribi

Netherlands
17 Posts

Posted - Jul 17 2017 :  09:38:33  Show Profile  Reply
I debugged the code and it appears that there is a bug in the "procedure TIEMiscPluginsImageMagick_ReadImageStream" method.

In this method the following check is used to decide which MagickReadImage (file or stream) is used:

if filename <> '' then

However, when this code is executed the filename is [0] for a stream.
Because of this the:

if mstream <> nil then
readOK := TIEMiscPluginsImageMagick.MagickReadImageBlob(mwand, mstream.Memory, mstream.Size);

is never executed.
When I replaced the if statement with

if Stream is TIEWideFileStream then

The read from stream code worked fine.


Go to Top of Page

Oribi

Netherlands
17 Posts

Posted - Jul 17 2017 :  09:44:16  Show Profile  Reply
After looking at the code again I think it would be better (and enough) to just switch the if statements.

So instead of:

if filename <> '' then
readOK := TIEMiscPluginsImageMagick.MagickPingImage(..)
else
if mstream <> nil then
readOK := TIEMiscPluginsImageMagick.MagickPingImageBlob(..);

do:

if mstream <> nil then
readOK := TIEMiscPluginsImageMagick.MagickPingImageBlob(..)
else
if filename <> '' then
readOK := TIEMiscPluginsImageMagick.MagickPingImage(..);

Go to Top of Page

xequte

38176 Posts

Posted - Jul 18 2017 :  04:25:10  Show Profile  Reply
Thanks, I have updated it.

Apologies for the oversight.


Nigel
Xequte Software
www.xequte.com
nigel@xequte.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: