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;
        }
相关推荐
mailtolaozhao1 小时前
C#入门--Hello world
开发语言·c#
王维志1 小时前
C# 中的 DateTime
开发语言·c#·.net
重生之我是Java开发战士1 小时前
【C语言】文件操作全解析
c语言·开发语言
驾驭人生1 小时前
C#中统计某个字符出现次数的最简单方法
c#
歪歪1006 小时前
HTML 如何转 Markdown
开发语言·chrome·python·程序人生·html
小坏坏的大世界6 小时前
C++中多线程和互斥锁的基本使用
开发语言·c++
路由侠内网穿透6 小时前
本地部署 SQLite 数据库管理工具 SQLite Browser ( Web ) 并实现外部访问
运维·服务器·开发语言·前端·数据库·sqlite
王者鳜錸7 小时前
PYTHON从入门到实践-18Django模版渲染
开发语言·python·django
Hard but lovely7 小时前
C++ STL--> vector的模拟实现!
开发语言·c++
hweiyu007 小时前
IDEA搭建GO环境
开发语言·后端·golang·intellij-idea·idea·intellij idea