Code:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace Sprite
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string dir = Directory.GetCurrentDirectory();
private void Form1_Load(object sender, EventArgs e)
{
PictureBox sprite = new PictureBox();
Bitmap img = new Bitmap(dir + "/lofiObj3.png");
Rectangle section = new Rectangle(new Point(0, 0), new Size(8, 8));
img = getImage(img, section, new Size(64, 64));
sprite.Image = img;
sprite.SizeMode = PictureBoxSizeMode.AutoSize;
sprite.Top += 50;
sprite.Left += 50;
Controls.Add(sprite);
}
public Bitmap getImage(Bitmap source, Rectangle section, Size size)
{
// An empty bitmap which will hold the cropped image
Bitmap cropped = new Bitmap(section.Width, section.Height);
Graphics crop = Graphics.FromImage(cropped);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
crop.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
// An empty bitmap which will hold the resized image
Bitmap resized = new Bitmap(size.Width, size.Height);
Graphics resize = Graphics.FromImage(resized);
// Resize the bitmap to a new size
resize.InterpolationMode = InterpolationMode.NearestNeighbor;
resize.DrawImage(cropped, new Rectangle(new Point(0, 0), resized.Size));
return resized;
}
}
}
Why does my image look like this:
When it should look like this (used nearest neighbor in paint.net):