c# PDF 转换成图片

 

1.新建项目

2.新增一个新文件夹“lib”(主要是为了存放引用的dll)


3.将“gsdll32.dll  、PDFLibNet.dll  、PDFView.dll”3个dll添加到文件夹中

4.项目添加“PDFLibNet.dll  、PDFView.dll”2个类库的引用,并将gsdll32.dll 拷贝到项目生产根目录中(bin 目录)

(注意:gsdll32.dll 是无法和其它两个dll 一样添加到项目中进行引用的,这一点我之前未能给大家作出说明,不好意思,^_^ )


 

5.在主界面中添加文本框“TextBox1”,浏览按钮“button1”,转换按钮“button2”和文件选择控件“OpenFileDialog1”

 

6.执行方式:点击浏览按钮选择一个PDF,点击“转换按钮”即可

7.源代码:Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PDFEditer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 将PDF 相应的页转换为图片
        /// </summary>
        /// <param name="strPDFpath">PDF 路径</param>
        /// <param name="Page">需要转换的页页码</param>
        private void GetImage(string strPDFpath,int Page)
        {
            FileInfo file=new FileInfo (strPDFpath);
            string strSavePath=file.Directory.FullName;
            byte[] ImgData = GetImgData(strPDFpath, Page);

            MemoryStream ms = new MemoryStream(ImgData, 0, ImgData.Length);
            Bitmap returnImage = (Bitmap)Bitmap.FromStream(ms);

            string strImgPath=Path.Combine(strSavePath,string.Format("PDFConvert{0}.jpg",Page));
            returnImage.Save(strImgPath);
        }

        /// <summary>
        /// 从PDF中获取首页的图片
        /// </summary>
        /// <param name="PDFPath">PDF 文件路径</param>
        /// <param name="Page">需要获取的第几页</param>
        /// <returns></returns>
        private byte[] GetImgData(string PDFPath,int Page)
        {
            System.Drawing.Image img = PDFView.ConvertPDF.PDFConvert.GetPageFromPDF(PDFPath,Page, 300, "", true);
            return GetDataByImg(img);//读取img的数据并返回
        }

        /// <summary>
        /// 将单页的PDF转换为图片
        /// </summary>
        /// <param name="_image"></param>
        /// <returns></returns>
        private byte[] GetDataByImg(System.Drawing.Image _image)
        {
            System.IO.MemoryStream Ms = new MemoryStream();
            _image.Save(Ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imgdata = new byte[Ms.Length];
            Ms.Position = 0;
            Ms.Read(imgdata, 0, Convert.ToInt32(Ms.Length));
            Ms.Close();
            return imgdata;
        }


        string Pdfpath = "";
        /// <summary>
        /// 选择需要转换的PDF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Pdfpath = openFileDialog1.FileName;
            }
            else
            {
                Pdfpath = "";
            }
            textBox1.Text = Pdfpath;
        }

        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Pdfpath))
            {
                GetImage(Pdfpath,2);
            }
        }
    }
}

 

源码下载:http://u.163.com/8MJPw5bT   提取码:pe8y8a35

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值