XPS를 BMP로 변환하는 C# 코드

C#에서 XPS 파일을 BMP 이미지로 변환하는 것은 인쇄, 보관 또는 이미지 처리 워크플로에 통합하기 위해 XPS 문서의 시각적 표현을 추출해야 할 때 일반적인 요구 사항입니다. Conholdate.Total for .NET을 사용하면 이 작업이 간단하고 효율적으로 이루어집니다.


XPS를 BMP로 변환하는 이유는 무엇인가요?

여기 XPS를 BMP로 변환해야 할 몇 가지 이유가 있습니다:

  • 고품질 출력: BMP는 전체 이미지 충실도를 유지하며, 인쇄 또는 디자인 작업에 유용합니다.
  • 간소화된 공유: BMP 이미지 는 플랫폼 간에 쉽게 열고 볼 수 있습니다.
  • 데이터 추출: 분석 또는 OCR을 위한 페이지 수준 콘텐츠 추출.
  • 배치 처리: .NET 자동화를 사용하여 한 번에 여러 페이지 또는 문서를 변환합니다.

XPS를 BMP로 변환하는 C# 코드

다음 단계를 따라 변환을 수행하세요:

  1. XpsDocument 클래스를 사용하여 XPS 문서를 로드하십시오.
  2. BmpSaveOptions의 인스턴스를 생성하고 구성합니다.
  3. SaveAsImage 메서드를 호출하여 BMP 이미지 바이트 배열을 생성합니다.
  4. 각 페이지를 반복하여 이미지를 디스크에 저장합니다.

XPS를 BMP로 변환하는 C# 코드

// Define the working directory.
string dataDir = "/Desktop";
string outputFileName = dataDir + "XPStoImage_out.bmp";

// Instantiate an instance of the XpsLoadOptions class.  
// Load XPS document from the XPS file by initializing an instance of the XpsDocument class. 
XpsDocument document = new XpsDocument(dataDir + "input.xps", new XpsLoadOptions());

// Initialize BmpSaveOptions object with necessary parameters. 
BmpSaveOptions options = new BmpSaveOptions()
{
    SmoothingMode = SmoothingMode.HighQuality,
    Resolution = 300,
    PageNumbers = new int[] { 1, 2, 6 }
};

// Invoke SaveAsImage method to save XPS document to the images byte arrays. 
byte[][][] imagesBytes = document.SaveAsImage(options);

// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
    // Iterate through partition pages
   for (int j = 0; j < imagesBytes[i].Length; j++)
    {
        // Initialize image output stream
        using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
            Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
            Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
            // Write image 
            imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
    }
}

이 코드는 C#에서 XPS 문서를 BMP 이미지로 변환하는 전체 프로세스를 보여줍니다. XPS 파일은 XpsDocument 클래스를 사용하여 로드되며, 렌더링을 준비하기 위해 XpsLoadOptions와 함께 사용됩니다. 그런 다음 이미지 해상도, 스무딩 모드 및 변환할 특정 페이지를 설정할 수 있는 BmpSaveOptions 객체가 초기화됩니다. 이를 통해 모든 페이지가 명확한 레이블과 함께 별도로 저장되어 생성된 이미지를 관리하고 이후 응용 프로그램에서 쉽게 사용할 수 있습니다. 전반적으로 이 방법은 BMP 형식으로 XPS 콘텐츠를 추출하고 보존하는 매우 효율적인 방법을 제공합니다.

결론

C#에서 XPS 문서를 BMP 이미지로 변환하면 개발자가 용이한 통합, 보관 또는 추가 처리를 위한 래스터화된 이미지 출력을 생성할 수 있습니다. Conholdate.Total for .NET은 이러한 문서 변환을 최소한의 노력으로 처리할 수 있는 강력한 API를 제공합니다.

See Also