Skip to content

Commit 228b8eb

Browse files
authored
docs(signature): document value format (#1748)
* docs(signature): document value format * Update components/signature/overview.md * docs(signature): polish value format example * Update components/signature/overview.md
1 parent c83f461 commit 228b8eb

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

components/signature/overview.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,79 @@ The Blazor Signature component provides an area where users can draw their signa
3232
}
3333
````
3434

35+
## Value Format
36+
37+
The Signature produces a `Value`, which is a Base64-encoded PNG image. To display the image without saving it as a physical file, use the `Value` directly as a data URI. To save the `Value` and use it as a physical image, remove the data URI prefix `data:image/png;base64,` from the beginning of the `Value` string.
38+
39+
To test with physical PNG files, uncomment the code below and run the example in a Blazor Server app.
40+
41+
>caption Using the Signature Value with images
42+
43+
````CSHTML
44+
@*@inject IWebHostEnvironment HostingEnvironment*@
45+
46+
<p>Draw something to see how the Signature Value looks like.</p>
47+
48+
<TelerikSignature @bind-Value="@SignatureValue"
49+
Width="400px"
50+
Height="200px" />
51+
52+
@if (!string.IsNullOrEmpty(SignatureValue))
53+
{
54+
<p><TelerikButton OnClick="@SavePng">Save PNG Image</TelerikButton></p>
55+
56+
@if (ShowPng)
57+
{
58+
<h2>Signature Image as Saved PNG File</h2>
59+
<p><strong>Test this in a Blazor Server app.</strong></p>
60+
<p>The image source does not include <code>@PngBase64Prefix</code></p>
61+
<p><img src="signature.png?@CacheBuster" style="width:400px;" alt="Saved Signature PNG" /></p>
62+
}
63+
64+
<h2>Signature Value</h2>
65+
<div style="width:600px;height:5em;margin-top:2em;overflow:auto;word-break:break-all;">
66+
@( new MarkupString(SignatureValue
67+
.Replace(PngBase64Prefix, $"<strong style=\"color:red\">{PngBase64Prefix}</strong>")) )
68+
</div>
69+
70+
<h2>Signature Image as Data URI</h2>
71+
<p>The <code>img src</code> attribute includes <code>@PngBase64Prefix</code></p>
72+
<p><img src="@SignatureValue" style="width:400px;" alt="Signature PNG as Data URI" /></p>
73+
}
74+
75+
@code {
76+
private string SignatureValue { get; set; } = string.Empty;
77+
78+
private bool ShowPng { get; set; }
79+
private const string SignaturePngFileName = "signature.png";
80+
private const string PngBase64Prefix = "data:image/png;base64,";
81+
private string CacheBuster { get; set; } = string.Empty;
82+
83+
private async Task SavePng()
84+
{
85+
if (!string.IsNullOrEmpty(SignatureValue))
86+
{
87+
// Remove "data:image/png;base64," from SignatureValue
88+
byte[] imageBytes = Convert.FromBase64String(SignatureValue.Substring(PngBase64Prefix.Length));
89+
90+
// This code works only in Blazor Server apps.
91+
// In WebAssembly apps, you need to send the Signature Value to a server first.
92+
93+
//var imageSaveLocation = Path.Combine(HostingEnvironment.WebRootPath, SignaturePngFileName);
94+
95+
//using (var imageFile = new FileStream(imageSaveLocation, FileMode.Create))
96+
//{
97+
// await imageFile.WriteAsync(imageBytes, 0, imageBytes.Length);
98+
// await imageFile.FlushAsync();
99+
//}
100+
101+
CacheBuster = DateTime.Now.Ticks.ToString();
102+
ShowPng = true;
103+
}
104+
}
105+
}
106+
````
107+
35108
## Appearance
36109

37110
The Signature component provides settings to control its appearance, for example colors and borders. [Read more about the Blazor Signature appearance settings...]({%slug signature-appearance%})

0 commit comments

Comments
 (0)