首页 技术 正文
技术 2022年11月19日
0 收藏 700 点赞 2,664 浏览 4111 个字

用的是破解版的 O2S.Components.PDFRender4NET.dll 插件, 简单引用即可

    public static class PdfToImage
{
public static MemoryStream GetPdfImagePageStream(string pdfInputPath, int pageIndex, ImageFormat format, int width = , int height = , int quality = )
{
try
{
//pdf处理插件
PDFFile pdfFile = PDFFile.Open(pdfInputPath);
int total = pdfFile.PageCount; #region 防止异常参数
if (pageIndex < )
{
pageIndex = ;
}
if (pageIndex > total)
{
pageIndex = total - ;
}
if (quality < )
{
quality = ;
}
if (quality > )
{
quality = ;
}
if (width <= )
{
width = ;
} if (height <= )
{
height = ;
}
#endregion //pdf转换图片
SizeF pageSize = pdfFile.GetPageSize(pageIndex); Bitmap pageImage = pdfFile.GetPageImage(pageIndex, * quality); MemoryStream ms = new MemoryStream(); pageImage.Save(ms, format); //原图
Image img = Image.FromStream(ms, true); double ratio = (double)width / (double)height; double oRatio = (double)img.Width / (double)img.Height; int sbWidth = ; int sbHeight = ; int outX = ;
int outY = ; if (oRatio < ratio)
{
sbWidth = (int)(img.Width * ((double)height / (double)(img.Height)));
sbHeight = height; outX = (width - sbWidth) / ;
}
else
{
sbHeight = (int)(img.Height * ((double)width / (double)(img.Width)));
sbWidth = width; outY = (height - sbHeight) / ;
} //缩放
Image sbImg = new Bitmap(sbWidth, sbHeight);
Graphics sbGra = Graphics.FromImage(sbImg);
sbGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
sbGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
sbGra.Clear(Color.White);
sbGra.DrawImage(img, new System.Drawing.Rectangle(, , sbWidth, sbHeight), new System.Drawing.Rectangle(, , img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel); //补白
Image outImg = new System.Drawing.Bitmap(width, height);
Graphics outGra = System.Drawing.Graphics.FromImage(outImg);
outGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
outGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
outGra.Clear(Color.White);
outGra.DrawImage(sbImg, new System.Drawing.Rectangle(outX, outY, sbWidth, sbHeight), new System.Drawing.Rectangle(, , sbWidth, sbHeight), System.Drawing.GraphicsUnit.Pixel); MemoryStream outMs = new MemoryStream(); outImg.Save(outMs, format); sbImg.Dispose();
outImg.Dispose();
img.Dispose(); return outMs; }
catch (Exception ex)
{ } return new MemoryStream();
} public static MemoryStream GetPdfImagePageStream(Stream stream, int pageIndex, ImageFormat format, int width = , int height = , int quality = )
{
try
{
//pdf处理插件
PDFFile pdfFile = PDFFile.Open(stream);
int total = pdfFile.PageCount; #region 防止异常参数
if (pageIndex < )
{
pageIndex = ;
}
if (pageIndex > total)
{
pageIndex = total - ;
}
if (quality < )
{
quality = ;
}
if (quality > )
{
quality = ;
}
if (width <= )
{
width = ;
} if (height <= )
{
height = ;
}
#endregion //pdf转换图片
SizeF pageSize = pdfFile.GetPageSize(pageIndex); Bitmap pageImage = pdfFile.GetPageImage(pageIndex, * quality); MemoryStream ms = new MemoryStream(); pageImage.Save(ms, format); //原图
Image img = Image.FromStream(ms, true); double ratio = (double)width / (double)height; double oRatio = (double)img.Width / (double)img.Height; int sbWidth = ; int sbHeight = ; int outX = ;
int outY = ; if (oRatio < ratio)
{
sbWidth = (int)(img.Width * ((double)height / (double)(img.Height)));
sbHeight = height; outX = (width - sbWidth) / ;
}
else
{
sbHeight = (int)(img.Height * ((double)width / (double)(img.Width)));
sbWidth = width; outY = (height - sbHeight) / ;
} //缩放
Image sbImg = new Bitmap(sbWidth, sbHeight);
Graphics sbGra = Graphics.FromImage(sbImg);
sbGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
sbGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
sbGra.Clear(Color.White);
sbGra.DrawImage(img, new System.Drawing.Rectangle(, , sbWidth, sbHeight), new System.Drawing.Rectangle(, , img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel); //补白
Image outImg = new System.Drawing.Bitmap(width, height);
Graphics outGra = System.Drawing.Graphics.FromImage(outImg);
outGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
outGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
outGra.Clear(Color.White);
outGra.DrawImage(sbImg, new System.Drawing.Rectangle(outX, outY, sbWidth, sbHeight), new System.Drawing.Rectangle(, , sbWidth, sbHeight), System.Drawing.GraphicsUnit.Pixel); MemoryStream outMs = new MemoryStream(); outImg.Save(outMs, format); sbImg.Dispose();
outImg.Dispose();
img.Dispose(); return outMs; }
catch (Exception ex)
{ } return new MemoryStream();
}
}

DLL下载: O2S.Components.PDFRender4NET.dll

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290