Good morning class, (see what I did there:P ?) Today we will discuss a very important subject: creating pixel art in Minecraft from a given image.

Ok guys, so I saw people creating pixel art in Minecraft (trying to render images in the game using blocks) and I really liked the idea. Unfortunately I’m not good at drawing but I’m a coder and this will suffice, for now - hopefully this will prove its usefulness for those of you who want to “decorate” their servers.

The program's idea is quite simple: it scans the image pixel by pixel and for each pixel places a block with a similar color in the game, by sending a command (/setblock X Y Z <block name>) to the server’s InputStream (no networking stuff, no delay, no headache).

## Results

This is what I managed to obtain with this program using a palette of 16 colors. The coloring is not very accurate since I can’t produce all the colors in the photos with only 16 types of blocks - however this can be fixed by using more types of blocks.

## 1. Starting the server

In order to send commands to your server’s InputStream you need to tweak a few settings at process level. This means you’ll have to start the server from this program, using the Process class. Our goal is to make this process read inputs from a custom stream (Process.StandardInput) - this stream will be used by our console application to send the set of /setblock commands.

After the input stream was changed, this method can be used to supply information to the process:

If you want to display, in your console, any messages that the server sends, you’ll need 2 event handlers (1 for the outputstream and 1 for the errorstream). I wrote them as anonymous functions to keep this sourcecode as short as possible:

After this, you’ll need to call these 2 functions:

Basically you’ll start cmd.exe from the C# application and from there launch the server by loading the jar file with a line like this:
java -Xmx1024M -Xms1024M -jar minecraft_server.1.8.8.jar nogui

What you need to be careful about is that once you start the server, your only way to communicate with it is the console - if you close the console, the server will keep running in the background (java.exe) and you’ll have to kill the process in order to stop it.

To avoid this make sure you send the command: stop before the console closes.

Until now we have something that looks like a primitive wrapper:

## 2. Choosing the right block

Ok, that was the easy part, believe it or not. We’ll now have to create a list (it’s actually a Dictionary) that contains the name of the available blocks and their colors.

The next step is to realize that these images contain more than 16 colors so we need a function that takes a pixel’s color and tells us which color (block) from the list above is the best match.

Our function is based on a method called k nearest neighbor: which compares 2 colors and returns an error. The lower the error is the closer the block’s color is to the original. In our case the formula looks like the euclidean distance between 2 points in a 3d space - but instead of coordinates (X, Y, Z) we use (R, G, B) and the points are actually the colors.

For example:

if we have a pixel of this color: P(R:100 G:100 B:100) and we have only 2 available blocks: BlackBlock(R:0 G:0 B:0) and WhiteBlock(R:255 G:255 B:255) we compute the errors for each one

• Comparing the pixel’s color to the BlackBlock:
$error = \sqrt{(100-0)^2 + (100-0)^2 + (100-0)^2)} = 173.2$
• Now comparing it to the WhiteBlock:
$error = \sqrt{(100-255)^2 + (100-255)^2 + (100-255)^2} = 268.46$

This means the BlackBlock is a better choice as it matches the pixel’s color better.

The function will look like this (it returns the index of the block):

## 3. Rendering the image in game

The last step is constructing the image using blocks; take each pixel of the image, get its color using GetPixel(x, y) and run it through the function above to obtain the block that matches the color.

Make sure you include this reference: System.Drowing.dll.

Once you have the index of the block, you’ll have to construct a /setblock command and send it to the server.

small note: you can use LockBits() and UnlockBits() for efficiency reasons but the server will still limit your speed. In my opinion, it’s not worth it here.

## 4. Complete Sourcecode

This is the complete sourcecode - it was made for fun, so don’t expect it to be really optimized.

Feel free to use / modify it and if you find it interesting or useful, consider sharing this page or dropping a backlink - it helps a lot xD