Can anyone help me with this error. I kind of a noob/hack so sorry for my ignorance. I have an existing application that I am trying to add functionality to. I am trying to add some text and a bitmap or jpg to a pdf. I am testing this right now and it works fine, the pdf is created and opens. It looks just as I would expect but I get this Access Violation Exception error just after the document opens. I am using VB 2008 Express Edition. I also have C# 2008 Express Edition so I can compile the source if needed.
Code:
System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="System.Drawing"
StackTrace:
at System.Drawing.SafeNativeMethods.Gdip.GdipDrawImageRectI(HandleRef graphics, HandleRef image, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at CompositeVision.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
I am trying to do this on a button click. Here is a snippet of my code that shows how I am doing it. What am I doing wrong? If I comment out the line that inserts the JPG it seems to run fine one time but if I click the button a second time it gives me the same error.
Code:
Dim TmpBitmap As Bitmap
'Dim TmpObject As Object
Dim TmpText As String
Dim CellContents As String
TmpBitmap = Me.CvsInSightDisplay1.GetBitmap
'TmpBitmap = Me.CvsInSightDisplay1.GetPicture
Me.CvsInSightDisplay1.SaveBitmap("TmpJpg.JPG")
PictureBox2.Image = TmpBitmap
'PictureBox2.Image = TmpObject
' Create a new PDF document
Dim document As PdfDocument = New PdfDocument
document.Info.Title = "Created with PDFsharp"
' Create an empty page
Dim page As PdfPage = document.AddPage
' Create a font
Dim font As XFont = New XFont("Verdana", 15, XFontStyle.Bold)
' Get an XGraphics object for drawing
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
' Draw the text
gfx.DrawString("Test Cognex PDF", font, XBrushes.Black, New XRect(0, 10, (page.Width.Point), 30), XStringFormats.Center)
' Draw the text
TmpText = "Cell B14 "
Me.CvsInSightDisplay1.SetCurrentCell(14, 1)
CellContents = Me.CvsInSightDisplay1.CurrentCellExpression
gfx.DrawString(TmpText & CellContents, font, XBrushes.Black, New XRect(0, 30, (page.Width.Point), 50), XStringFormats.Center)
' Insert Bitmap
gfx.DrawImage(XImage.FromFile("TmpJpg.JPG"), New XRect(400, 100, 200, 200))
' Save the document...
Dim filename As String = "CognexPDF.pdf"
document.Save(filename)
' ...and start a viewer.
Process.Start(filename)
document.Dispose()