Image Converter — C# source
Convert images between PNG, JPEG and WebP entirely in your browser. Batch-friendly with quality control and a before/after size delta — nothing is uploaded.
This is the C# implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Image converter — C# port of the image-converter tool.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
static class ImageConverterTool
{
// Decode -> (JPEG only) flatten onto white -> re-encode at quality.
// WebP needs a modern codec (ImageSharp / SkiaSharp); System.Drawing
// covers the PNG + JPEG path that mirrors the web tool's core logic.
static byte[] Convert(byte[] input, string target, long quality = 85)
{
using var src = new Bitmap(new MemoryStream(input));
// JPEG has no alpha channel: paint onto an opaque white canvas,
// or transparent pixels render black in the encoded file.
Bitmap canvas;
if (target == "jpeg")
{
canvas = new Bitmap(src.Width, src.Height, PixelFormat.Format24bppRgb);
using var g = Graphics.FromImage(canvas);
g.Clear(Color.White);
g.DrawImage(src, 0, 0, src.Width, src.Height);
}
else
{
canvas = src;
}
var format = target == "jpeg" ? ImageFormat.Jpeg : ImageFormat.Png;
var codec = ImageCodecInfo.GetImageEncoders()
.First(c => c.FormatID == format.Guid);
var eps = new EncoderParameters(1);
// Quality is 0..100; the web tool's 0..1 slider multiplies by 100.
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
using var outMs = new MemoryStream();
canvas.Save(outMs, codec, eps);
if (canvas != src) canvas.Dispose();
return outMs.ToArray();
}
static void Main()
{
byte[] png = File.ReadAllBytes("input.png");
File.WriteAllBytes("output.jpg", Convert(png, "jpeg", quality: 85));
}
}
Also available in 8 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →