PPTX DOCX XLSX PDF ODP
Aspose.Imaging  สำหรับ .NET
GIF

เปลี่ยนพื้นหลังใน GIF ผ่าน C#

สร้างแอป .NET ของคุณเองเพื่อเปลี่ยนพื้นหลังในไฟล์ GIF โดยใช้ API ฝั่งเซิร์ฟเวอร์

วิธีเปลี่ยนพื้นหลังในไฟล์ GIF โดยใช้ C#

บ่อยครั้ง การได้ภาพในอุดมคติจำเป็นต้องเปลี่ยนพื้นหลัง เพื่อให้ได้เอฟเฟ็กต์ภาพตามรูปแบบ GIF ที่ต้องการ วัตถุเบื้องหน้าจะต้องแยกออกจากส่วนที่เหลือของภาพ สามารถตรวจจับวัตถุอัตโนมัติได้หากพื้นหลังมีความสม่ำเสมอ หากพื้นหลังของภาพถ่ายไม่สม่ำเสมอหรือการแยกวัตถุทำได้ยาก แนะนำให้ทำเครื่องหมายรูปภาพไว้ล่วงหน้า ซึ่งเกี่ยวข้องกับการระบุบริเวณสี่เหลี่ยมภายในภาพถ่ายซึ่งมีวัตถุที่ต้องการอยู่และระบุประเภทของวัตถุเหล่านั้น ซึ่งสามารถทำได้ด้วยตนเองหรือโดยอัตโนมัติผ่านฟีเจอร์การจดจำวัตถุของ Cloud API หลังจากเลือกวัตถุและลบพื้นหลังดั้งเดิมแล้ว คุณสามารถใช้พื้นหลังใหม่หรือนำความโปร่งใสมาใช้ได้ เราจะใช้เพื่อเปลี่ยนพื้นหลังในไฟล์ GIF Aspose.Imaging for .NET API ซึ่งเป็น API การจัดการและการแปลงรูปภาพที่มีคุณลักษณะหลากหลาย มีประสิทธิภาพ และใช้งานง่ายสำหรับแพลตฟอร์ม C# เปิด NuGet ตัวจัดการแพ็คเกจ ค้นหา ** Aspose.Imaging ** และติดตั้ง คุณสามารถใช้คำสั่งต่อไปนี้จาก Package Manager Console

Package Manager Console Command


PM> Install-Package Aspose.Imaging

ขั้นตอนในการเปลี่ยนพื้นหลังใน GIFs ผ่าน C#

คุณต้องใช้ aspose.imaging.dll เพื่อลองใช้เวิร์กโฟลว์ต่อไปนี้ในสภาพแวดล้อมของคุณเอง

  • โหลดไฟล์ GIF ด้วยวิธี Image.Load
  • เปลี่ยนพื้นหลัง;
  • บันทึกภาพลงแผ่นดิสก์ในรูปแบบที่รองรับโดย Aspose.Imaging

ความต้องการของระบบ

Aspose.Imaging สำหรับ .NET ได้รับการสนับสนุนในระบบปฏิบัติการหลักทั้งหมด เพียงตรวจสอบให้แน่ใจว่าคุณมีข้อกำหนดเบื้องต้นดังต่อไปนี้

  • Microsoft Windows หรือระบบปฏิบัติการที่เข้ากันได้กับ .NET Framework, .NET Core, Windows Application, ASP.NET Web Application
  • สภาพแวดล้อมการพัฒนาเช่น Microsoft Visual Studio
  • Aspose.Imaging สำหรับ .NET ที่อ้างอิงในโครงการของคุณ
 

