Home > Uncategorized > Remove the background from an Image using C#

Remove the background from an Image using C#

Instead of manually processing images, and cutting out the subject from the background, you can use an API to do this, as long as the image is publicly accessible via a URL – Even temporarily accessible, you could upload to S3, then delete it again afterwards.

Here’s the code – in C#


var url = "https://image-background-removal3.p.rapidapi.com/BackgroundRemoverLambda";

var oReq = new { url = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ2nkYh11Mswy-IVb5tM3C4c_nlbYOvNyso9w&usqp=CAU" };

var strReq = JsonConvert.SerializeObject(oReq);

using var web = new WebClient();
web.Headers.Add("x-rapidapi-key", "xxxxxx");
var response = web.UploadData(url, "POST", Encoding.UTF8.GetBytes(strReq));
var b64Response = Convert.FromBase64String(Encoding.UTF8.GetString(response));
File.WriteAllBytes("result3.png", b64Response);

Where the API Key can be found here: https://rapidapi.com/appaio/api/image-background-removal3/

Advertisement
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: