@using System.Net.Http @using System.Net.Http.Json @using Microsoft.AspNetCore.Components.Forms @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.Web @using Microsoft.AspNetCore.Components.Web.Virtualization @using Microsoft.AspNetCore.Components.WebAssembly.Http @using Microsoft.JSInterop @using WordSearch @using SixLabors.ImageSharp @inject HttpClient HClient Wordsearch Creator
@if(Font != null) {

Wordsearch Creator


@if(string.IsNullOrWhiteSpace(imageUrl)) {
if(failed) { } else if(generating) { } else { } }else{ Wordsearch
Download } }else{ Loading... }
@code { protected override async Task OnInitializedAsync() { SixLabors.Fonts.FontCollection collection=new(); MemoryStream ms=new (await HClient.GetByteArrayAsync("FreeMonoBold.ttf")); ms.Position=0; collection.Add(ms); Font = new SixLabors.Fonts.Font(collection.Families.First(),16); } bool generating{get;set;}=false; bool failed{get;set;}=false; string imageUrl {get;set;}=""; string Words {get;set;}=""; SixLabors.Fonts.Font? Font {get;set;}=null; public void GenerateWordsearch() { if(Font ==null) return; try { string words = Words; generating = true; this.StateHasChanged(); WordSearchGenerator generator=new WordSearchGenerator(); foreach(var word in words.Replace("\r","").Split('\n')) { generator.AddWord(word); } using(var img=generator.GenerateImage(Font,new SixLabors.ImageSharp.PixelFormats.Rgb24(0,0,0),new SixLabors.ImageSharp.PixelFormats.Rgb24(255,255,255))) { using(var ms = new MemoryStream()) {img.SaveAsPng(ms); imageUrl=$"data:image/png;base64,{Convert.ToBase64String(ms.ToArray())}";} } generating=false; failed=false; this.StateHasChanged(); } catch(Exception ex) { failed=true; imageUrl = ""; _=ex; this.StateHasChanged(); } } }