天蓝色函数 c# .net 中的 DrawingCore 异常 [英] DrawingCore exception in azure function c# .net

查看:37
本文介绍了天蓝色函数 c# .net 中的 DrawingCore 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未处理的异常:System.MemberAccessException:

Object is busy and not state allow this operation [GDI+ status: ObjectBusy]在 System.DrawingCore.GDIPlus.CheckStatus(状态状态)在 System.DrawingCore.Image.Dispose(Boolean disposing)在 System.DrawingCore.Image.Finalize()

以下代码偶尔会出现此错误.我正在使用 sautinsoft 库,而 imageFormat 是 System.DrawingCore.Imaging.

 using (Stream fs = pdfFile.OpenReadStream()){等待 Task.Run(() => _pdfFocus.OpenPdf(fs));如果(_pdfFocus.PageCount > 0){_pdfFocus.ImageOptions.ImageFormat = imageFormat;_pdfFocus.ImageOptions.Dpi = 100;_pdfFocus.ImageOptions.JpegQuality = 90;for (int i = 1; i <= _pdfFocus.PageCount; i++){await Task.Run(() => pdfPagesAsImageFileList.Add(_pdfFocus.ToImage(i)));}}Task.WaitAll();}

解决方案

就像 Marc 所说的 azure 有沙盒限制,而且大多数 .net 包将 pdf 转换为图像需要 GDI,这是不支持的.现在我只找到一个包来使用 .net 来实现它.您可以尝试使用

Unhandled Exception: System.MemberAccessException:

Object is busy and cannot state allow this operation [GDI+ status: ObjectBusy]
       at System.DrawingCore.GDIPlus.CheckStatus(Status status)
       at System.DrawingCore.Image.Dispose(Boolean disposing)
       at System.DrawingCore.Image.Finalize()

This error occurs once in a while for following code. I am using sautinsoft library and imageFormat is of System.DrawingCore.Imaging.

  using (Stream fs = pdfFile.OpenReadStream())
      {
        await Task.Run(() => _pdfFocus.OpenPdf(fs));
        if (_pdfFocus.PageCount > 0)
          {
            _pdfFocus.ImageOptions.ImageFormat = imageFormat;
            _pdfFocus.ImageOptions.Dpi = 100;
            _pdfFocus.ImageOptions.JpegQuality = 90;
            for (int i = 1; i <= _pdfFocus.PageCount; i++)
              {
                 await Task.Run(() => pdfPagesAsImageFileList.Add(_pdfFocus.ToImage(i)));
              }
            }
         Task.WaitAll();
       }

解决方案

Like Marc said the azure has sandbox limitations, and mostly .net package to convert pdf to image need GDI and this is not supported. For now I only find one package to iplement it with .net. You could try with GhostScript and Magick.NET-Q16-AnyCPU to implement it.

After installing the Ghostscript, you will get the gsdll32.dll file in the bin folder. The below is my test code, copy the 02.pdf and gsdll32.dll to the kudu wwwroot folder.

using System.Net.Http;
using ImageMagick;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

namespace FunctionApp6
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log,ExecutionContext context)
        {
            log.Info("C# HTTP trigger function processed a request.");

            MagickNET.SetGhostscriptDirectory(context.FunctionAppDirectory);
            log.Info(context.FunctionAppDirectory);
            MagickReadSettings settings = new MagickReadSettings();
            // Settings the density to 300 dpi will create an image with a better quality
            settings.Density = new Density(300, 300);

            using (MagickImageCollection images = new MagickImageCollection())
            {
                log.Info(context.FunctionAppDirectory + "\02.pdf");
                // Add all the pages of the pdf file to the collection
                images.Read(context.FunctionAppDirectory+"\02.pdf", settings);

                int page = 1;
                foreach (MagickImage image in images)
                {
                    log.Info(context.FunctionAppDirectory + "\outpng" + page + ".png");
                    // Write page to file that contains the page number
                    image.Write(context.FunctionAppDirectory + "\outpng" + page + ".png");
                    // Writing to a specific format works the same as for a single image
                    //image.Format = MagickFormat.Ptif;
                    //image.Write(SampleFiles.OutputDirectory + "Snakeware.Page" + page + ".tif");
                    page++;
                }
            }

            log.Info("convert finish");
        }
    }
}

And here is the result pic in azure.

这篇关于天蓝色函数 c# .net 中的 DrawingCore 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