From a6190e12abd2840cf1bc8e7f0c7a7bdc62eee5a7 Mon Sep 17 00:00:00 2001 From: Marcin Badurowicz Date: Tue, 24 May 2022 23:35:13 +0200 Subject: [PATCH] Update readme.md with examples --- readme.md | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 1564ac6..422986d 100644 --- a/readme.md +++ b/readme.md @@ -8,8 +8,8 @@ A [Blurhash](https://github.com/woltapp/blurhash) implementation based on [blurhash.net](https://github.com/MarkusPalcer/blurhash.net) for SkiaSharp, thus fully available for Xamarin and other .NET Standard 2.0 platforms. -Currently allows to decode a blurhash into `SKBitmap`. Creation of blurhash from -images is not supported as of 1.0. +Currently allows to decode a blurhash into `SKBitmap` or encode a `SKBitmap` +into Blurhash string. Several portions of the code are directly copy-pasted from the [System.Drawing implementation](https://github.com/MarkusPalcer/blurhash.net/tree/master/Blurhash-System.Drawing). @@ -21,13 +21,31 @@ Tested on Xamarin.Forms on UWP, iOS and Android. ### Displaying a placeholder for an image in Xamarin.Forms Image control ```csharp -// ... +// decode blurhash instring into a SKBitmap +var im = Blurhash.SkiaSharp.Blurhasher.Decode("LEHV6nWB2yk8pyo0adR*.7kCMdnj", 219, 176); -var decoder = new Blurhash.SkiaSharp.Decoder(); -var im = decoder.Decode("LEHV6nWB2yk8pyo0adR*.7kCMdnj", 219, 176); +// load it into a SKImage var data = SKImage.FromBitmap(im).Encode(); +// use as a source for Image control image1.Source = ImageSource.FromStream(data.AsStream); ``` +### Encoding an resource image into a Blurhash string + +```csharp +// get the image from resources +var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("TestMobileApp.noah.jpg"); + +// load to SKImage and to Image control +var img = SKImage.FromEncodedData(stream); +image1.Source = ImageSource.FromFile("noah.jpg"); + +// create SKBitmap from Image and create a blurhash for it +var hash = Blurhash.SkiaSharp.Blurhasher.Encode(SKBitmap.FromImage(img), 4, 3); + +// and show it in a Editor control +editor1.Text = hash; +``` + ![Example of Xamarin.Forms image with Blurhash switching to loaded image](https://raw.githubusercontent.com/ktos/Blurhash.SkiaSharp/master/ex1.gif)