马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
迩来一直在做pdf渲染图片的标题,nuget PDFtoImage
支持3.1到net8 ,直接上代码
- 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;
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |