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;
        }
相关推荐
上去我就QWER15 分钟前
Qt中如何获取系统版本信息
开发语言·qt
我是苏苏1 小时前
C#高级:程序查询写法性能优化提升策略(附带Gzip算法示例)
开发语言·算法·c#
木木子99991 小时前
业务架构、应用架构、数据架构、技术架构
java·开发语言·架构
sali-tec2 小时前
C# 基于halcon的视觉工作流-章56-彩图转云图
人工智能·算法·计算机视觉·c#
大佬,救命!!!6 小时前
C++多线程同步与互斥
开发语言·c++·学习笔记·多线程·互斥锁·同步与互斥·死锁和避免策略
开开心心就好6 小时前
电子报纸离线保存:一键下载多报PDF工具
网络·笔记·macos·pdf·word·音视频·phpstorm
裴嘉靖6 小时前
Vue 生成 PDF 完整教程
前端·vue.js·pdf
赵文宇(温玉)6 小时前
构建内网离线的“github.com“,完美解决内网Go开发依赖
开发语言·golang·github
qq7422349847 小时前
Python操作数据库之pyodbc
开发语言·数据库·python
Joker100857 小时前
仓颉自定义序列化:从原理到高性能多协议实现
开发语言