Do you own a Debenu Quick PDF Library version 7, 8, 9, 10, 11, 12, 13 or iSEDQuickPDF license? Upgrade to Debenu Quick PDF Library 14 today!

Debenu Quick PDF Library - PDF SDK Community Forum Homepage
Forum Home Forum Home > For Users of the Library > I need help - I can help
  New Posts New Posts RSS Feed - Add an image
  FAQ FAQ  Forum Search   Register Register  Login Login

Add an image

 Post Reply Post Reply
Author
Message
loul View Drop Down
Beginner
Beginner
Avatar

Joined: 04 Jan 11
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote loul Quote  Post ReplyReply Direct Link To This Post Topic: Add an image
    Posted: 04 Jan 11 at 4:16PM
I am trying to add an image of a captured control to a PDF without saving it to a file first.
Saving the image to a file first does work but my report will have ~30 images of screen captured controls and I would prefer to leave the disc out of it. 
 
Environment:
MSVS 2008
C#
QuickPDF 7.22 ActiveX
 
Any help would be appreciated.
TIA,
Lou
Back to Top
Dimitry View Drop Down
Team Player
Team Player


Joined: 18 Feb 10
Status: Offline
Points: 37
Post Options Post Options   Thanks (0) Thanks(0)   Quote Dimitry Quote  Post ReplyReply Direct Link To This Post Posted: 12 Jan 11 at 10:01AM
Please check the following functions:
 
Regards,
Dmitry
Back to Top
loul View Drop Down
Beginner
Beginner
Avatar

Joined: 04 Jan 11
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote loul Quote  Post ReplyReply Direct Link To This Post Posted: 12 Jan 11 at 1:58PM
Originally posted by Dimitry Dimitry wrote:

Please check the following functions:
 

The only one of these that is available in the ActiveX is AddImageFromVariant.

I have tried to use this method using an Image and Bitmap types to no avail.

C# doesn't have a "variant" type so I may need to do some processing that I have not figured out yet since the time was short and I needed to make something work.  I am currently writing the images to temp jpeg files and using the AddImageFromFile.  I am not taking as hard of a performance hit as I expected but I would prefer to avoid writing the files to disc and reading them back if possible.
Back to Top
Dimitry View Drop Down
Team Player
Team Player


Joined: 18 Feb 10
Status: Offline
Points: 37
Post Options Post Options   Thanks (0) Thanks(0)   Quote Dimitry Quote  Post ReplyReply Direct Link To This Post Posted: 24 Jan 11 at 8:15PM
Hope this function imageToByteArray() and the following code sample will help you:
 
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
  MemoryStream ms = new MemoryStream();
  imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
  return ms.ToArray();
}
 
private void button1_Click(object sender, EventArgs e)
{
  QuickPDFAX0723.PDFLibrary qp = new QuickPDFAX0723.PDFLibrary();
  qp.UnlockKey(" . . . "); // Enter valid trial or commercial license key
 
  OpenFileDialog openFileDialog1 = new OpenFileDialog();
  openFileDialog1.DefaultExt = "bmp";
  openFileDialog1.Filter = "Image files (*.bmp)|*.bmp" ;
  if (openFileDialog1.ShowDialog() != DialogResult.OK)
    return;
 
  Image image1 = Image.FromFile(openFileDialog1.FileName);
  qp.AddImageFromVariant(imageToByteArray(image1), 0);
 
  qp.SetOrigin(1);
  qp.DrawImage(10, 10, qp.ImageWidth(), qp.ImageHeight());
  qp.SaveToFile("sample.pdf");
}
 
Regards,
Dmitry
Back to Top
loul View Drop Down
Beginner
Beginner
Avatar

Joined: 04 Jan 11
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote loul Quote  Post ReplyReply Direct Link To This Post Posted: 24 Jan 11 at 9:19PM
I pretty much set this aside because writing the images to temp files and using AddImageFromFile got the job done and the performance hit wasn't too bad.

But I got a direct reply from AndrewC over the weekend and I started playing with this again this AM. Then combining with the reply from Dimitry I played some more (I was actually working of course) and this is what I managed to get working.  This is trimmed a little and there is more code that deals with maintaining the control image's aspect ratio.  I also removed some comments to save space.

Thanks to AndrewC and Dimitry for heading me in the right direction.

// capture an image of a control
private Bitmap CaptureControlImage(Control argControl)
{
  Bitmap MyImage = null;
  Graphics g1 = argControl.CreateGraphics();
  try
  {
    MyImage = new Bitmap(argControl.ClientRectangle.Width,
                          argControl.ClientRectangle.Height,
                          g1);
    Graphics g2 = Graphics.FromImage(MyImage);
    IntPtr dc1 = g1.GetHdc();
    IntPtr dc2 = g2.GetHdc();
    BitBlt(dc2, 0, 0,
           argControl.ClientRectangle.Width,
           argControl.ClientRectangle.Height,
           dc1, 0, 0, 13369376);
    g1.ReleaseHdc(dc1);
    g2.ReleaseHdc(dc2);
  }
  catch
  {
    MyImage = null;
  }
  return (MyImage);
}
 
// convert an image to a byte array
private byte[] GenerateImageBytes(Bitmap argBitMapIn,
                                  ImageFormat argFormat)
{
  using (MemoryStream ms = new MemoryStream())
  {
    argBitMapIn.Save(ms, argFormat);
    return ms.ToArray();
  }
}
 
public bool AddControlImageToPDF(PDFLibrary argPDFLib,
                                 Control argControl
                                 double argLeft,
                                 double argTop,
                                 double argHeight,
                                 double argWidth)
{
  // capture the control image
  Bitmap img = CaptureControlImage(this.pictureBox1);
  if (img == null) return (false);
  // convert to array of bytes
  Byte[] bitBytes = GenerateImageBytes(img, ImageFormat.Png);
  //add image to the PDF
  int iOK = argLib.AddImageFromVariant((Byte[])bitBytes, 0);
  // if all went well draw the image
  if (iOK > 0) argLib.DrawImage(argLeft, argTop, argWidth, argHeight);
  return (iOK > 0);
}

I am sure would are better ways (there always is in programming) but this does the job.  I am planning on using the QuickPDF Library for generating more reports for our instruments and for accessing the help PDFs in the future.

Thanks again,
Lou
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 11.01
Copyright ©2001-2014 Web Wiz Ltd.

Copyright © 2017 Debenu. Debenu Quick PDF Library is a PDF SDK. All rights reserved. AboutContactBlogSupportOnline Store