Pašalinti foną iš PNG naudojant C#
Sukurkite savo .NET programas, kad pašalintumėte foną iš PNG failų naudodami serverio API.
Kaip pašalinti foną iš PNG failų naudojant C#
Fono pašalinimas iš vaizdo apima priekinio plano objektų izoliavimą, o tai reikalauja objekto atpažinimo. Yra keletas būdų, kaip identifikuoti objektus nuotraukoje PNG formatu. Paprastiems vaizdams su vienodos spalvos fonu pakanka automatinio metodo. Tačiau nuotraukoms su keliomis arba iš dalies sujungtomis figūromis reikia iš anksto pažymėti objektus. Tam reikia nurodyti stačiakampius regionus ir objektų tipus, kuriuos reikia pašalinti. Sudėtingais atvejais debesies API įgalina automatinį objektų aptikimą. „Cloud API“ teikia debesies programą, galinčią atpažinti objektus nuotraukose ir panaudoti susidariusius kontūrus fono pašalinimui. Po pašalinimo figūrų palikti kraštai gali būti išlyginti, kad būtų pagerinta vaizdo kokybė. Norėdami pašalinti foną iš PNG failų, naudosime Aspose.Imaging for .NET API, kuri yra daug funkcijų, galinga ir lengvai naudojama vaizdo apdorojimo ir konvertavimo API, skirta C# platformai. Atviras NuGet paketų tvarkyklė, ieškokite Aspose.Imaging ir įdiegti. Taip pat galite naudoti šią komandą iš paketų tvarkyklės konsolės.
Paketų tvarkytuvės konsolės komanda
PM> Install-Package Aspose.Imaging
Veiksmai, kaip pašalinti foną iš PNG naudojant C#
Jums reikia aspose.imaging.dll norėdami išbandyti šią darbo eigą savo aplinkoje.
- Įkelkite PNG failus naudodami Image.Load metodą
- Pašalinti foną;
- Išsaugokite vaizdą į diską palaikomu Aspose.Imaging formatu
Sistemos reikalavimai
„Aspose.Imaging“, skirta .NET, palaikoma visose pagrindinėse operacinėse sistemose. Tiesiog įsitikinkite, kad turite šias būtinas sąlygas.
– Microsoft Windows arba suderinama OS su .NET Framework, .NET Core, Windows Application, ASP.NET Web Application.
- Kūrimo aplinka, tokia kaip Microsoft Visual Studio. – Aspose.Imaging, skirtas .NET, nurodytas jūsų projekte.
Pašalinkite foną iš PNG vaizdų – .NET
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Masking; | |
using Aspose.Imaging.Masking.Options; | |
using Aspose.Imaging.Masking.Result; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads"; | |
RemoveBackgroundGenericExample(); | |
void RemoveBackgroundProcessingWithManualRectangles() | |
{ | |
List<string> rasterFormats = new List<string>() { "jpg", "png", "bmp", "apng", "dicom", | |
"jp2", "j2k", "tga", "webp", "tif", "gif","ico" }; | |
List<string> vectorFormats = new List<string>() { "svg", "otg", "odg", "wmf", "emf", "wmz", "emz", "cmx", "cdr" }; | |
List<string> allFormats = new List<string>(rasterFormats); | |
allFormats.AddRange(vectorFormats); | |
allFormats.ForEach( | |
formatExt => | |
{ | |
var inputFile = Path.Combine(templatesFolder, $"couple.{formatExt}"); | |
bool isVectorFormat = vectorFormats.IndexOf(formatExt) > -1; | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = RasterizeVectorImage(formatExt, inputFile); | |
} | |
var outputFile = Path.Combine(templatesFolder, $"remove_background_manual_rectangles.{formatExt}"); | |
Console.WriteLine($"Processing {formatExt}"); | |
using (var image = (RasterImage)Image.Load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/net/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
var maskingOptions = new AutoMaskingGraphCutOptions | |
{ | |
FeatheringRadius = 2, | |
Method = SegmentationMethod.GraphCut, | |
Args = new AutoMaskingArgs() | |
{ | |
ObjectsRectangles = new Aspose.Imaging.Rectangle[] | |
{ | |
// girl's bound box | |
new Aspose.Imaging.Rectangle(87, 47, 123, 308), | |
// boy's bound box | |
new Aspose.Imaging.Rectangle(180, 24, 126, 224) | |
} | |
}, | |
ExportOptions = new PngOptions | |
{ | |
ColorType = PngColorType.TruecolorWithAlpha, | |
Source = new FileCreateSource(outputFile, false) | |
} | |
}; | |
using (var maskingSession = new ImageMasking(image).CreateSession(maskingOptions)) | |
{ | |
// first run of segmentation | |
using (maskingSession.Decompose()) { } | |
var argsWithUserMarkers = new AutoMaskingArgs() | |
{ | |
ObjectsPoints = new Point[][] | |
{ | |
// background markers | |
null, | |
// foreground markers | |
new UserMarker() | |
// boy's head | |
.AddPoint(218, 48, 10) | |
// girl's head | |
.AddPoint(399, 66, 10) | |
// girs's body | |
.AddPoint(158, 141, 10) | |
.AddPoint(158, 209, 20) | |
.AddPoint(115, 225, 5) | |
.GetPoints() | |
} | |
}; | |
using (var maskingResult = maskingSession.ImproveDecomposition(argsWithUserMarkers)) | |
{ | |
using (var resultImage = maskingResult[1].GetImage()) | |
{ | |
resultImage.Save(); | |
} | |
} | |
} | |
} | |
File.Delete(outputFile); | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
File.Delete(inputFile); | |
} | |
} | |
); | |
} | |
void RemoveBackgroundAutoProcessingWithAssumedObjects() | |
{ | |
List<string> rasterFormats = new List<string>() { "jpg", "png", "bmp", "apng", "dicom", | |
"jp2", "j2k", "tga", "webp", "tif", "gif" }; | |
List<string> vectorFormats = new List<string>() { "svg", "otg", "odg", "eps", "wmf", "emf", "wmz", "emz", "cmx", "cdr" }; | |
List<string> allFormats = new List<string>(rasterFormats); | |
allFormats.AddRange(vectorFormats); | |
allFormats.ForEach( | |
formatExt => | |
{ | |
var inputFile = Path.Combine(templatesFolder, $"couple.{formatExt}"); | |
bool isVectorFormat = vectorFormats.IndexOf(formatExt) > -1; | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = RasterizeVectorImage(formatExt, inputFile); | |
} | |
var outputFile = Path.Combine(templatesFolder, $"remove_background_auto_assumed_objects.{formatExt}"); | |
Console.WriteLine($"Processing {formatExt}"); | |
using (var image = (RasterImage)Image.Load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/net/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
var maskingOptions = new AutoMaskingGraphCutOptions | |
{ | |
AssumedObjects = new List<AssumedObjectData> | |
{ | |
// girl's bound box | |
new AssumedObjectData(DetectedObjectType.Human, new Aspose.Imaging.Rectangle(87, 47, 123, 308)), | |
// boy's bound box | |
new AssumedObjectData(DetectedObjectType.Human, new Aspose.Imaging.Rectangle(180, 24, 126, 224)), | |
}, | |
CalculateDefaultStrokes = true, | |
FeatheringRadius = 1, | |
Method = SegmentationMethod.GraphCut, | |
ExportOptions = new PngOptions | |
{ | |
ColorType = PngColorType.TruecolorWithAlpha, | |
Source = new FileCreateSource(outputFile, false) | |
}, | |
BackgroundReplacementColor = Color.Green | |
}; | |
using (var maskingResult = new ImageMasking(image).Decompose(maskingOptions)) | |
{ | |
using (var resultImage = maskingResult[1].GetImage()) | |
{ | |
resultImage.Save(); | |
} | |
} | |
} | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
File.Delete(inputFile); | |
} | |
File.Delete(outputFile); | |
} | |
); | |
} | |
void RemoveBackgroundAutoProcessing() | |
{ | |
List<string> rasterFormats = new List<string>() { "jpg", "png", "bmp", "apng", "dicom", | |
"jp2", "j2k", "tga", "webp", "tif", "gif" }; | |
List<string> vectorFormats = new List<string>() { "svg", "otg", "odg", "eps", "wmf", "emf", "wmz", "emz", "cmx", "cdr" }; | |
List<string> allFormats = new List<string>(rasterFormats); | |
allFormats.AddRange(vectorFormats); | |
allFormats.ForEach( | |
formatExt => | |
{ | |
var inputFile = Path.Combine(templatesFolder, $"couple.{formatExt}"); | |
bool isVectorFormat = vectorFormats.IndexOf(formatExt) > -1; | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = RasterizeVectorImage(formatExt, inputFile); | |
} | |
var outputFile = Path.Combine(templatesFolder, $"remove_background_auto.{formatExt}"); | |
Console.WriteLine($"Processing {formatExt}"); | |
using (var image = (RasterImage)Image.Load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/net/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
var maskingOptions = new AutoMaskingGraphCutOptions | |
{ | |
FeatheringRadius = 1, | |
Method = SegmentationMethod.GraphCut, | |
ExportOptions = new PngOptions | |
{ | |
ColorType = PngColorType.TruecolorWithAlpha, | |
Source = new FileCreateSource(outputFile, false) | |
}, | |
BackgroundReplacementColor = Color.Green | |
}; | |
using (var maskingResult = new ImageMasking(image).Decompose(maskingOptions)) | |
{ | |
using (var resultImage = maskingResult[1].GetImage()) | |
{ | |
resultImage.Save(); | |
} | |
} | |
} | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
File.Delete(inputFile); | |
} | |
File.Delete(outputFile); | |
} | |
); | |
} | |
void RemoveBackgroundGenericExample() | |
{ | |
List<string> rasterFormats = new List<string>() { "jpg", "png", "bmp", "apng", "dicom", | |
"jp2", "j2k", "tga", "webp", "tif", "gif" }; | |
List<string> vectorFormats = new List<string>() { "svg", "otg", "odg", "wmf", "emf", "wmz", "emz", "cmx", "cdr" }; | |
List<string> allFormats = new List<string>(rasterFormats); | |
allFormats.AddRange(vectorFormats); | |
allFormats.ForEach( | |
formatExt => | |
{ | |
var inputFile = Path.Combine(templatesFolder, $"couple.{formatExt}"); | |
bool isVectorFormat = vectorFormats.IndexOf(formatExt) > -1; | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = RasterizeVectorImage(formatExt, inputFile); | |
} | |
var outputFile = Path.Combine(templatesFolder, $"remove_background.{formatExt}"); | |
Console.WriteLine($"Processing {formatExt}"); | |
using (var image = (RasterImage)Image.Load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/net/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
var maskingOptions = new AutoMaskingGraphCutOptions | |
{ | |
CalculateDefaultStrokes = true, | |
FeatheringRadius = 1, | |
Method = SegmentationMethod.GraphCut, | |
ExportOptions = new PngOptions | |
{ | |
ColorType = PngColorType.TruecolorWithAlpha, | |
Source = new FileCreateSource(outputFile, false) | |
}, | |
BackgroundReplacementColor = Color.Green | |
}; | |
using (var maskingResult = new ImageMasking(image).Decompose(maskingOptions)) | |
{ | |
using (var resultImage = maskingResult[1].GetImage()) | |
{ | |
resultImage.Save(); | |
} | |
} | |
} | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
File.Delete(inputFile); | |
} | |
File.Delete(outputFile); | |
} | |
); | |
} | |
string RasterizeVectorImage(string formatExt, string inputFile) | |
{ | |
string outputFile = Path.Combine(templatesFolder, $"rasterized.{formatExt}.png"); | |
using (var image = Image.Load(inputFile)) | |
{ | |
image.Save(outputFile, new PngOptions()); | |
} | |
return outputFile; | |
} | |
class UserMarker | |
{ | |
private readonly List<Point> _list = new List<Point>(); | |
public UserMarker AddPoint(int left, int top, int radius) | |
{ | |
for (var y = top - radius; y <= top + radius; y++) | |
{ | |
for (var x = left - radius; x <= left + radius; x++) | |
{ | |
this._list.Add(new Point(x, y)); | |
} | |
} | |
return this; | |
} | |
public Point[] GetPoints() | |
{ | |
return this._list.ToArray(); | |
} | |
} |
Apie „Aspose.Imaging“, skirta .NET API
Aspose.Imaging API yra vaizdo apdorojimo sprendimas, skirtas kurti, modifikuoti, piešti ar konvertuoti vaizdus (nuotraukas) programose. Ji siūlo: kelių platformų vaizdo apdorojimą, įskaitant, bet tuo neapsiribojant, konvertavimą tarp įvairių vaizdo formatų (įskaitant vienodą kelių puslapių arba kelių kadrų vaizdo apdorojimą), modifikacijas, tokias kaip piešimas, darbas su grafiniais primityvais, transformacijos (keisti dydį, apkarpyti, apversti ir pasukti). , dvejetainis, pilkos spalvos tonas, koregavimas), pažangios vaizdo apdorojimo funkcijos (filtravimas, keitimas, maskavimas, iškrypimas) ir atminties optimizavimo strategijos. Tai yra atskira biblioteka ir nepriklauso nuo jokios programinės įrangos vaizdo operacijoms. Projektuose galima lengvai pridėti didelio našumo vaizdo konvertavimo funkcijų naudojant vietines API. Tai yra 100 % privačios vietinės API, o vaizdai apdorojami jūsų serveriuose.Pašalinkite PNG foną naudodami internetinę programą
Pašalinkite PNG dokumentų foną apsilankę [tiesioginės demonstracinės versijos svetainėje] ( https://products.aspose.app/imaging/remove-background) . Tiesioginė demonstracinė versija turi šiuos privalumus
PNG Kas yra PNG Failo formatas
PNG, Portable Network Graphics, reiškia rastrinio vaizdo failo formato tipą, kuriame naudojamas nepralaidus glaudinimas. Šis failo formatas buvo sukurtas kaip grafinio mainų formato (GIF) pakaitalas ir neturi jokių autorių teisių apribojimų. Tačiau PNG failo formatas nepalaiko animacijų. PNG failo formatas palaiko nenutrūkstamą vaizdo glaudinimą, todėl jis yra populiarus tarp vartotojų. Laikui bėgant, PNG tapo vienu iš dažniausiai naudojamų vaizdo failų formatų. Beveik visos operacinės sistemos palaiko PNG failų atidarymą. Pavyzdžiui, „Microsoft Windows Viewer“ turi galimybę atidaryti PNG failus, nes OS pagal numatytuosius nustatymus palaiko diegimo dalį.
Skaityti daugiauKiti palaikomi Pašalinkite fono formatus
Naudojant C#, galima lengvai pašalinti foną iš įvairių formatų, įskaitant.