Last active
January 12, 2022 08:27
-
-
Save atruskie/02f6fb35d6967c57ed543d109e83736e to your computer and use it in GitHub Desktop.
System.Drawing and SkiaSharp - fill blending via opacity channel for rgb (no alpha) image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using SixLabors.ImageSharp; | |
using SixLabors.ImageSharp.Drawing.Processing; | |
using SixLabors.ImageSharp.Processing; | |
using SkiaSharp; | |
using System; | |
using System.IO; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ImageSharp(); | |
CairoImage(); | |
SkiaSharp(); | |
SystemDrawing(); | |
} | |
public static void ImageSharp() | |
{ | |
Console.WriteLine("Hello World!"); | |
var image = new SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgb24>(SixLabors.ImageSharp.Configuration.Default, 100, 100, SixLabors.ImageSharp.Color.Black); | |
image.Mutate(x => x.Fill(SixLabors.ImageSharp.Color.Red.WithAlpha(0.5f), new RectangleF(10, 10, 80, 80))); | |
image.Save("rgb24demo_imagesharp.png"); | |
} | |
public static void CairoImage() | |
{ | |
var surface = new Cairo.ImageSurface(Cairo.Format.Rgb24, 100, 100); | |
using (var cr = new Cairo.Context(surface)) | |
{ | |
cr.Color = new Cairo.Color(0, 0, 0); | |
cr.Fill(); | |
cr.Color = new Cairo.Color(1, 0, 0, 0.5); | |
cr.Rectangle(10, 10, 80, 80); | |
cr.Fill(); | |
} | |
surface.WriteToPng("rgb24demo_cairo.png"); | |
} | |
public static void SkiaSharp() | |
{ | |
using var image = new SKBitmap(new SKImageInfo(100, 100, SKColorType.Rgb888x)); | |
using (var canvas = new SKCanvas(image)) | |
{ | |
canvas.Clear(SKColors.Black); | |
SKPaint redFill = new SKPaint | |
{ | |
Style = SKPaintStyle.Fill, | |
Color = SKColors.Red.WithAlpha(128), | |
}; | |
canvas.DrawRect(10, 10, 80, 80, redFill); | |
canvas.Flush(); | |
} | |
using (var save = SKImage.FromBitmap(image)) | |
using (var data = save.Encode(SKEncodedImageFormat.Png, 100)) | |
{ | |
// save the data to a stream | |
using (var stream = File.OpenWrite("rgb24demo_skia.png")) | |
{ | |
data.SaveTo(stream); | |
} | |
} | |
} | |
public static void SystemDrawing() | |
{ | |
using var sdImage = new System.Drawing.Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format24bppRgb); | |
using (var graphics = System.Drawing.Graphics.FromImage(sdImage)) | |
{ | |
graphics.Clear(System.Drawing.Color.Black); | |
graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(128, 255, 0, 0)), new System.Drawing.Rectangle(10, 10, 80, 80)); | |
} | |
using (var stream = File.OpenWrite("rgb24demo_system.drawing.png")) | |
{ | |
sdImage.Save(stream, System.Drawing.Imaging.ImageFormat.Png); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For future reference, this example was made for SixLabors/ImageSharp.Drawing#38