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;
        }
相关推荐
xb11327 分钟前
C# WinForms界面设计
开发语言·c#
-Rane15 分钟前
【C++】内存管理
开发语言·c++
DARLING Zero two♡21 分钟前
【计算机网络】简学深悟启示录:序列化&&反序列化
开发语言·计算机网络·php
ID_1800790547321 分钟前
乐天(Letian)商品详情API接口的调用示例与代码实现
开发语言·python
一位搞嵌入式的 genius30 分钟前
深入理解 JavaScript 原型与继承:从基础到进阶
开发语言·前端·javascript
晨非辰31 分钟前
C++波澜壮阔40年|类和对象篇:拷贝构造与赋值重载的演进与实现
运维·开发语言·c++·人工智能·后端·python·深度学习
m0_7190841139 分钟前
滴滴滴滴滴
java·开发语言
董世昌4140 分钟前
深度解析var、let、const的区别与最佳使用场景
开发语言·前端·javascript
FJW02081441 分钟前
Python中的闭包
开发语言·python
C_心欲无痕43 分钟前
Next.js 平行路由:构建模块化动态布局
开发语言·前端·javascript