System.Drawing.Bitmap를 사용할 때 발생하는 "The type initializer for 'Gdip' threw an exception." 오류를 해결하는 방법에 대해 알아봅니다.

 

 

 

1. 상황

 

프로젝트에서 System.Drawing.Bitmap을 사용하려고 할 때 Exception이 발생하며 "The type initializer for 'Gdip' threw an exception."와 같은 메시지가 출력됩니다.

 

프로젝트는 Docker Desktop을 사용해 Docker를 지원하는 프로젝트였습니다.

 

 

 

2. 원인

 

MS에서 Docker 지원 프로젝트를 만들면 자동으로 생성해주는 Dockerfile에 의해 생성되는 컨테이너는 libgdiplus를 지원하지 않아 발생하는 문제였습니다.

 

 

 

 

3. 해결

 

Dockerfile에 libgidplus를 지원할 수 있는 코드를 추가해 주면 됩니다. 코드는 다음 글에서 참고하였습니다: Can't use System.Drawing.Common in microsoft/dotnet:runtime

 

Can't use System.Drawing.Common in microsoft/dotnet:runtime · Issue #618 · dotnet/dotnet-docker

Steps to reproduce the issue pull microsoft/dotnet:runtime add .net core app that is using System.Drawing.Common package try and run app Expected behavior Should be able to create thumbnail images ...

github.com

 

# Your Dockerfile
#...
# install System.Drawing native dependencies
RUN apt-get update \
    && apt-get install -y --allow-unauthenticated \
        libc6-dev \
        libgdiplus \
        libx11-dev \
     && rm -rf /var/lib/apt/lists/*
#...

 

위와 같이 Dockefile을 수정 후 코드를 실행하면 정상적으로 동작합니다.

 

 

 

 

 

반응형

+ Recent posts