c# 系列pdf转图片 各种处理3--net3.1到net8 PDFtoImage

最近一直在做pdf渲染图片的问题,nuget PDFtoImage

支持3.1到net8 ,直接上代码

cs 复制代码
        private static void DownloadFileAsync(string url, string localPath)
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
                client.Timeout = TimeSpan.FromSeconds(15);// 设置超时时间

                using (HttpResponseMessage response = client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead).Result)
                {
                    using (var stream = response.Content.ReadAsByteArrayAsync())
                    {
                        var immp = Path.Combine(localPath, $"Page_1.png");
                        var ss = new FileStream(immp, FileMode.Create, FileAccess.Write);
#pragma warning disable CA1416 // Validate platform compatibility
                  
                        using var bitmap = PDFtoImage.Conversion.ToImage(stream.Result,null, 0);
#pragma warning restore CA1416 // Validate platform compatibility
                        var SKEncodedImage = bitmap.Encode(SKEncodedImageFormat.Jpeg, 100);
                        SKEncodedImage.SaveTo(ss);
                    }
                }
            }
        }

        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        /// 将 byte[] 转成 Stream

        public static Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
相关推荐
流星白龙6 分钟前
【Qt】4.项目文件解析
开发语言·数据库·qt
iuuia10 分钟前
05--JavaScript基础语法(1)
开发语言·javascript·ecmascript
郝学胜-神的一滴11 分钟前
深入解析Linux下的`lseek`函数:文件定位与操作的艺术
linux·运维·服务器·开发语言·c++·软件工程
念你那丝微笑13 分钟前
vue实现批量导出二维码到PDF(支持分页生成 PDF)
前端·vue.js·pdf
一晌小贪欢19 分钟前
Python爬虫第4课:XPath与lxml高级解析技术
开发语言·爬虫·python·网络爬虫·python爬虫·python3·python办公
蓝色汪洋21 分钟前
string字符集
java·开发语言
虚行23 分钟前
C#项目连接S7-PLCSIM Advanced读写操作
开发语言·python·c#
让我上个超影吧36 分钟前
深入浅出 Java 中的 CompletableFuture:让异步编程变得简单
java·开发语言
聊聊MES那点事1 小时前
Cogent DataHub vs Kepware,两大工业数据中间件的深度对比分析
开发语言·中间件·opc·opc ua
“抚琴”的人1 小时前
C#中获取程序执行时间
服务器·前端·c#