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;
        }
相关推荐
炸膛坦客18 小时前
单片机/C/C++八股:(十九)栈和堆的区别?
c语言·开发语言·c++
零雲18 小时前
java面试:了解抽象类与接口么?讲一讲它们的区别
java·开发语言·面试
Jay_Franklin18 小时前
Quarto与Python集成使用
开发语言·python·markdown
2401_8318249619 小时前
代码性能剖析工具
开发语言·c++·算法
是wzoi的一名用户啊~19 小时前
【C++小游戏】2048
开发语言·c++
其实秋天的枫19 小时前
2025年12月大学英语六级真题及答案电子版pdf三套全
经验分享·pdf
Sunshine for you19 小时前
C++中的职责链模式实战
开发语言·c++·算法
@我漫长的孤独流浪20 小时前
Python编程核心知识点速览
开发语言·数据库·python
qq_4160187220 小时前
C++中的状态模式
开发语言·c++·算法
2401_8845632420 小时前
模板代码生成工具
开发语言·c++·算法