Skip to content

Create a "simple" picture view #255

Closed

Description

@mattleibow

This view should be able to just have a setter:

var view = new SKImageView();
view.Bitmap = SKBitmap.Decode(myImagePath);

Also, another option would be to create extension methods to convert images to platform images:

// bitmaps
SKBitmap bitmap = ...;
var uiImage = bitmap.ToUIImage();

// pictures
SKPicture picture = ...;
var cgImage = picture.ToCGImage(new SKSizeI(100, 100));

Activity

  1. added this to the 1.56.2 milestone on Feb 28, 2017
  2. mattleibow commented on Feb 28, 2017

    @mattleibow
    ContributorAuthor

    I decided against creating a custom image view just for SkiaSharp for several reasons:

    • re-doing the work of every platform out there
    • too many features, for too many platforms
    • existing infrastructure/extensions (loaders/effects) won't work

    However... I have provided a way to convert from SkiaSharp types (SkBitmap, SKPicture, SKImage and SKPixmap) into platform types (CGImage, CIImage, UIImage, NSImage, System.Drawing.Bitmap, Android.Graphics.Bitmap, WriteableBitmap).

    These are basic extension methods that take a copy of the pixels and hand it over to the OS. Then, these can be used in any UI picture view on the platform - and any custom ones that are in use:

    myNativeImageView.Image = viewModel.AwesomeImage.ToUIImage();

    I have also created a set of ImageSource types for Xamarin.Forms (SKBitmapImageSource, SKImageImageSource, SKPictureImageSource and SKPixmapImageSource). These are used as "normal" in XAML:

    <Image WidthRequest="100" HeightRequest="100">
        <Image.Source>
            <skiasharp:SKImageImageSource Image="{Binding AwesomeImage}" />
        </Image.Source>
    </Image>

    Or in C#

    var image = new Image {
        Source = new SKImageImageSource {
            Image = viewModel.AwesomeImage
        }
    }
    
    // or with the nice implicit cast
    var image = new Image {
        Source = (SKImageImageSource) viewModel.AwesomeImage
    }

    I am closing this now as I think I have something that will help everyone :) But, feel free to re-open if you need/prefer something else:

    • you really need a custom image view...
    • you want some extension method that does NOT copy the pixels (you will have to manage the lifetime of the SkiaSharp instance) - this is also not always possible on some of the platforms
    • another idea based on displaying a SkiaSharp image in some native way
  3. nathanielcook commented on Jul 22, 2017

    @nathanielcook

    I see here and also here that there is a way to use SkiaSharp from XAML (Xamarin Forms), but what about AXML (native Android) layouts?

  4. mattleibow commented on Jul 22, 2017

    @mattleibow
    ContributorAuthor

    You can use:

    <skiasharp.views.android.SKCanvasView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/canvasView" />

    https://github.com/mattleibow/SkiaSharpDemo/blob/master/SkiaSharpNativeDemo/SkiaSharpNativeDemo.Android/Resources/layout/main.axml

  5. ghost locked as resolved and limited conversation to collaborators on Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions