Introduction of Image Edit Plugin for Xamarin
Tags: xamarinforms, xamarin
Published: 2018-04-02 02:46:25

---
title: Introduction of Image Edit Plugin for Xamarin
published: true
description: 
tags: Xamarin.Forms,Xamarin
---

I needed the processing in my app that an image was cropped, resized, rotated and saved. So I implemented it using DependencyService.

After that, I tried extracting the processing to use as a library and I thought this should be made as a Plugin.

That's why I made the Plugin which edits an image. This plugin is available on NuGet.

## Source

[https://github.com/muak/Xamarin.Plugin.ImageEdit](https://github.com/muak/Xamarin.Plugin.ImageEdit)

## Nuget

[https://www.nuget.org/packages/Xamarin.Plugin.ImageEdit/](https://www.nuget.org/packages/Xamarin.Plugin.ImageEdit/)

## Installation

```bash
Install-Package Xamarin.Plugin.ImageEdit
```

Install into your PCL / .NETStandard project and each platform projects.

## Supported

|Platform|Supported|Version|
| ------------------- | :-----------: | :------------------: |
|Xamarin.iOS|Yes|iOS 9+|
|Xamarin.Android|Yes|API 22+|
|Windows 10 UWP|No||
|Xamarin.Mac|No|| 

## Features

* Resizing
* Cropping
* Rotation
* Turning monochrome
* Convert to png/jpeg bytes array
* Getting ARGB pixels information

{% youtube u__sdgHhinM %}


## How to use

An image can be cropped, rotated, resized and converted PNG data in the following code:

```cs
using (var image = await CrossImageEdit.Current.CreateImageAsync(imageByteArray)) {
	var croped = await Task.Run(() =>
			image.Crop(10, 20, 250, 100)
				 .Rotate(180)
				 .Resize(100, 0)
				 .ToPng()
	);
}
```

### With DI container (eg. prism.forms)

In case using as plugin, it is difficult to do unit test.
Considering unit test, you should use DI container such as unity on prism.forms as the following code:

```cs
//View model constructor
public ViewModel(IImageEdit imageEdit){

	using (var image = await imageEdit.CreateImageAsync(imageByteArray)) {
		var croped = await Task.Run(() =>
				image.Crop(10, 20, 250, 100)
					 .Rotate(180)
					 .Resize(100, 0)
					 .ToPng()
		);
	}
}

//on platform
public class iOSInitializer : IPlatformInitializer
{
	public void RegisterTypes(IUnityContainer container)
	{
		container.RegisterType<IImageEdit,ImageEdit>();
	}
}
```

## API Usage

### IImageEdit.CreateImageAsync

This method is used to get EditableImage object.

```cs
//from byte[]
var editableImage = await CrossImageEdit.Current.CreateImageAsync(imageByteArray);

//from stream
var editableImage = await CrossImageEdit.Current.CreateImageAsync(imageStream);
```

You can manipulate an image using a got EditableImage.

### IEditableImage.Resize

This method resizes an image and returns IEditableImage.

```cs
var width = 200;
var height = 150;
editableImage.Resize(width, height);
editableImage.Resize(width, 0); //auto height
editableImage.Resize(0, height); //auto width

editableImage.Resize(50); //specify max length of long side. other side auto size.
```
Specifying either width or height to 0, the other side will automatically be adjusted following the aspect ratio.

Passing only one argument, the long side length will become the value length and the other side will automatically be adjusted following the aspect ratio. 

### IEditableImage.Crop

This method crop an image and returns IEditableImage.

```cs
var x = 10;
var y = 10;
var width = 50;
var height = 50;
editableImage.Crop(x, y, width, height);
```

### IEditableImage.Rotate

This method rotate an image and returns IEditableImage.

```cs
var degree = 90; // 0-360;
editableImage.Rotate(degree);
```

The rotation angle is specified from 0 to 360.

### IEditableImage.ToMonochrome

This method turns an image color into monochrome and returns IEditableImage.

```cs
editableImage.ToMonochrome();
```

### IEditableImage.ToPng

This method converts an image to PNG and returns PNG bytes array.

```cs
var pngBytes = editableImage.ToPng();
```

### IEditableImage.ToJpeg

This method converts an image to JPEG and returns JPEG bytes array.

```cs
var jpgBytes = editableImage.ToJpeg(90); // quality(0-100)
```

An image quality is specified from 0 to 100.

### IEditableImage.ToArgbPixels

This method returns the image pixels array which has ARGB information.

For example, when an element of this array has 0xFF00F090 value, the state is the following.

|A|R|G|B|
| :--- | :--- | :--- | :--- |
|FF|00|F0|90|

And in order to get an individual color from this value, like the following code is used.

```cs
var pixels = editableImage.ToArgbPixels();

var pixel = pixels[10];
var r = pixel & 0x00FF0000 >> 16; //Get R
var g = pixel & 0x0000FF00 >> 8;  //Get G
var b = pixel & 0x000000FF;       //Get B
```

### IEditableImage.GetNativeImage

This method returns each platform native image. If the platform is iOS, returns UIImage; Otherwise, returns Bitmap.

## Concluding

I would be grateful if you could use this library.

If there are some questions, requests, and bug reports, please report it to GitHub issues or twitter (@muak_x).

Thank you.