Print Page | Close Window

Adding Drawing.Bitmap

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=1117
Printed Date: 22 Nov 24 at 5:58PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Adding Drawing.Bitmap
Posted By: Jim Sullivan
Subject: Adding Drawing.Bitmap
Date Posted: 23 Jun 09 at 8:06PM
I'm using ASP.NET and one of my systems generates a Drawing.Bitmap object.  I need to then add that to my PDF.  I tried encoding the bitmap and then using AddImageFromString, but it doesn't seem to work.
 
Has anyone else tried to use an AddImage function to add a bitmap from memory, not from a file?
 

Dim stream As New IO.MemoryStream()

bmp.Save(stream, Drawing.Imaging.ImageFormat.Bmp)

bitmapString = enc.GetString(stream.ToArray())

Dim encoding As New System.Text.ASCIIEncoding

Dim buffer As Byte()

buffer = encoding.GetBytes(bitmapString)

qp.AddImageFromString(buffer, 0)

 
bmp is the drawing.bitmap and enc is a Text.Encoding



Replies:
Posted By: Jim Sullivan
Date Posted: 24 Jun 09 at 5:30PM
I created a new app so that I could isolate my requrements.  Here's the function:
 

 
        Dim bmp As New Drawing.Bitmap("C:\temp\b.bmp")
        Dim pr As New QuickPDFDLL0712.PDFLibrary(Server.MapPath("~\") + "bin\QuickPDFDLL0713.dll")
        pr.UnlockKey("mykey")
        Dim x As Int32 = pr.AddImageFromFile("C:\temp\a.bmp", 0)
 
        Dim buffer As Byte()
        Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream
        bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp)
        buffer = stream.ToArray
        Dim y As Int32 = pr.AddImageFromString(buffer, 0)
        pr.SelectImage(x)
        pr.DrawImage(50, 100, 300, 100)
        pr.SelectImage(y)
        pr.DrawImage(50, 300, 300, 100)
        pr.SaveToFile("c:\temp\a.pdf")
 
The variable y returns a 0, meaning that it couldn't add the image.
 


Posted By: Rowan
Date Posted: 25 Jun 09 at 9:57AM
Originally posted by Jim Sullivan Jim Sullivan wrote:

Dim pr As New QuickPDFDLL0712.PDFLibrary(Server.MapPath("~\") + "bin\QuickPDFDLL0713.dll")

This is probably just an error in your post, but you've listed both QuickPDFDLL0712 and QuickPDFDLL0713 there, which won't work.


Posted By: Jim Sullivan
Date Posted: 25 Jun 09 at 2:39PM
Well... since I'm using the DLL and the wrapper, it does work.  The 12 one just references the namespace, which I can set to anything I want.  The only time it would cause a problem is if the function call within the DLL were changed.  Or I'd be missing a reference to a new function.  Neither applies in this case.
 
But anyway, I threw this code together in a new project, and my original project has all of the references correct.


Posted By: deabrew
Date Posted: 20 Jul 09 at 2:21PM
Jim,

Did you get any further with this?


Posted By: Rowan
Date Posted: 22 Jul 09 at 5:55AM
Hi Jim,

It looks as if you're trying to convert from a byte array to a string and then back to a byte array. The code we've provided below should provide a more direct way.

The ToArray method of the stream class returns a byte array that can be passed directly to the AddImageFromString function.

--------------------------
Dim bitmap As New Bitmap(100, 100)
Dim graphics As Graphics = graphics.FromImage(bitmap)
Dim brush As New SolidBrush(Color.Red)
graphics.FillRectangle(brush, 0, 0, 100, 100)
brush = New SolidBrush(Color.Blue)
graphics.FillEllipse(brush, 20, 20, 60, 60)

Dim ms As New IO.MemoryStream()
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Dim imageData As Byte() = ms.ToArray()

Dim qp As New QuickPDFDLL0714.PDFLibrary("<path>\QuickPDFDLL0714.dll")

qp.UnlockKey("<Insert License Key>")

' Set the images to be compressed
qp.CompressImages(1)

' Load the image from the byte array
qp.AddImageFromString(imageData, 0)

' Draw the image onto the page
qp.DrawImage(100, 500, 200, 200)

' Save the PDF
qp.SaveToFile("<path>\system.drawing.pdf")
--------------------------

I hope this helps.

Cheers,
- Rowan.



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