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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388742.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388742.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388742.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java finally在return_Java finally語句到底是在return之前還是之后執行?

點擊上方“方志朋”&#xff0c;選擇“置頂或者星標”你的關注意義重大&#xff01;網上有很多人探討Java中異常捕獲機制try...catch...finally塊中的finally語句是不是一定會被執行&#xff1f;很多人都說不是&#xff0c;當然他們的回答是正確的&#xff0c;經過我試驗&#…

oracle 死鎖

為什么80%的碼農都做不了架構師&#xff1f;>>> ORA-01013: user requested cancel of current operation 轉載于:https://my.oschina.net/8808/blog/2994537

面試題:二叉樹的深度

題目描述&#xff1a;輸入一棵二叉樹&#xff0c;求該樹的深度。從根結點到葉結點依次經過的結點&#xff08;含根、葉結點&#xff09;形成樹的一條路徑&#xff0c;最長路徑的長度為樹的深度。 思路&#xff1a;遞歸 //遞歸 public class Solution {public int TreeDepth(Tre…

a/b測試_如何進行A / B測試?

a/b測試The idea of A/B testing is to present different content to different variants (user groups), gather their reactions and user behaviour and use the results to build product or marketing strategies in the future.A / B測試的想法是將不同的內容呈現給不同…

hibernate h2變mysql_struts2-hibernate-mysql開發案例 -解道Jdon

Hibernate專題struts2-hibernate-mysql開發案例與源碼源碼下載本案例展示使用Struts2&#xff0c;Hibernate和MySQL數據庫開發一個個人音樂管理器Web應用程序。&#xff0c;可將您的音樂收藏添加到數據庫中。功能有&#xff1a;顯示一個添加記錄的表單和所有的音樂收藏的列表。…

P5024 保衛王國

傳送門 我現在還是不明白為什么NOIPd2t3會是一道動態dp…… 首先關于動態dp可以看這里 然后這里就是把把矩陣給改一改&#xff0c;改成這個形式\[\left[dp_{i-1,0},dp_{i-1,1}\right]\times \left[\begin{matrix}\infty&ldp_{i,1}\\ldp_{i,0}&ldp_{i,1}\end{matrix}\ri…

提取圖像感興趣區域_從圖像中提取感興趣區域

提取圖像感興趣區域Welcome to the second post in this series where we talk about extracting regions of interest (ROI) from images using OpenCV and Python.歡迎來到本系列的第二篇文章&#xff0c;我們討論使用OpenCV和Python從圖像中提取感興趣區域(ROI)。 As a rec…

解決java compiler level does not match the version of the installed java project facet

ava compiler level does not match the version of the installed java project facet錯誤的解決 因工作的關系&#xff0c;Eclipse開發的Java項目拷來拷去&#xff0c;有時候會報一個很奇怪的錯誤。明明源碼一模一樣&#xff0c;為什么項目復制到另一臺機器上&#xff0c;就會…

php模板如何使用,ThinkPHP如何使用模板

到目前為止&#xff0c;我們只是使用了控制器和模型&#xff0c;還沒有接觸視圖&#xff0c;下面來給上面的應用添加視圖模板。首先我們修改下 Action 的 index 操作方法&#xff0c;添加模板賦值和渲染模板操作。PHP代碼classIndexActionextendsAction{publicfunctionindex(){…

理解Windows窗體和WPF中的跨線程調用

你曾開發過Windows窗體程序&#xff0c;可能會注意到有時事件處理程序將拋出InvalidOperationException異常&#xff0c;信息為“ 跨線程調用非法&#xff1a;在非創建控件的線程上訪問該控件”。這種Windows窗體應用程序中 跨線程調用時的一個最為奇怪的行為就是&#xff0c;有…

什么是嵌入式系統

在我們的日常生活中&#xff0c;我們經常使用許多使用嵌入式系統技術設計的電氣和電子電路和套件。計算機&#xff0c;手機&#xff0c;平板&#xff0c;筆記本電腦&#xff0c;數字電子系統以及其他電子和電子設備都是使用嵌入式系統設計的。 什么是嵌入式系統&#xff1f;將硬…

面向數據科學家的實用統計學_數據科學家必知的統計數據

面向數據科學家的實用統計學Beginners usually ignore most foundational statistical knowledge. To understand different models, and various techniques better, these concepts are essential. These work as baseline knowledge for various concepts involved in data …

字符串、指針、引用、數組基礎

1.字符串&#xff1a;字符是由單引號所括住的單個字母、數字或符號。若將單引號改為雙引號&#xff0c;該字符就會變成字符串。它們之間主要的差別是&#xff1a;雙引號的字符串“A”會比單引號的字符串’A’在字符串的最后補上一個結束符’\0’&#xff08;Null字符&#xff0…

suse安裝php,SUSE下安裝LAMP

安裝Apache可以看到編譯安裝Apache出錯&#xff0c;rpm包安裝gcc (首先要安裝GCC)makemake install修改apache端口cd /home/sxit/apache2vi conf/httpd.confListen 8000啟動 apache/home/root/apache2/bin/apachectl start(stop restart)http://localhost:8000安裝一下PHP開發…

自己動手寫事件總線(EventBus)

2019獨角獸企業重金招聘Python工程師標準>>> 本文由云社區發表 事件總線核心邏輯的實現。 <!--more--> EventBus的作用 Android中存在各種通信場景&#xff0c;如Activity之間的跳轉&#xff0c;Activity與Fragment以及其他組件之間的交互&#xff0c;以及在某…

viz::viz3d報錯_我可以在Excel中獲得該Viz嗎?

viz::viz3d報錯Have you ever found yourself in the following situation?您是否遇到以下情況&#xff1f; Your team has been preparing and working tireless hours to create and showcase the end product — an interactive visual dashboard. It’s a culmination of…

php 數組合并字符,PHP將字符串或數組合并到一個數組內方法

本文主要和大家分享PHP將字符串或數組合并到一個數組內方法&#xff0c;有兩種方法&#xff0c;希望希望能幫助到大家。一般寫法&#xff1a;<?php /*** add a string or an array to another array** param array|string $val* param array $array*/function add_val_to_a…

xcode 4 最低的要求是 10.6.6的版本,如果你是 10.6.3的版本,又不想升級的話。可以考慮通過修改版本號的方法進行安裝

xcode 4 最低的要求是 10.6.6的版本&#xff0c;如果你是 10.6.3的版本&#xff0c;又不想升級的話。可以考慮通過修改版本號的方法進行安裝。 一、打開控制臺&#xff1b; 二、使用root用戶&#xff1b; 命令&#xff1a;sudo -s 之后輸入密碼即可 三、編輯 /System/Library/C…

android 調試技巧

1.查看當前堆棧 Call tree new Exception(“print trace”).printStackTrace(); &#xff08;在logcat中打印當前函數調用關系&#xff09; 2.MethodTracing 性能分析與優&#xff08; 函數占用CPU時間&#xff0c; 調用次數&#xff0c; 函數調用關系&#xff09; a) 在程序…

Xml序列化

xml序列化 實現思路 通過程序生成一個xml文件來備份手機短信. 先獲取手機短信的內容 —>通過xml備份.StringBuffer 代碼如下public void click(View view) {StringBuffer sb new StringBuffer();sb.append("<?xml version\"1.0\" encoding\"UTF-8\…