Skip to content

Instantly share code, notes, and snippets.

@AldeRoberge
Created March 6, 2024 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AldeRoberge/8e335d92521f654a3d4de2b2344924c7 to your computer and use it in GitHub Desktop.
Save AldeRoberge/8e335d92521f654a3d4de2b2344924c7 to your computer and use it in GitHub Desktop.
Converts a sprite (16x16 pixels) into a RotMG-like sprite (with outline and shadow)
using System;
using System.Diagnostics;
using SkiaSharp;
namespace AGES.Utils.Runtime.SpriteImageProcessor
{
public class SpriteImageProcessor
{
public SpriteImageProcessor()
{
// Load the image
using var bitmap = SKBitmap.Decode("Resources/input.png");
// Upscale the image
var scaledBitmap = bitmap.Resize(new SKImageInfo(bitmap.Width * 4, bitmap.Height * 4), SKFilterQuality.None);
// Create a new bitmap to add border
var bitmapWithBorder = new SKBitmap(scaledBitmap.Width + 2, scaledBitmap.Height + 2);
using var canvas = new SKCanvas(bitmapWithBorder);
// Draw the image 8 times to create border
var paint = new SKPaint();
paint.Color = new SKColor(0, 0, 0);
for (int dx = -1; dx <= 1; dx++)
{
for (int dy = -1; dy <= 1; dy++)
{
if (dx != 0 || dy != 0)
{
canvas.DrawBitmap(scaledBitmap, dx + 1, dy + 1, paint);
}
}
}
// Replace all non transparent pixels with black
for (int x = 0; x < bitmapWithBorder.Width; x++)
{
for (int y = 0; y < bitmapWithBorder.Height; y++)
{
var pixel = bitmapWithBorder.GetPixel(x, y);
if (pixel.Alpha != 0)
{
bitmapWithBorder.SetPixel(x, y, new SKColor(0, 0, 0));
}
}
}
// Draw the main image over the top
canvas.DrawBitmap(scaledBitmap, 1, 1, paint);
// Create bitmap for shadow
var shadowBitmap = new SKBitmap(bitmapWithBorder.Width, bitmapWithBorder.Height);
// Create black paint
var shadowPaint = new SKPaint
{
Color = new SKColor(0, 0, 0),
ImageFilter = SKImageFilter.CreateDropShadow(0, 0, 3, 3, SKColors.Black)
};
using var shadowCanvas = new SKCanvas(shadowBitmap);
// Draw shadow
shadowCanvas.DrawBitmap(bitmapWithBorder, 0, 0, shadowPaint);
// Draw the main bitmap over the top
shadowCanvas.DrawBitmap(bitmapWithBorder, 0, 0, paint);
// Save the image to disk
using var image = SKImage.FromBitmap(shadowBitmap);
using var data = image.Encode();
System.IO.File.WriteAllBytes("output.png", data.ToArray());
// Open image in explorer
Process.Start("explorer", "/select,\"" + AppDomain.CurrentDomain.BaseDirectory + "output.png\"");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment