Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RadImageEditor with Remove BG API Tool
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:ie="clr-namespace:Telerik.XamarinForms.ImageEditor;assembly=Telerik.XamarinForms.ImageEditor"
x:Class="ImageEditorTools.Portable.MainPage"
BackgroundColor="LightGray">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ie:RadImageEditor x:Name="Editor"
Source="cat4.jpeg"
BackgroundColor="Transparent"/>
<ie:RadImageEditorToolbar ImageEditor="{x:Reference Editor}"
AutoGenerateItems="False"
Grid.Row="1">
<ie:CommandToolbarItem Text="Remove BG"
Tapped="RemoveBackground_Clicked" />
<ie:CommandToolbarItem Text="Save"
Tapped="Save_Clicked" />
<ie:UndoToolbarItem />
<ie:RedoToolbarItem />
</ie:RadImageEditorToolbar>
</Grid>
</ContentPage>
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Telerik.XamarinForms.ImageEditor;
using Xamarin.Forms;
namespace ImageEditorTools.Portable
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void Save_Clicked(object sender, EventArgs e)
{
await SaveToPicturesFolderAsync();
}
private async void RemoveBackground_Clicked(object sender, EventArgs e)
{
await RemoveBackgroundAsync();
}
private async Task RemoveBackgroundAsync()
{
var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var tempFilePath = Path.Combine(folderPath, "temp_image.jpeg");
using (var tempFileStream = File.OpenWrite(tempFilePath))
{
await this.Editor.SaveAsync(tempFileStream, ImageFormat.Jpeg, 0.9);
}
using (var formData = new MultipartFormDataContent())
{
// Get Your API Key here https://www.remove.bg/api
var apiKey = "YOUR_API_KEY_HERE";
formData.Headers.Add("X-Api-Key", apiKey);
formData.Add(new ByteArrayContent(File.ReadAllBytes(tempFilePath)), "image_file", "file.jpg");
formData.Add(new StringContent("auto"), "size");
using(var client = new HttpClient())
using (var response = await client.PostAsync("https://api.remove.bg/v1.0/removebg", formData))
{
if (response.IsSuccessStatusCode)
{
var noBgFilePath = Path.Combine(folderPath, "no-bg_image.jpg");
using (var fileStream = File.OpenWrite(noBgFilePath))
{
await response.Content.CopyToAsync(fileStream);
}
Editor.Source = new FileImageSource { File = noBgFilePath };
}
else
{
Debug.WriteLine("Error: " + response.Content.ReadAsStringAsync().Result);
}
}
}
}
private async Task SaveToPicturesFolderAsync()
{
var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
using (var fileStream = File.OpenWrite(Path.Combine(folderPath, "ImageEditor_Final.jpeg")))
{
await this.Editor.SaveAsync(fileStream, ImageFormat.Jpeg, 0.9);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment