视频生成缩略图或pdf文件生成缩略图

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.WindowsAPICodePack.Shell;
using System.Drawing;
//using PdfiumViewer;
using GhostscriptSharp;
//using System.IO;
//using iTextSharp;
using System.Drawing.Imaging;
//using iTextSharp.text.pdf;
//using iTextSharp.text.pdf.parser;
using O2S.Components.PDFRender4NET;

namespace 获取视频缩略图demo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
try
{
//string mp4URL = Server.MapPath("~/Upload/") + "33.mp4";
//string name = Server.MapPath("~/Upload/") + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";
//ShellFile shellFile = ShellFile.FromFilePath(mp4URL);
//Bitmap thumbnail = shellFile.Thumbnail.ExtraLargeBitmap;
//thumbnail.Save(name);

 


string pdfURL = Server.MapPath("~/Upload/") + "111.pdf";
string outURL = Server.MapPath("~/Upload/") + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";
ConvertPdf3Image(pdfURL, outURL,0,1, ImageFormat.Jpeg,2);

}
catch (Exception ex)
{
string msg = ex.Message;
}
return View();
}

 

 

 

/// <summary>
/// 将PDF文档转换为图片的方法
/// </summary>
/// <param name="pdfInputPath">PDF文件路径</param>
/// <param name="imageOutputPath">图片输出完整路径(包括文件名)</param>
/// <param name="startPageNum">从PDF文档的第几页开始转换</param>
/// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
/// <param name="imageFormat">设置所需图片格式</param>
/// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
private static void ConvertPdf3Image(string pdfInputPath, string imageOutputPath,
int startPageNum, int endPageNum, ImageFormat imageFormat, int definition)
{

PDFFile pdfFile = PDFFile.Open(pdfInputPath);

if (startPageNum <= 0)
{
startPageNum = 1;
}

if (endPageNum > pdfFile.PageCount)
{
endPageNum = pdfFile.PageCount;
}

if (startPageNum > endPageNum)
{
int tempPageNum = startPageNum;
startPageNum = endPageNum;
endPageNum = startPageNum;
}

var bitMap = new Bitmap[endPageNum];

for (int i = startPageNum; i <= endPageNum; i++)
{
Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * definition);
Bitmap newPageImage = new Bitmap(pageImage.Width / 4, pageImage.Height / 4);

Graphics g = Graphics.FromImage(newPageImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;           //重新画图的时候Y轴减去130,高度也减去130 这样水印就看不到了
g.DrawImage(pageImage, new Rectangle(0, 0, pageImage.Width / 4, pageImage.Height / 4),
new Rectangle(0, 130, pageImage.Width, pageImage.Height - 130), GraphicsUnit.Pixel);

bitMap[i - 1] = newPageImage;
g.Dispose();
}

//合并图片
var mergerImg = MergerImg(bitMap);
//保存图片
mergerImg.Save(imageOutputPath, imageFormat);
pdfFile.Dispose();
}

/// <summary>
/// 合并图片
/// </summary>
/// <param name="maps"></param>
/// <returns></returns>
private static Bitmap MergerImg(params Bitmap[] maps)
{
int i = maps.Length;

if (i == 0)
throw new Exception("图片数不能够为0");
else if (i == 1)
return maps[0];

//创建要显示的图片对象,根据参数的个数设置宽度
Bitmap backgroudImg = new Bitmap(maps[0].Width, i * maps[0].Height);
Graphics g = Graphics.FromImage(backgroudImg);
//清除画布,背景设置为白色
g.Clear(System.Drawing.Color.White);
for (int j = 0; j < i; j++)
{
g.DrawImage(maps[j], 0, j * maps[j].Height, maps[j].Width, maps[j].Height);
}
g.Dispose();
return backgroudImg;
}

 

 


#region SplitePDF2


void SplitePDF(string filepath)
{
iTextSharp.text.pdf.PdfReader reader = null;
int currentPage = 1;
int pageCount = 0;
//string filepath_New = filepath +"\\PDFDestination\";

System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
//byte[] arrayofPassword = encoding.GetBytes(ExistingFilePassword);
reader = new iTextSharp.text.pdf.PdfReader(filepath);
reader.RemoveUnusedObjects();
pageCount = reader.NumberOfPages;
string ext = System.IO.Path.GetExtension(filepath);
for (int i = 1; i <= pageCount; i++)
{

//iTextSharp.text.pdf.PdfReader reader1 = new iTextSharp.text.pdf.PdfReader(filepath);
//string outfile = filepath.Replace((System.IO.Path.GetFileName(filepath)), (System.IO.Path.GetFileName(filepath).Replace(".pdf", "") + "_" + i.ToString()) + ext);
//reader1.RemoveUnusedObjects();
//iTextSharp.text.Document doc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(currentPage));
//iTextSharp.text.pdf.PdfCopy pdfCpy = new iTextSharp.text.pdf.PdfCopy(doc, new System.IO.FileStream(outfile, System.IO.FileMode.Create));
//doc.Open();
//for (int j = 1; j <= 1; j++)
//{
// iTextSharp.text.pdf.PdfImportedPage page = pdfCpy.GetImportedPage(reader1, currentPage);
// //pdfCpy.SetFullCompression();
// pdfCpy.AddPage(page);
// currentPage += 1;
//}
//doc.Close();
//pdfCpy.Close();
//reader1.Close();
//reader.Close();

}
}
#endregion

 

