-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for converting ImageSource to SKImage (#99)
- Loading branch information
1 parent
6d1c064
commit d75db72
Showing
26 changed files
with
633 additions
and
11 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
samples/SkiaSharpDemo/Demos/Helpers/ImageSourceToImagePage.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:views="clr-namespace:SkiaSharpDemo.Views" | ||
x:Class="SkiaSharpDemo.Demos.ImageSourceToImagePage" | ||
Title="ToImage"> | ||
|
||
<ScrollView> | ||
<StackLayout Padding="12" Spacing="6" HeightRequest="-1"> | ||
<StackLayout.Resources> | ||
<ResourceDictionary> | ||
<Style TargetType="StackLayout"> | ||
<Setter Property="HeightRequest" Value="100" /> | ||
</Style> | ||
<Style TargetType="Image"> | ||
<Setter Property="HorizontalOptions" Value="FillAndExpand" /> | ||
<Setter Property="WidthRequest" Value="100" /> | ||
</Style> | ||
<Style TargetType="views:SKImageView"> | ||
<Setter Property="HorizontalOptions" Value="FillAndExpand" /> | ||
<Setter Property="WidthRequest" Value="100" /> | ||
</Style> | ||
</ResourceDictionary> | ||
</StackLayout.Resources> | ||
|
||
<StackLayout Orientation="Horizontal" Spacing="12" HeightRequest="-1"> | ||
<Label Text="ImageSource" | ||
FontAttributes="Bold" HorizontalTextAlignment="Center" | ||
HorizontalOptions="FillAndExpand" WidthRequest="100" /> | ||
<Label Text="SkiaSharp" | ||
FontAttributes="Bold" HorizontalTextAlignment="Center" | ||
HorizontalOptions="FillAndExpand" WidthRequest="100" /> | ||
</StackLayout> | ||
|
||
<Label Text="File" /> | ||
<StackLayout Orientation="Horizontal" Spacing="12"> | ||
<Image Source="{Binding FileImage}" /> | ||
<views:SKImageView Source="{Binding FileImage}" /> | ||
</StackLayout> | ||
|
||
<Label Text="Stream" /> | ||
<StackLayout Orientation="Horizontal" Spacing="12"> | ||
<Image Source="{Binding StreamImage}" /> | ||
<views:SKImageView Source="{Binding StreamImage}" /> | ||
</StackLayout> | ||
|
||
<Label Text="Uri" /> | ||
<StackLayout Orientation="Horizontal" Spacing="12"> | ||
<Image Source="{Binding UriImage}" /> | ||
<views:SKImageView Source="{Binding UriImage}" /> | ||
</StackLayout> | ||
|
||
<Label Text="Font" /> | ||
<StackLayout Orientation="Horizontal" Spacing="12"> | ||
<Image Source="{Binding FontImage}" /> | ||
<views:SKImageView Source="{Binding FontImage}" /> | ||
</StackLayout> | ||
|
||
<Label Text="SkiaSharp" /> | ||
<StackLayout Orientation="Horizontal" Spacing="12"> | ||
<Image Source="{Binding SkiaSharpImage}" /> | ||
<views:SKImageView Source="{Binding SkiaSharpImage}" /> | ||
</StackLayout> | ||
|
||
</StackLayout> | ||
</ScrollView> | ||
|
||
</ContentPage> |
33 changes: 33 additions & 0 deletions
33
samples/SkiaSharpDemo/Demos/Helpers/ImageSourceToImagePage.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using SkiaSharp; | ||
using SkiaSharp.Views.Forms; | ||
using Xamarin.Forms; | ||
|
||
namespace SkiaSharpDemo.Demos | ||
{ | ||
public partial class ImageSourceToImagePage : ContentPage | ||
{ | ||
public ImageSourceToImagePage() | ||
{ | ||
InitializeComponent(); | ||
|
||
BindingContext = this; | ||
} | ||
|
||
public ImageSource FileImage => | ||
new FileImageSource { File = "logo.png" }; | ||
|
||
public ImageSource StreamImage => | ||
new StreamImageSource { Stream = token => Task.Run(() => App.GetImageResourceStream("logo.png")) }; | ||
|
||
public ImageSource UriImage => | ||
new UriImageSource { Uri = new Uri("https://raw.githubusercontent.com/mono/SkiaSharp.Extended/main/images/logo.png") }; | ||
|
||
public ImageSource FontImage => | ||
new FontImageSource { Glyph = "S", FontFamily = "Arial", Color = Color.Black }; | ||
|
||
public ImageSource SkiaSharpImage => | ||
new SKBitmapImageSource { Bitmap = SKBitmap.Decode(App.GetImageResourceStream("logo.png")) }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using SkiaSharp; | ||
using SkiaSharp.Extended.UI; | ||
using SkiaSharp.Extended.UI.Extensions; | ||
using SkiaSharp.Views.Forms; | ||
using Xamarin.Forms; | ||
|
||
namespace SkiaSharpDemo.Views | ||
{ | ||
public class SKImageView : SKCanvasView | ||
{ | ||
private SKImage? image; | ||
|
||
public static readonly BindableProperty SourceProperty = BindableProperty.Create( | ||
nameof(Source), | ||
typeof(ImageSource), | ||
typeof(SKImageView), | ||
null, | ||
propertyChanged: OnSourceChanged); | ||
|
||
public ImageSource? Source | ||
{ | ||
get => (ImageSource?)GetValue(SourceProperty); | ||
set => SetValue(SourceProperty, value); | ||
} | ||
|
||
protected override void OnPaintSurface(SKPaintSurfaceEventArgs e) | ||
{ | ||
base.OnPaintSurface(e); | ||
|
||
var info = e.Info; | ||
var canvas = e.Surface.Canvas; | ||
|
||
canvas.Clear(SKColors.Transparent); | ||
|
||
if (image != null) | ||
{ | ||
var rect = info.Rect.AspectFit(new SKSizeI(image.Width, image.Height)); | ||
|
||
canvas.DrawImage(image, rect); | ||
} | ||
} | ||
|
||
private static async void OnSourceChanged(BindableObject bindable, object oldValue, object newValue) | ||
{ | ||
if (bindable is SKImageView view) | ||
{ | ||
var source = newValue as ImageSource; | ||
|
||
if (source == null) | ||
view.image = null; | ||
else | ||
{ | ||
try | ||
{ | ||
var img = await source.ToSKImageAsync(); | ||
view.image = img; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.WriteLine($"Unable to load image: " + ex); | ||
|
||
view.image = null; | ||
} | ||
} | ||
|
||
view.InvalidateSurface(); | ||
} | ||
} | ||
} | ||
} |
7 changes: 3 additions & 4 deletions
7
source/SkiaSharp.Extended.UI.WPF/SkiaSharp.Extended.UI.WPF.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
source/SkiaSharp.Extended.UI/Extensions/SKImageSourceExtensions.android.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using SkiaSharp.Views.Android; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Internals; | ||
using Xamarin.Forms.Platform.Android; | ||
|
||
namespace SkiaSharp.Extended.UI.Extensions | ||
{ | ||
public static partial class SKImageSourceExtensions | ||
{ | ||
private static async Task<SKImage?> PlatformToSKImageAsync(ImageSource imageSource, CancellationToken cancellationToken = default) | ||
{ | ||
var handler = Registrar.Registered.GetHandlerForObject<IImageSourceHandler>(imageSource); | ||
if (handler == null) | ||
throw new InvalidOperationException($"Unable to determine the handler for the image source ({imageSource.GetType().Name})."); | ||
|
||
using var bitmap = await handler.LoadImageAsync(imageSource, Android.App.Application.Context, cancellationToken).ConfigureAwait(false); | ||
if (bitmap == null) | ||
return null; | ||
|
||
var image = bitmap.ToSKImage(); | ||
|
||
bitmap.Recycle(); | ||
|
||
return image; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
source/SkiaSharp.Extended.UI/Extensions/SKImageSourceExtensions.ios.macos.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xamarin.Forms; | ||
#if __MOBILE__ | ||
using SkiaSharp.Views.iOS; | ||
using Xamarin.Forms.Platform.iOS; | ||
#else | ||
using SkiaSharp.Views.Mac; | ||
using Xamarin.Forms.Platform.MacOS; | ||
#endif | ||
|
||
namespace SkiaSharp.Extended.UI.Extensions | ||
{ | ||
public static partial class SKImageSourceExtensions | ||
{ | ||
private static async Task<SKImage?> PlatformToSKImageAsync(ImageSource imageSource, CancellationToken cancellationToken = default) | ||
{ | ||
var handler = Xamarin.Forms.Internals.Registrar.Registered.GetHandlerForObject<IImageSourceHandler>(imageSource); | ||
if (handler == null) | ||
throw new InvalidOperationException($"Unable to determine the handler for the image source ({imageSource.GetType().Name})."); | ||
|
||
using var nativeImage = await handler.LoadImageAsync(imageSource, cancellationToken).ConfigureAwait(false); | ||
if (nativeImage == null) | ||
return null; | ||
|
||
var image = nativeImage.ToSKImage(); | ||
|
||
return image; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
source/SkiaSharp.Extended.UI/Extensions/SKImageSourceExtensions.ref.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xamarin.Forms; | ||
|
||
namespace SkiaSharp.Extended.UI.Extensions | ||
{ | ||
public static partial class SKImageSourceExtensions | ||
{ | ||
private static Task<SKImage?> PlatformToSKImageAsync(ImageSource imageSource, CancellationToken cancellationToken = default) => | ||
throw new NotImplementedException(); | ||
} | ||
} |
Oops, something went wrong.