Hi,
I m trying to use Radaee Windows Lib to generate Bitmaps from a PDF file but I m facing a System.AccessViolation Exception "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." at the rendering RenderToBmp method :
page.RenderToBmp(m_dib, mat, true, PDF_RENDER_MODE.mode_normal);
In fact, I think that the problem is that the PDFMatrix cannot support a scale = 2.5 with a PageHeight 1350 and PageWidth 2400 because when I tried to change the scale to 2 the Exception disappears but I m getting a Bad rendering quality.
I m using this code to generate Bitmap from a Pdf File :
public async static Task RenderPDFPagesAsync(StorageFile file)
{
var tempFolder = await LocalFileManager.GetTempCachePdfFolderAsync(file);
var doc = new RDPDFLib.pdf.PDFDoc();
try
{
using (var stream = await file.OpenReadAsync())
{
doc.Open(stream, "");
}
double previousPageHeight = 0, previousPageWidth = 0;
PDFBmp m_dib = null;
foreach (var index in Enumerable.Range(0, (int)doc.PageCount))
{
var page = doc.GetPage(index);
try
{
var cFileName = index.ToString() + ".jpg";
page.RenderPrepare();
var pageHeight = doc.GetPageHeight(index);
var pageWidth = doc.GetPageWidth(index);
float scale = (float)1.0;
if (pageHeight < 1500 && pageWidth < 10000 || pageWidth < 1500 && pageHeight < 10000)
{
scale = (float)2.5;
}
pageHeight = pageHeight * scale;
pageWidth = pageWidth * scale;
if (previousPageHeight != pageHeight || previousPageWidth != pageWidth || m_dib == null)
{
m_dib = new PDFBmp((int)pageWidth, (int)pageHeight);
previousPageHeight = pageHeight;
previousPageWidth = pageWidth;
}
m_dib.Reset(0xFFFFFF);
PDFMatrix mat = new PDFMatrix(scale, -scale, 0, pageHeight);
page.RenderToBmp(m_dib, mat, true, PDF_RENDER_MODE.mode_normal);
m_dib.SaveJPG(tempFolder.Path + "\\" + cFileName, 90);
}
catch (Exception e) { } //TODO log
finally
{
page.Close();
}
}
}
catch (Exception) { }//TODO log
finally
{
doc.Close();
}
}
And This is the Exception StackTrace :
at RDPDFLib.pdf.PDFPage.RenderToBmp(PDFBmp bmp, PDFMatrix mat, Boolean show_annot, PDF_RENDER_MODE mode)
at SalesApp.Core.Common.PDFHelper.<RenderPDFPagesAsync>d__2.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Object stateMachine)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c__DisplayClass4_0.<OutputAsyncCausalityEvents>b__0()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
at System.Runtime.CompilerServices.TaskAwaiter.<>c__DisplayClass11_0.<OutputWaitEtwEvents>b__0()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
at System.Threading.Tasks.AwaitTaskContinuation.InvokeAction(Object state)
at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.Run(Task task, Boolean canInlineContinuationTask)
at System.Threading.Tasks.Task.FinishContinuations()
at System.Threading.Tasks.Task.FinishStageThree()
at System.Threading.Tasks.Task`1.TrySetResult(TResult result)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.SetResult(TResult result)
at SalesApp.Core.IO.LocalFileManager.<GetTempCachePdfFolderAsync>d__58.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Object stateMachine)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c__DisplayClass4_0.<OutputAsyncCausalityEvents>b__0()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
at System.Runtime.CompilerServices.TaskAwaiter.<>c__DisplayClass11_0.<OutputWaitEtwEvents>b__0()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.<>c.<.cctor>b__8_0(Object state)
at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()
at System.Threading.WinRTSynchronizationContext.Invoker.InvokeInContext(Object thisObj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.WinRTSynchronizationContext.Invoker.Invoke()
Is there any way to solve this problem ?
Waiting for your response.
Best Regards,
BOUZIDI Hamza