static void Convert(string pdfPath, string outputDir)
{
//输出的图片名字固定为 page_1.jpg, page_2.jpg...
//outputDir = Path.Combine(outputDir, "page_%d.jpg");

GhostscriptSettings setting = new GhostscriptSettings
{
Device = GhostscriptSharp.Settings.GhostscriptDevices.jpeg,//输出格式由此字段和后缀共同控制
Resolution = new System.Drawing.Size(100, 100), //图片分辨率,两个相同表示按pdf的宽高等比例导出
Size = new GhostscriptSharp.Settings.GhostscriptPageSize
{
Manual = new System.Drawing.Size(1, 1)//数值随意,但必须有
},
Page = new GhostscriptSharp.Settings.GhostscriptPages
{

AllPages = true //导出全部页面,也可指定开始页面和结束页面
}
};
GhostscriptWrapper.GenerateOutput(pdfPath, outputDir, setting);
}

public void ffmpeg(string mp4, string jpg, int frames)
{
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\ffmpeg.exe";
process.StartInfo.Arguments = $@"-i {mp4} -ss {frames} -f image2 {jpg}";
process.Start();
process.WaitForExit();
process.Close();
}
catch
{

}
}


#region PDF转图片
/// <summary>
/// 将PDF转换为图片的方法
/// </summary>
/// <param name="pdfInputPath">PDF文件路径</param>
/// <param name="imageOutputPath">图片输出路径</param>
/// <param name="imageName">生成图片的名字</param>
/// <param name="startPageNum">从PDF文档的第几页开始转换</param>
/// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
/// <param name="imageFormat">设置所需图片格式</param>
/// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
public static void PdfToImage2(string pdfInputPath, ImageFormat imageFormat)
{

//string YJ = AppDomain.CurrentDomain.BaseDirectory + "JQ\\";
//if (System.IO.Directory.Exists(YJ))
//{
// var files = Directory.GetFiles(YJ);
// for (int i = 0; i < files.Length; i++)
// {
// System.IO.File.Delete(files[i]);
// }
//}
//string imageOutputPath = AppDomain.CurrentDomain.BaseDirectory + "JQ\\";
//int startPageNum = 1;
//PDFFile pdfFile = PDFFile.Open(pdfInputPath);

//PdfReader reader = new PdfReader(pdfInputPath);

//int endPageNum = pdfFile.PageCount;
//if (!Directory.Exists(imageOutputPath))
//{
// Directory.CreateDirectory(imageOutputPath);
//}
//if (startPageNum <= 0)
//{
// startPageNum = 1;
//}
//if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; };
//if (endPageNum > pdfFile.PageCount)
//{
// endPageNum = pdfFile.PageCount;
//}
//if (startPageNum > endPageNum)
//{
// int tempPageNum = startPageNum;
// startPageNum = endPageNum;
// endPageNum = startPageNum;
//}
//for (int i = 0; i < endPageNum; i++)
//{
// //后面乘的那个5是设置清晰度,数字越大越清晰,如果有需要可以设置参数,从外部传参
// Bitmap pageImage = pdfFile.GetPageImage(i, 56 * 2);
// pageImage.Save(imageOutputPath + i.ToString() + "." + imageFormat.ToString(), imageFormat);
// pageImage.Dispose();
//}
//pdfFile.Dispose();
}

#endregion


/// <summary>
/// PDF转图片
/// </summary>
/// <param name="dicomFile">PDF文件路径</param>
/// <param name="destJpgFile">保存图片路径</param>
/// <returns></returns>
public static string PdfToJPG(string dicomFile, string destJpgFile)
{
//string JPGFilePath = string.Empty;
//try
//{
// PdfDocument pdf = PdfDocument.Load(dicomFile);
// int pdfpage = pdf.PageCount;
// var pagesizes = pdf.PageSizes;
// Size size = new Size();
// size.Height = (int)pagesizes[0].Height;
// size.Width = (int)pagesizes[0].Width;
// Bitmap tmpBmp = new Bitmap(size.Width, size.Height);
// Graphics g = Graphics.FromImage(tmpBmp);
// g.Clear(SystemColors.AppWorkspace);
// for (int i = 1; i <= pdfpage; i++)
// {
// System.Drawing.Image image = pdf.Render(i - 1, size.Width, size.Height, 600, 600, PdfRenderFlags.Annotations);
// g.DrawImage(image, 0, (i - 1) * size.Height, size.Width, size.Height);
// image.Dispose();
// }
// tmpBmp.Save(destJpgFile);
// JPGFilePath = destJpgFile;
// tmpBmp.Dispose();
// g.Dispose();
// pdf.Dispose();
//}
//catch (Exception ex)
//{
// JPGFilePath = ex.Message;
//}
//return JPGFilePath;
return "";

}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";

return View();
}
}
}

posted @ 2023-08-11 20:46  .net&new  阅读(68)  评论(0编辑  收藏  举报