Print Page | Close Window

Add an image

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=1698
Printed Date: 08 Sep 24 at 5:45AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Add an image
Posted By: loul
Subject: Add an image
Date 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



Replies:
Posted By: Dimitry
Date Posted: 12 Jan 11 at 10:01AM
Please check the following functions:
http://www.quickpdflibrary.com/help/quickpdf/AddImageFromStream.php - AddImageFromStream()
http://www.quickpdflibrary.com/help/quickpdf/AddImageFromString.php - AddImageFromString()
http://www.quickpdflibrary.com/help/quickpdf/AddImageFromVariant.php - AddImageFromVariant()
 


-------------
Regards,
Dmitry


Posted By: loul
Date Posted: 12 Jan 11 at 1:58PM
Originally posted by Dimitry Dimitry wrote:

Please check the following functions:
http://www.quickpdflibrary.com/help/quickpdf/AddImageFromStream.php - AddImageFromStream()
http://www.quickpdflibrary.com/help/quickpdf/AddImageFromString.php - AddImageFromString()
http://www.quickpdflibrary.com/help/quickpdf/AddImageFromVariant.php - AddImageFromVariant()
 

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.


Posted By: Dimitry
Date 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


Posted By: loul
Date 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



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk