Closed nikolayyordanov closed 7 years ago
This is due to how P/Invoke in .NET works and is not something (I think) have control over.
What solution do you propose?
OK, I have implemented some additional checks (checking the platform and if the file exists).
public static List<byte[]> PdfStreamToImageArr(MemoryStream ms)
{
var result = new List<byte[]>();
try
{
var pfpath = Path.GetDirectoryName(typeof(PdfDocument).Assembly.Location);
string platformFolderName = "x86";
if (Environment.Is64BitProcess)
{
platformFolderName = "x64";
}
pfpath = Path.Combine(pfpath, platformFolderName, "pdfium.dll");
if (File.Exists(pfpath))
{
using (PdfDocument doc = PdfDocument.Load(ms))
{
for (int i = 0; i < doc.PageCount; i++)
{
int height = (int)doc.PageSizes[i].Height;
int width = (int)doc.PageSizes[i].Width;
using (var ret = new MemoryStream())
{
using (var img = doc.Render(i, width, height, 300,300,
PdfRenderFlags.CorrectFromDpi
| PdfRenderFlags.Grayscale
| PdfRenderFlags.LcdText
| PdfRenderFlags.ForPrinting))
{
//if (width > height)
//{
// img.RotateFlip(RotateFlipType.Rotate90FlipNone);
//}
img.Save(ret, ImageFormat.Png);
//img.Save(ret,)
result.Add(ret.ToArray());
}
}
}
doc.Dispose();
}
}
else
{
throw new DllNotFoundException();
}
}
catch (DllNotFoundException ex)
{
throw new EX(MSG.ERR_MISSING_SERVER_DLL);
}
return result;
}
Thanks.
No problem.
When the dlls are not in place the DLLNotFoundException breaks the current process. Testing this with Console app (hosting WCF). If I check if the dlls are in place and raise DllNotFoundException it works fine.