Skip to content

Instantly share code, notes, and snippets.

@svejdo1
Created January 20, 2017 12:07
Show Gist options
  • Save svejdo1/5d2f5edf7d89ec015ec5c34bde12c673 to your computer and use it in GitHub Desktop.
Save svejdo1/5d2f5edf7d89ec015ec5c34bde12c673 to your computer and use it in GitHub Desktop.
Simple utility that takes image (where width equals to height) and make isometric tile out of it.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace Barbar.IsoTexture
{
class Program {
static int Main(string[] args) {
if (args == null || args.Length != 2 || string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1])) {
Console.Out.WriteLine("Usage:");
Console.Out.WriteLine("isotexture [input.(gif|png|jpg)] [output]");
return -1;
}
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string input, output;
if (Path.IsPathRooted(args[0])) {
input = args[0];
} else {
input = Path.Combine(basePath, args[0]);
}
if (Path.IsPathRooted(args[1])) {
output = args[1] + ".png";
} else {
output = Path.Combine(basePath, args[1] + ".png");
}
if (!File.Exists(input)) {
Console.Error.WriteLine("Input file does not exists.");
return -1;
}
if (File.Exists(output)) {
Console.Out.WriteLine("Output file exists, overwrite ? [Y/N]");
do {
string response = Console.In.ReadLine();
if (response.StartsWith("Y", StringComparison.OrdinalIgnoreCase)) {
break;
}
if (response.StartsWith("N", StringComparison.OrdinalIgnoreCase)) {
return -1;
}
} while (true);
}
Bitmap inputImage = new Bitmap(input);
if (inputImage.Width != inputImage.Height) {
Console.Error.WriteLine("Image width must match image height.");
return -1;
}
var outputImage = CreateRotatedTexture(inputImage, 18);
outputImage.Save(output, ImageFormat.Png);
return 0;
}
private static Bitmap CreateRotatedTexture(Bitmap inputImage, int slices) {
if (inputImage == null)
throw new ArgumentNullException("inputImage");
if (inputImage.Width != inputImage.Height)
throw new ArgumentException("Width and height of image must match.", "inputImage");
double xscale = Math.Pow(3, 0.5);
int size = inputImage.Width;
var outputImage = new Bitmap((int)(xscale * size), size * slices, PixelFormat.Format32bppPArgb);
using (Graphics graphics = Graphics.FromImage(outputImage)) {
graphics.FillRectangle(new SolidBrush(Color.Transparent), new Rectangle(0, 0, outputImage.Width, outputImage.Height));
}
for (var i = 0; i < slices; i++) {
using (Graphics graphics = Graphics.FromImage(outputImage)) {
Matrix matrix = new Matrix();
matrix.Translate(0, i * size);
matrix.Translate((float)(xscale * 0.5 * (double)size), size / 2);
matrix.Scale((float)Math.Pow(1.5, 0.5), (float)Math.Pow(2, -0.5));
matrix.Rotate(-45 + (360 / slices) * i);
matrix.Translate(-size / 2, -size / 2);
graphics.Transform = matrix;
graphics.DrawImage(inputImage, new Point(0, 0));
graphics.Flush();
}
}
return outputImage;
}
private static Bitmap CreateIsoTexture(Bitmap inputImage) {
if (inputImage == null)
throw new ArgumentNullException("inputImage");
if (inputImage.Width != inputImage.Height)
throw new ArgumentException("Width and height of image must match.", "inputImage");
double xscale = Math.Pow(3, 0.5);
int size = inputImage.Width;
Bitmap outputImage = new Bitmap((int)(xscale * size), size, PixelFormat.Format32bppPArgb);
Matrix matrix = new Matrix();
matrix.Translate((float)(xscale * 0.5 * (double)size), size / 2);
matrix.Scale((float)Math.Pow(1.5, 0.5), (float)Math.Pow(2, -0.5));
matrix.Rotate(-45);
matrix.Translate(-size / 2, -size / 2);
using (Graphics graphics = Graphics.FromImage(outputImage)) {
graphics.FillRectangle(new SolidBrush(Color.Transparent), new Rectangle(0, 0, outputImage.Width, outputImage.Height));
graphics.Transform = matrix;
var attributes = new ImageAttributes();
//attributes.SetColorKey(inputImage.GetPixel(0, 0), inputImage.GetPixel(0, 0));
graphics.DrawImage(inputImage, new Point(0,0));
}
return outputImage;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment