We started getting the following exception when we updated our environment from ASP.NET 2.0 to ASP.NET 2.0:
----------------------
System.MethodAccessException
Attempt by security transparent method ’ . + . ()’ to access security critical method ‘System.Runtime.InteropServices.Marshal.Copy(IntPtr, Byte[], Int32, Int32)’ failed.
Assembly ‘Aspose.Pdf, Version=5.0.1.0, Culture=neutral, PublicKeyToken=716fcc553a201e56’ is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model. Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.
------------------------------------------------
This seems to be the exact same issue as there was with Pdf.Kit library (Free Support Forum - aspose.com)
Below is the code snippet that produced the error:
private float _pageHeight = PageSize.A4Height;This is the line that causes the error:
private float _pageWidth = PageSize.A4Width;
private const float _marginSize = .1F * 72; //.1 inches. The small margin size is for images that have already been scanned with margins<span style="color:blue;">public</span> Aspose.Pdf.<span style="color:#2b91af;">Image</span> ToAsposeImage(<span style="color:blue;">byte</span>[] imageBytes) { <span style="color:#2b91af;">MemoryStream</span> imageStream = <span style="color:blue;">null</span>; <span style="color:blue;">try</span> { imageStream = <span style="color:blue;">new</span> <span style="color:#2b91af;">MemoryStream</span>(imageBytes); Aspose.Pdf.<span style="color:#2b91af;">Image</span> image = <span style="color:blue;">new</span> Aspose.Pdf.<span style="color:#2b91af;">Image</span>(); <span style="color:green;">//Set the image properties</span> image.ImageInfo.ImageStream = imageStream; image.ImageInfo.ImageFileType = Aspose.Pdf.<span style="color:#2b91af;">ImageFileType</span>.Tiff; image.ImageInfo.TiffFrame = -1; image.ImageScale = 10000f; <span style="color:green;">//just scale to fill the page</span> <span style="color:blue;">return</span> image; } <span style="color:blue;">catch</span> (<span style="color:#2b91af;">Exception</span> ex) { <span style="color:blue;">if</span> (imageStream != <span style="color:blue;">null</span>) imageStream.Dispose(); <span style="color:blue;">throw</span> ex; } } <span style="color:blue;">public</span> <span style="color:blue;">byte</span>[] Convert(<span style="color:blue;">byte</span>[] originalContent) { <span style="color:#2b91af;">MemoryStream</span> outStream = <span style="color:blue;">null</span>; Aspose.Pdf.<span style="color:#2b91af;">Image</span> image = <span style="color:blue;">null</span>; <span style="color:blue;">try</span> { <span style="color:#2b91af;">LicenseManager</span>.Initialize(); <span style="color:green;">//Instantiate a Pdf object by calling its empty constructor</span> <span style="color:#2b91af;">Pdf</span> pdf = <span style="color:blue;">new</span> <span style="color:#2b91af;">Pdf</span>(); <span style="color:green;">//Create a section in the Pdf object</span> <span style="color:#2b91af;">Section</span> section = pdf.Sections.Add(); section.PageInfo.PageHeight = _pageHeight; section.PageInfo.PageWidth = _pageWidth; section.PageInfo.Margin.Left = _marginSize; section.PageInfo.Margin.Right = _marginSize; section.PageInfo.Margin.Top = _marginSize; section.PageInfo.Margin.Bottom = _marginSize; image = ToAsposeImage(originalContent); <span style="color:green;">//Add image object into the Paragraphs collection of the section</span> section.Paragraphs.Add(image); <span style="color:green;">//Save</span> outStream = <span style="color:blue;">new</span> <span style="color:#2b91af;">MemoryStream</span>(); pdf.Save(outStream); <span style="color:blue;">return</span> outStream.ToArray(); } <span style="color:blue;">finally</span> { <span style="color:blue;">if</span> (image != <span style="color:blue;">null</span> && image.ImageInfo.ImageStream != <span style="color:blue;">null</span>) image.ImageInfo.ImageStream.Dispose(); <span style="color:blue;">if</span> (outStream != <span style="color:blue;">null</span>) outStream.Dispose(); } }
pdf.Save(outStream);
Attached is the TIFF file that was being placed into the PDF. It was passed as a byte array into the Convert method.
Thank you for your help.