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;
        }
相关推荐
VBA63371 小时前
VBA即用型代码手册:利用函数保存为PDF文件UseFunctionSaveAsPDF
开发语言
say_fall1 小时前
C语言编程实战:每日刷题 - day2
c语言·开发语言·学习
有过~2 小时前
Total PDF Converter v6.5.0.356.0 电脑PDF多功能转换器
pdf
上去我就QWER2 小时前
Qt快捷键“魔法师”:QKeySequence
开发语言·c++·qt
Pluto_CSND4 小时前
Java中的静态代理与动态代理(Proxy.newProxyInstance)
java·开发语言
惊讶的猫6 小时前
LSTM论文解读
开发语言·python
獨枭7 小时前
C# 本地项目引用失效与恢复全攻略
开发语言·c#·visual studio
清风与日月7 小时前
c# 上位机作为控制端与下位机通信方式
单片机·嵌入式硬件·c#
国服第二切图仔7 小时前
Rust开发之Trait 定义通用行为——实现形状面积计算系统
开发语言·网络·rust
mjhcsp7 小时前
C++ 循环结构:控制程序重复执行的核心机制
开发语言·c++·算法