-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDefaultDom2ImageService.cs
More file actions
85 lines (78 loc) · 3.01 KB
/
DefaultDom2ImageService.cs
File metadata and controls
85 lines (78 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
namespace BootstrapBlazor.Components;
/// <summary>
/// 默认 Html to Image 实现
/// <param name="runtime"></param>
/// <param name="logger"></param>
/// </summary>
class DefaultDom2ImageService(IJSRuntime runtime, ILogger<DefaultDom2ImageService> logger) : IDom2ImageService
{
private JSModule? _jsModule;
/// <summary>
/// <inheritdoc/>
/// </summary>
public async Task<string?> GetUrlAsync(string selector, Dom2ImageOptions? options = null, CancellationToken token = default)
{
string? data = null;
try
{
_jsModule ??= await LoadModule();
data = await _jsModule.InvokeAsync<string?>("getUrl", token, selector, options);
}
catch (OperationCanceledException) { }
catch (Exception ex)
{
logger.LogError(ex, "{GetUrlAsync} throw exception: {ex}", nameof(GetUrlAsync), ex.Format());
}
return data;
}
/// <summary>
/// <inheritdoc/>
/// </summary>
public async Task<Stream?> GetStreamAsync(string selector, Dom2ImageOptions? options = null, CancellationToken token = default)
{
Stream? data = null;
try
{
_jsModule ??= await LoadModule();
var streamReference = await _jsModule.InvokeAsync<IJSStreamReference?>("getStream", selector, options);
if (streamReference != null)
{
data = await streamReference.OpenReadStreamAsync(streamReference.Length, token);
}
}
catch (OperationCanceledException) { }
catch (Exception ex)
{
logger.LogError(ex, "{GetStreamAsync} throw exception: {ex}", nameof(GetStreamAsync), ex.Format());
}
return data;
}
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="selector"></param>
/// <param name="fileName"></param>
/// <param name="format"></param>
/// <param name="backgroundColor"></param>
/// <param name="options"></param>
/// <returns></returns>
public async Task DownloadAsync(string selector, string fileName = "capture", string? format = "png", string? backgroundColor = null, Dom2ImageOptions? options = null)
{
try
{
_jsModule ??= await LoadModule();
await _jsModule.InvokeAsync<IJSStreamReference?>("downloadAsync", selector, fileName, format, backgroundColor, options);
}
catch (OperationCanceledException) { }
catch (Exception ex)
{
logger.LogError(ex, "{DownloadAsync} throw exception: {ex}", nameof(DownloadAsync), ex.Format());
}
}
private Task<JSModule> LoadModule() => runtime.LoadModule("./_content/BootstrapBlazor.Dom2Image/dom2image.js");
}