首页 > 其他分享 >视频生成缩略图或pdf文件生成缩略图

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

时间:2023-08-11 20:55:53浏览次数:36  
标签:string using 缩略图 int System 生成 new 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();
}
}
}

标签:string,using,缩略图,int,System,生成,new,pdf
From: https://www.cnblogs.com/wugh8726254/p/17623917.html

相关文章

  • 6529: 构造完全图 最小生成树
    描述 对于完全图G,若有且仅有一棵最小生成树为T,则称完全图G是树T的扩展出的。给你一棵树T,找出T能扩展出的边权和最小的完全图G。 输入 第一行N表示树T的点数。接下来N-1行:Si,Ti,Di;描述一条边(Si,Ti)权值为Di。保证输入数据构成一棵树。对于20%的数据,N<=10对于50%的......
  • 开启想象翅膀:轻松实现文本生成模型的创作应用,支持LLaMA、ChatGLM、UDA、GPT2等模型,开
    开启想象翅膀:轻松实现文本生成模型的创作应用,支持LLaMA、ChatGLM、UDA、GPT2等模型,开箱即用1.介绍TextGen实现了多种文本生成模型,包括:LLaMA、ChatGLM、UDA、GPT2、Seq2Seq、BART、T5、SongNet等模型,开箱即用。1.1最新更新[2023/06/15]v1.0.0版本:新增ChatGLM/LLaMA/Bloom模......
  • 代码生成以及数据生成
    我们在正常开发中设计到数据库的设计,以及对应实体类的代码。我现在讲解两个知识点。代码先行以及数据库先行1、代码先行就是你在程序中创建一个类库,专门用来管理你的实体类实体类写完后,利用ORM框架,譬如EF或者SqlSugar自带的性质可以直接生成数据库,以及数据表而代码实体类创......
  • 关于dev c++显示中文不显示,乱码和生成的可执行文件中文乱码
    1.不显示中文工具----编译器选项----显示-----去掉底下的复选框(第一个consolas下面)2,运行窗口中文乱码方法:1、工具—编译选项2、在第一个框中填入-fexec-charset=gbk3、勾选“编译器加入以下命令”4、重新编译一次以后运行。  ......
  • 小程序生成App:可跨平台开发的移动应用开发框架
    小程序生成App可以成为一种轻量低门槛的开发App的方式,但是需要根据具体情况进行选择。如果应用需要处理大量数据或需要进行复杂计算,或者需要实现原生特有的功能或交互效果,可能需要选择其他开发方式。在文章开始之前,我们看看目前市面上比较容易上手、低门槛开发App的框架和方式Rea......
  • 【专题】2023快消行业营销白皮书报告PDF合集分享(附原数据表)
    全文链接:https://tecdat.cn/?p=33411我们在这份报告合集中分享了有关中国本土企业的信息,包括快消品企业的渠道布局、所面临的外部风险和挑战,以及如何应对这些挑战。阅读原文,获取专题报告合集全文,解锁文末19份快消品行业相关报告。中国本土企业在制定价格策略方面,面临的......
  • 【专题】2022中国快消品零售市场趋势解读报告PDF合集分享(附原数据表)
    全文链接:https://tecdat.cn/?p=33411我们在这份报告合集中分享了有关中国本土企业的信息,包括快消品企业的渠道布局、所面临的外部风险和挑战,以及如何应对这些挑战。阅读原文,获取专题报告合集全文,解锁文末19份快消品行业相关报告。中国本土企业在制定价格策略方面,面临的......
  • 【专题】618全周期大快消品类趋势报告PDF合集分享(附原数据表)
    全文链接:https://tecdat.cn/?p=33411我们在这份报告合集中分享了有关中国本土企业的信息,包括快消品企业的渠道布局、所面临的外部风险和挑战,以及如何应对这些挑战。阅读原文,获取专题报告合集全文,解锁文末19份快消品行业相关报告。中国本土企业在制定价格策略方面,面临的......
  • 【专题】2022快消品行业线下分销链路数字化转型报告PDF合集分享(附原数据表)
    全文链接:https://tecdat.cn/?p=33411我们在这份报告合集中分享了有关中国本土企业的信息,包括快消品企业的渠道布局、所面临的外部风险和挑战,以及如何应对这些挑战。阅读原文,获取专题报告合集全文,解锁文末19份快消品行业相关报告。中国本土企业在制定价格策略方面,面临的......
  • 【专题】2023年快消品行业的热门话题、受众洞察与消费报告PDF合集分享(附原数据表)
    全文链接:https://tecdat.cn/?p=33411我们在这份报告合集中分享了有关中国本土企业的信息,包括快消品企业的渠道布局、所面临的外部风险和挑战,以及如何应对这些挑战。阅读原文,获取专题报告合集全文,解锁文末19份快消品行业相关报告。中国本土企业在制定价格策略方面,面临的......