Hello! I'm making a simple game in C# and i'm trying to slice my UI images into 9 smaller rectangles, so
the image doesn't get stretched when drawing to different resolutions, but my current implementation does
not seems correct and i would like this method to look similar to the Unity3D sprite slicing editor.
I have the following method for slicing an image into rectangles:
public class Sprite
{
public Image texture;
public PointF position;
public SizeF size;
public RectangleF rectangle;
public RectangleF[] SliceRectangle(RectangleF rectangle, float left, float right, float top, float bottom)
{
float x = rectangle.X;
float y = rectangle.Y;
float left_width = (float) Math.Abs(Math.Sqrt(Math.Pow(left - rectangle.Left, 2)));
float middle_width = (float) Math.Abs(Math.Sqrt(Math.Pow(left - right, 2)));
float right_width = (float) Math.Abs(Math.Sqrt(Math.Pow(right - rectangle.Right, 2)));
float top_height = (float) Math.Abs(Math.Sqrt(Math.Pow(top - rectangle.Top, 2)));
float middle_height = (float) Math.Abs(Math.Sqrt(Math.Pow(top - bottom, 2)));
float bottom_height = (float) Math.Abs(Math.Sqrt(Math.Pow(bottom - rectangle.Bottom, 2)));
return new RectangleF[]
{
new RectangleF(x, y, left_width, top_height),
new RectangleF(left, y, middle_width, top_height),
new RectangleF(right, y, right_width, top_height),
new RectangleF(x, top, left_width, middle_height),
new RectangleF(left, top, middle_width, middle_height),
new RectangleF(right, top, right_width, middle_height),
new RectangleF(x, bottom, left_width, bottom_height),
new RectangleF(left, bottom, middle_width, bottom_height),
new RectangleF(right, bottom, right_width, bottom_height),
};
}
}
And then i draw the sliced image from the Sprite class like this:
var sprite = new Sprite();
public void Render (Graphics graphics)
{
sprite.rectangle = new RectangleF(0, 0, 512, 512);
var destination_slices = sprite.SliceRectangle(rectangle: sprite.rectangle, left: 32, right: 480, top: 32, bottom: 480);
var source_slices = sprite.SliceRectangle(rectangle: new RectangleF(0, 0, sprite.texture.Width, sprite.texture.Height), left: 32, right: 224, top: 16, bottom: 48);
for (int i = 0; i < source_slices.Length; i++)
{
graphics.DrawImage(sprite.texture, destination_slices[i], source_slices[i], GraphicsUnit.Pixel);
Debug.Log(source_slices[i]);
}
}
Now, the problem with this method is:
. I also need to slice the main sprite rectangle, which makes no sense.
. If i set the padding of the main sprite rectangle wrong, it will mess with the whole drawing.
. If i make an simple editor for visually setting the paddings of a texture, i would also need to do it for the main rectangle.
Some specifications:
. I want the image to be sliced into smaller parts.
. The size of the image i'm drawing is 256 by 64 pixels.
. The area i want to draw the image is 512 by 512 pixels.
. The left/right/top/bottom values are basically lines that demarcates the bounds of each slice.
So, what can i do to fix these problems? Thanks in advance!
What I have tried:
I'm searching for a correct method to slice sprites since yesterday but did not found anything that solved my problem.