เปลี่ยนพื้นหลังในรูปภาพ GIF - .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();
}
}
 
  • เกี่ยวกับ Aspose.Imaging สำหรับ .NET API

    Aspose.Imaging API เป็นโซลูชันการประมวลผลรูปภาพเพื่อสร้าง แก้ไข วาดหรือแปลงรูปภาพ (ภาพถ่าย) ภายในแอปพลิเคชัน นำเสนอ: การประมวลผลภาพข้ามแพลตฟอร์ม รวมถึงแต่ไม่จำกัดเพียงการแปลงระหว่างรูปแบบภาพต่างๆ (รวมถึงการประมวลผลภาพแบบหลายหน้าหรือหลายเฟรมแบบเดียวกัน) การปรับเปลี่ยน เช่น การวาด การทำงานกับภาพกราฟิกดั้งเดิม การแปลงภาพ (ปรับขนาด ครอบตัด พลิกและหมุน , ไบนารี, ระดับสีเทา, ปรับ), คุณสมบัติการจัดการภาพขั้นสูง (การกรอง, การแยกสี, การปิดบัง, การเดสก์) และกลยุทธ์การปรับหน่วยความจำให้เหมาะสม เป็นไลบรารีแบบสแตนด์อโลนและไม่ขึ้นกับซอฟต์แวร์ใด ๆ สำหรับการทำงานของรูปภาพ คุณสามารถเพิ่มคุณสมบัติการแปลงรูปภาพประสิทธิภาพสูงด้วย API ดั้งเดิมภายในโปรเจ็กต์ได้อย่างง่ายดาย สิ่งเหล่านี้เป็น API ภายในองค์กรที่เป็นส่วนตัว 100% และอิมเมจได้รับการประมวลผลที่เซิร์ฟเวอร์ของคุณ

    เปลี่ยนพื้นหลังใน GIF ผ่านแอปออนไลน์

    เปลี่ยนพื้นหลังในเอกสาร GIF โดยไปที่ เว็บไซต์ Live Demos การสาธิตสดมีประโยชน์ดังต่อไปนี้

      ไม่จำเป็นต้องดาวน์โหลดหรือตั้งค่าอะไรเลย
      ไม่ต้องเขียนโค้ดใดๆ
      เพียงอัปโหลดไฟล์ GIF แล้วกดปุ่ม เปลี่ยนพื้นหลังทันที
      รับลิงค์ดาวน์โหลดทันทีสำหรับไฟล์ผลลัพธ์

    GIF คืออะไร GIF รูปแบบไฟล์

    รูปแบบ GIF หรือ Graphical Interchange เป็นประเภทรูปภาพที่มีการบีบอัดสูง Unisys เป็นเจ้าของ GIF ใช้อัลกอริธึมการบีบอัด LZW ที่ไม่ลดคุณภาพของภาพ สำหรับแต่ละภาพ โดยทั่วไปแล้ว GIF อนุญาตให้ใช้ได้ถึง 8 บิตต่อพิกเซล และอนุญาตให้ใช้สีได้สูงสุด 256 สีทั่วทั้งภาพ ตรงกันข้ามกับภาพ JPEG ที่สามารถแสดงสีได้มากถึง 16 ล้านสีและสัมผัสได้ถึงขีดจำกัดของสายตามนุษย์ ย้อนกลับไปเมื่ออินเทอร์เน็ตปรากฏขึ้น GIF ยังคงเป็นตัวเลือกที่ดีที่สุด เนื่องจากต้องใช้แบนด์วิดท์ต่ำและเข้ากันได้กับกราฟิกที่ใช้พื้นที่สีทึบ GIF แบบเคลื่อนไหวจะรวมรูปภาพหรือเฟรมจำนวนมากเป็นไฟล์เดียว และแสดงตามลำดับเพื่อสร้างคลิปภาพเคลื่อนไหวหรือวิดีโอสั้น ข้อจำกัดสีสูงสุด 256 สำหรับแต่ละเฟรม และมีแนวโน้มว่าจะเหมาะสมน้อยที่สุดสำหรับการสร้างภาพอื่นๆ และภาพถ่ายที่มีการไล่ระดับสี

    อ่านเพิ่มเติม

    รูปแบบพื้นหลังการเปลี่ยนแปลงอื่น ๆ ที่รองรับ

    การใช้ C# คุณสามารถเปลี่ยนพื้นหลังในรูปแบบต่างๆ ได้อย่างง่ายดายรวมถึง

    APNG (กราฟิกเครือข่ายแบบพกพาแบบเคลื่อนไหว)
    BMP (รูปภาพบิตแมป)
    ICO (ไอคอน Windows)
    JPG (กลุ่มผู้เชี่ยวชาญด้านการถ่ายภาพร่วม)
    DIB (บิตแมปอิสระของอุปกรณ์)
    DICOM (การถ่ายภาพและการสื่อสารดิจิทัล)
    DJVU (รูปแบบกราฟิก)
    DNG (ภาพกล้องดิจิตอล)
    EMF (รูปแบบไฟล์ Metafile ที่ปรับปรุงแล้ว)
    EMZ (Windows บีบอัด Metafile ที่ปรับปรุงแล้ว)
    JP2 (JPEG 2000)
    J2K (ภาพบีบอัดเวฟเล็ต)
    PNG (กราฟิกเครือข่ายแบบพกพา)
    TIFF (รูปแบบภาพที่ติดแท็ก)
    WEBP (รูปภาพเว็บแรสเตอร์)
    WMF (Microsoft Windows Metafile)
    WMZ (สกิน Windows Media Player ที่บีบอัด)
    TGA (Targa Graphic)
    SVG (กราฟิกแบบเวกเตอร์ที่ปรับขนาดได้)
    EPS (ภาษา PostScript ที่ห่อหุ้ม)
    CDR (วาดภาพเวกเตอร์)
    CMX (รูปภาพ Corel Exchange)
    OTG (มาตรฐาน OpenDocument)
    ODG (รูปแบบการวาด Apache OpenOffice)