简体   繁体   English

C#WPF System.Drawing.Image到System.Windows.Media.Imaging.BitmapSource

[英]C# WPF System.Drawing.Image to System.Windows.Media.Imaging.BitmapSource

I use the library spire.pdf to convert a pdf file into pictures, do it through the code available in the documentation. 我使用spire.pdf库将pdf文件转换为图片,请通过文档中提供的代码来完成。 Generates a PDF file to image because he wants them to print, and PDF file itself can not print (I mean the printer) 生成PDF文件到图像,因为他希望它们打印,而PDF文件本身无法打印(我的意思是打印机)

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();


            Spire.Pdf.PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("sample.pdf");

            BitmapSource source;
            Bitmap bmp;

            for (int i = 1; i < pdf.Pages.Count+1; i++)
            {
                source = pdf.SaveAsImage(i);
                bmp = SourceToBitmap(source);
                bmp.Save(string.Format("result-{0}.png", i), ImageFormat.Png);
            }
        }

        private Bitmap SourceToBitmap(BitmapSource source)
        {
            Bitmap bmp;
            using (MemoryStream ms = new MemoryStream())
            {
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(source));
                encoder.Save(ms);
                bmp = new Bitmap(ms);
            }
            return bmp;
        }
    }

My includes: 我的包括:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Spire.Pdf;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Media.Imaging;
using System.IO;

but I get an error from source = pdf.SaveAsImage(i); 但我从source = pdf.SaveAsImage(i);收到错误source = pdf.SaveAsImage(i);

Error   1   Cannot implicitly convert type 'System.Drawing.Image' to 'System.Windows.Media.Imaging.BitmapSource'    c:\users\łukasz\documents\visual studio 2013\Projects\WpfApplication5\WpfApplication5\MainWindow.xaml.cs    39  26  WpfApplication5

Apparently PdfDocument.SaveAsImage returns a System.Drawing.Image , so you don't need to do any WPF to WinForms bitmap conversion. 显然PdfDocument.SaveAsImage返回一个System.Drawing.Image ,因此您不需要执行任何WPF到WinForms位图转换。

This should be sufficient: 这应该足够了:

var source = pdf.SaveAsImage(i);
source.Save(string.Format("result-{0}.png", i), ImageFormat.Png);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 System.Windows.Media.Imaging.BitmapSource 转换为 System.Drawing.Image - Convert System.Windows.Media.Imaging.BitmapSource to System.Drawing.Image 在C#中的WindowsForm中的PictureBox中显示System.Windows.Media.Imaging.BitmapSource - Show a System.Windows.Media.Imaging.BitmapSource in a PictureBox in WindowsForm in C# 如何将System.Windows.Media.Imaging.BitmapImage转换为System.Drawing.Image? - How to Convert System.Windows.Media.Imaging.BitmapImage to System.Drawing.Image? 如何将&#39;System.Windows.Media.Imaging.BitmapImage&#39;转换为&#39;System.Drawing.Image&#39;? - How to convert 'System.Windows.Media.Imaging.BitmapImage' to 'System.Drawing.Image'? 如何在 C# 中将类型“system.windows.media.imagesource”转换为“system.drawing.image”或 byte[]? - How to convert type 'system.windows.media.imagesource' to 'system.drawing.image' or byte[] in C#? System.Drawing.Image 到 stream C# - System.Drawing.Image to stream C# 如何在C#中将“ system.windows.control.image”转换为“ System.Drawing.Image” - how to “convert a system.windows.control.image” to “System.Drawing.Image” in c# 如何将system.Drawing.Image序列化为C#中的流? - how serialize a system.Drawing.Image into stream in c#? System.Drawing.Image在Web窗体C#2010中不起作用 - System.Drawing.Image not working in Web Form C# 2010 C# .svg 文件到 System.Drawing.Image - C# .svg file to System.Drawing.Image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM