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;
        }
相关推荐
码上飞扬5 分钟前
Java大师成长计划之第20天:Spring Framework基础
java·开发语言
刚入门的大一新生6 分钟前
C++初阶-string类的模拟实现1
开发语言·c++
chenyuhao202423 分钟前
链表面试题7之相交链表
数据结构·算法·链表·面试·c#
Pluchon34 分钟前
硅基计划2.0 学习总结 壹 Java初阶
java·开发语言·学习·算法
wowocpp38 分钟前
Java MVC
java·开发语言·mvc
测试者家园40 分钟前
安装Python和配置开发环境
开发语言·软件测试·人工智能·python·职场和发展·零基础·质量效能
筏.k42 分钟前
智能指针入门:深入理解 C++ 的 shared_ptr
开发语言·c++·c#11.0
Rverdoser1 小时前
代理服务器运行速度慢是什么原因
开发语言·前端·php
牛马baby1 小时前
Java高频面试之并发编程-16
java·开发语言·面试
Blossom.1181 小时前
探索边缘计算:赋能物联网的未来
开发语言·人工智能·深度学习·opencv·物联网·机器学习·边缘计算