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!
Add an image |
Post Reply |
Author | |
loul
Beginner Joined: 04 Jan 11 Status: Offline Points: 3 |
Post Options
Thanks(0)
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
|
|
Dimitry
Team Player Joined: 18 Feb 10 Status: Offline Points: 37 |
Post Options
Thanks(0)
|
Please check the following functions:
|
|
Regards,
Dmitry |
|
loul
Beginner Joined: 04 Jan 11 Status: Offline Points: 3 |
Post Options
Thanks(0)
|
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.
|
|
Dimitry
Team Player Joined: 18 Feb 10 Status: Offline Points: 37 |
Post Options
Thanks(0)
|
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 |
|
loul
Beginner Joined: 04 Jan 11 Status: Offline Points: 3 |
Post Options
Thanks(0)
|
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 |
|
Post Reply | |
Tweet
|
Forum Jump | Forum Permissions You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |
Copyright © 2017 Debenu. Debenu Quick PDF Library is a PDF SDK. All rights reserved. About — Contact — Blog — Support — Online Store