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;
        }
相关推荐
weixin_307779139 分钟前
Jenkins Gson API插件:统一JSON处理的基础库
java·运维·开发语言·架构·jenkins
高频交易dragon10 分钟前
python缠论形态分析过程
开发语言·网络·python
fufu031111 分钟前
Linux环境下的C语言编程(三十六)
linux·c语言·开发语言·数据结构·算法
rit843249916 分钟前
LTE系统资源分配MATLAB实现示例(基于OFDMA的动态调度)
开发语言·matlab
chilavert31826 分钟前
技术演进中的开发沉思-231 Ajax:页面内容修改
开发语言·前端·javascript
李日灐26 分钟前
C++STL:熟悉vector的底层实现,部分源码解析,迭代器失效和深层次浅拷贝
开发语言·c++
车载测试工程师30 分钟前
CAPL学习-ETH功能函数-方法类2
网络·网络协议·学习·c#·以太网·capl·canoe
在路上看风景31 分钟前
1.12 多线程和异步编程
c#
wuk99831 分钟前
基于MATLAB的混合动力汽车(HEV)简单整车模型实现
开发语言·matlab·汽车
偶像你挑的噻31 分钟前
1.Qt-编译器基本知识介绍
开发语言·qt