using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using DTV.Classes.Types;
namespace DTV.Classes.Utils
{
public enum ImagePart
{
TopLeft = 1,
TopRight = 2,
BottomLeft = 3,
BottomRight = 4,
Top = 5,
Bottom = 6,
Left = 7,
Right = 8,
Center = 9
}
public enum ButtonDesignState
{
None = 0,
Unpressed = 1,
Pressed = 2
}
public class ImageRender
{
// PRIVATE MEMBERS
protected Dictionary<ImagePart, Image> m_CuttedImages = new Dictionary<ImagePart, Image>();
protected Image m_OriginalImage;
protected int m_DivX;
protected int m_DivY;
protected BorderTypes m_TypeRender;
protected ButtonDesignState m_ButtonState;
// CONSTRUCTOR - DESTRUCTOR
public ImageRender(BorderTypes p_TypeRender, Image p_Image, ButtonDesignState p_Bs)
{
if (p_Image != null)
m_OriginalImage = new Bitmap(p_Image);
m_ButtonState = p_Bs;
m_TypeRender = p_TypeRender;
Build();
}
// PROPERTIES
/// <summary>
/// Get the original image before render.
/// </summary>
public Image OriginalImage
{
get { return m_OriginalImage; }
}
/// <summary>
/// Get the X diviser component depending on the original image.
/// </summary>
public int DivX
{
get { return m_DivX; }
}
/// <summary>
/// Get the status of the Button for the design. Can be None, Unpressed, Pressed.
/// </summary>
public ButtonDesignState ButtonState
{
get { return m_ButtonState; }
}
/// <summary>
/// Get the Y diviser component depending on the original image.
/// </summary>
public int DivY
{
get { return m_DivY; }
}
/// <summary>
/// Get the Border type defined for this ImageRender.
/// </summary>
public BorderTypes BorderType
{
get { return m_TypeRender; }
}
/// <summary>
/// Get the status of the analyze done for the original image. Used for checking if the image is correct for modifiers.
/// </summary>
/// <returns></returns>
public String GetAnalyze()
{
String s = "";
if (m_TypeRender == BorderTypes.Border)
{
s = "";
if ((OriginalImage.Width % 8) != 0)
{
s += "The initial width cannot be divided by 8. ";
}
if ((OriginalImage.Height % 4) != 0 && ButtonState == ButtonDesignState.None)
{
s += "The initial height cannot be divided by 4. ";
}
if ((OriginalImage.Height % 8) != 0 && ButtonState != ButtonDesignState.None)
{
s += "The initial height cannot be divided by 8. ";
}
}
if (m_TypeRender == BorderTypes.HorizontalBorder)
{
s = "";
if ((OriginalImage.Width % 10) != 0)
{
s += "The initial width cannot be divided by 10.";
}
if ((OriginalImage.Height % 2) != 0 && ButtonState != ButtonDesignState.None)
{
s += "The initial height cannot be divided by 2. ";
}
}
if (m_TypeRender == BorderTypes.EndCap)
{
s = "";
if ((OriginalImage.Width % 2) != 0)
{
s += "The initial width cannot be divided by 2.";
}
if ((OriginalImage.Height % 2) != 0 && ButtonState == ButtonDesignState.None)
{
s += "The initial height cannot be divided by 2.";
}
if ((OriginalImage.Height % 4) != 0 && ButtonState != ButtonDesignState.None)
{
s += "The initial height cannot be divided by 4.";
}
}
if (s == "")
return "Valid";
return s;
}
// METHODS
private void Build()
{
int IntIsButton = 1;
if (ButtonState != ButtonDesignState.None)
IntIsButton = 2;
if (m_TypeRender == BorderTypes.Border)
{
// Cut data.
int divx = (m_OriginalImage.Width / 4);
int divy = (m_OriginalImage.Height / (8 * IntIsButton));
int maxx = m_OriginalImage.Width;
int maxy = m_OriginalImage.Height;
m_DivX = divx;
m_DivY = divy;
int i, j, k;
Graphics l;
Image b;
List<Image> Imgs = new List<Image>();
k = 0;
for (j = 0; j < 8 * IntIsButton; j++)
{
for (i = 0; i < 4; i++)
{
b = new Bitmap(divx, divy);
l = Graphics.FromImage(b);
l.DrawImageUnscaled(m_OriginalImage, new Point(-(divx * i), -(divy * j)));
Imgs.Add(b);
k++;
}
}
Bitmap a;
Graphics ag;
// Draw Top.
a = new Bitmap( OriginalImage.Width, DivY);
ag = Graphics.FromImage(a);
ag.DrawImageUnscaled(Imgs[4], new Point(0, 0));
ag.DrawImageUnscaled(Imgs[5], new Point(DivX, 0));
ag.DrawImageUnscaled(Imgs[6], new Point(DivX * 2, 0));
ag.DrawImageUnscaled(Imgs[7], new Point(DivX * 3, 0));
Imgs[4] = a;
// Draw Bottom.
a = new Bitmap(OriginalImage.Width, DivY);
ag = Graphics.FromImage(a);
ag.DrawImageUnscaled(Imgs[8], new Point(0, 0));
ag.DrawImageUnscaled(Imgs[9], new Point(DivX, 0));
ag.DrawImageUnscaled(Imgs[10], new Point(DivX * 2, 0));
ag.DrawImageUnscaled(Imgs[11], new Point(DivX * 3, 0));
Imgs[8] = a;
// Draw Left.
a = new Bitmap(OriginalImage.Width, DivY);
ag = Graphics.FromImage(a);
ag.DrawImageUnscaled(Imgs[16], new Point(0, 0));
ag.DrawImageUnscaled(Imgs[17], new Point(DivX, 0));
ag.DrawImageUnscaled(Imgs[18], new Point(DivX * 2, 0));
ag.DrawImageUnscaled(Imgs[19], new Point(DivX * 3, 0));
Imgs[16] = a;
// Draw Right.
a = new Bitmap(OriginalImage.Width, DivY);
ag = Graphics.FromImage(a);
ag.DrawImageUnscaled(Imgs[12], new Point(0, 0));
ag.DrawImageUnscaled(Imgs[13], new Point(DivX, 0));
ag.DrawImageUnscaled(Imgs[14], new Point(DivX * 2, 0));
ag.DrawImageUnscaled(Imgs[15], new Point(DivX * 3, 0));
Imgs[12] = a;
m_CuttedImages.Clear();
if (ButtonState == ButtonDesignState.Unpressed || ButtonState == ButtonDesignState.None)
{
Imgs[12].RotateFlip(RotateFlipType.Rotate90FlipNone);
Imgs[16].RotateFlip(RotateFlipType.Rotate90FlipNone);
m_CuttedImages.Add(ImagePart.TopLeft, Imgs[0]);
m_CuttedImages.Add(ImagePart.TopRight, Imgs[1]);
m_CuttedImages.Add(ImagePart.BottomLeft, Imgs[2]);
m_CuttedImages.Add(ImagePart.BottomRight, Imgs[3]);
m_CuttedImages.Add(ImagePart.Top, Imgs[4]);
m_CuttedImages.Add(ImagePart.Bottom, Imgs[8]);
m_CuttedImages.Add(ImagePart.Left, Imgs[16]);
m_CuttedImages.Add(ImagePart.Right, Imgs[12]);
m_CuttedImages.Add(ImagePart.Center, Imgs[20]);
}
else if (ButtonState == ButtonDesignState.Pressed)
{
Imgs[44].RotateFlip(RotateFlipType.Rotate90FlipNone);
Imgs[48].RotateFlip(RotateFlipType.Rotate90FlipNone);
m_CuttedImages.Add(ImagePart.TopLeft, Imgs[32]);
m_CuttedImages.Add(ImagePart.TopRight, Imgs[33]);
m_CuttedImages.Add(ImagePart.BottomLeft, Imgs[34]);
m_CuttedImages.Add(ImagePart.BottomRight, Imgs[35]);
m_CuttedImages.Add(ImagePart.Top, Imgs[36]);
m_CuttedImages.Add(ImagePart.Bottom, Imgs[40]);
m_CuttedImages.Add(ImagePart.Left, Imgs[48]);
m_CuttedImages.Add(ImagePart.Right, Imgs[44]);
m_CuttedImages.Add(ImagePart.Center, Imgs[52]);
}
return;
}
if (m_TypeRender == BorderTypes.HorizontalBorder)
{
// Cut data.
int divx = m_OriginalImage.Width / 10;
int divy = (m_OriginalImage.Height / (1 * IntIsButton));
int maxx = m_OriginalImage.Width;
int maxy = m_OriginalImage.Height;
m_DivX = divx;
m_DivY = divy;
int i, j, k;
Graphics l;
Bitmap b;
List<Image> Imgs = new List<Image>();
k = 0;
for (j = 0; j < 1 * IntIsButton; j++)
{
for (i = 0; i < 10; i++)
{
b = new Bitmap(divx, divy);
l = Graphics.FromImage(b);
l.DrawImage(m_OriginalImage, new Point(-(divx * i), 0));
Imgs.Add(b);
k++;
}
}
m_CuttedImages.Clear();
if (ButtonState == ButtonDesignState.Unpressed || ButtonState == ButtonDesignState.None)
{
Imgs[4].RotateFlip(RotateFlipType.Rotate90FlipNone);
Imgs[5].RotateFlip(RotateFlipType.Rotate90FlipNone);
m_CuttedImages.Add(ImagePart.TopLeft, Imgs[0]);
m_CuttedImages.Add(ImagePart.TopRight, Imgs[1]);
m_CuttedImages.Add(ImagePart.BottomLeft, Imgs[2]);
m_CuttedImages.Add(ImagePart.BottomRight, Imgs[3]);
m_CuttedImages.Add(ImagePart.Top, Imgs[4]);
m_CuttedImages.Add(ImagePart.Bottom, Imgs[5]);
m_CuttedImages.Add(ImagePart.Left, Imgs[6]);
m_CuttedImages.Add(ImagePart.Right, Imgs[7]);
m_CuttedImages.Add(ImagePart.Center, Imgs[8]);
}
else if (ButtonState == ButtonDesignState.Pressed)
{
Imgs[14].RotateFlip(RotateFlipType.Rotate90FlipNone);
Imgs[15].RotateFlip(RotateFlipType.Rotate90FlipNone);
m_CuttedImages.Add(ImagePart.TopLeft, Imgs[10]);
m_CuttedImages.Add(ImagePart.TopRight, Imgs[11]);
m_CuttedImages.Add(ImagePart.BottomLeft, Imgs[12]);
m_CuttedImages.Add(ImagePart.BottomRight, Imgs[13]);
m_CuttedImages.Add(ImagePart.Top, Imgs[14]);
m_CuttedImages.Add(ImagePart.Bottom, Imgs[15]);
m_CuttedImages.Add(ImagePart.Left, Imgs[16]);
m_CuttedImages.Add(ImagePart.Right, Imgs[17]);
m_CuttedImages.Add(ImagePart.Center, Imgs[18]);
}
return;
}
if (m_TypeRender == BorderTypes.EndCap)
{
// Cut data.
int divx = m_OriginalImage.Width / 2;
int divy = m_OriginalImage.Height / (2 * IntIsButton);
int maxx = m_OriginalImage.Width;
int maxy = m_OriginalImage.Height;
m_DivX = divx;
m_DivY = divy;
int i, j, k;
Graphics l;
Bitmap b;
List<Image> Imgs = new List<Image>();
k = 0;
for (j = 0; j < 2 * IntIsButton; j++)
{
for (i = 0; i < 2; i++)
{
b = new Bitmap(divx, divy);
l = Graphics.FromImage(b);
l.DrawImageUnscaled(m_OriginalImage, new Point(-(divx * i), -(divy * j)));
Imgs.Add(b);
k++;
}
}
//Imgs[4].RotateFlip(RotateFlipType.Rotate90FlipNone);
//Imgs[5].RotateFlip(RotateFlipType.Rotate90FlipNone);
m_CuttedImages.Clear();
if (ButtonState == ButtonDesignState.Unpressed || ButtonState == ButtonDesignState.None)
{
m_CuttedImages.Add(ImagePart.Left, Imgs[0]);
m_CuttedImages.Add(ImagePart.Right, Imgs[1]);
m_CuttedImages.Add(ImagePart.Center, Imgs[2]);
}
else if (ButtonState == ButtonDesignState.Pressed)
{
m_CuttedImages.Add(ImagePart.Left, Imgs[4]);
m_CuttedImages.Add(ImagePart.Right, Imgs[5]);
m_CuttedImages.Add(ImagePart.Center, Imgs[6]);
}
return;
}
}
/// <summary>
/// Get the image part specified after cutted by the constructor.
/// </summary>
/// <param name="p_ImagePart">The image part you want.</param>
/// <returns></returns>
public Image GetImagePart(ImagePart p_ImagePart)
{
if (m_CuttedImages.ContainsKey(p_ImagePart))
return m_CuttedImages[p_ImagePart];
return null;
}
/// <summary>
/// Render the image for the specified size.
/// </summary>
/// <param name="p_Size">Size you want the image is designed for.</param>
public Image RenderImage(Size p_Size)
{
Bitmap b = new Bitmap(p_Size.Width, p_Size.Height);
Graphics finalg = Graphics.FromImage(b);
finalg.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
finalg.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None;
finalg.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic;
if (m_TypeRender == BorderTypes.Border)
{
// Draw the top borders.
finalg.DrawImageUnscaledAndClipped(GetImagePart(ImagePart.TopLeft), new Rectangle(new Point(0, 0), GetImagePart(ImagePart.TopLeft).Size));
finalg.DrawImageUnscaledAndClipped(GetImagePart(ImagePart.TopRight), new Rectangle(new Point((b.Width - GetImagePart(ImagePart.TopRight).Width), 0), GetImagePart(ImagePart.TopLeft).Size));
DrawImageFill(finalg, (Bitmap)GetImagePart(ImagePart.Top), new Rectangle(new Point(DivX, 0), new Size((b.Width - GetImagePart(ImagePart.TopLeft).Width - GetImagePart(ImagePart.TopRight).Width), GetImagePart(ImagePart.Top).Height)));
// Draw the side borders.
DrawImageFill(finalg, (Bitmap)GetImagePart(ImagePart.Left), new Rectangle(new Point(0, DivY), new Size(GetImagePart(ImagePart.Left).Width, b.Height - GetImagePart(ImagePart.TopLeft).Height - GetImagePart(ImagePart.BottomLeft).Height)));
DrawImageFill(finalg, (Bitmap)GetImagePart(ImagePart.Right), new Rectangle(new Point(b.Width - GetImagePart(ImagePart.Right).Width, DivY), new Size(GetImagePart(ImagePart.Left).Width, b.Height - GetImagePart(ImagePart.TopRight).Height - GetImagePart(ImagePart.BottomRight).Height)));
// Draw the bottom borders.
finalg.DrawImageUnscaledAndClipped(GetImagePart(ImagePart.BottomLeft), new Rectangle(new Point(0, (b.Height - GetImagePart(ImagePart.BottomLeft).Height)), GetImagePart(ImagePart.BottomLeft).Size));
finalg.DrawImageUnscaledAndClipped(GetImagePart(ImagePart.BottomRight), new Rectangle(new Point((b.Width - GetImagePart(ImagePart.BottomRight).Width), (b.Height - GetImagePart(ImagePart.BottomLeft).Height)), GetImagePart(ImagePart.BottomRight).Size));
DrawImageFill(finalg, (Bitmap)GetImagePart(ImagePart.Bottom), new Rectangle(new Point(DivX, b.Height - GetImagePart(ImagePart.Bottom).Height), new Size((b.Width - GetImagePart(ImagePart.BottomLeft).Width - GetImagePart(ImagePart.BottomRight).Width), GetImagePart(ImagePart.Bottom).Height)));
// Draw the center.
finalg.DrawImage((Bitmap)GetImagePart(ImagePart.Center), new Rectangle(new Point(DivY, DivY), new Size((b.Width - GetImagePart(ImagePart.Left).Width - GetImagePart(ImagePart.Right).Width), (b.Height - GetImagePart(ImagePart.Top).Height - GetImagePart(ImagePart.Bottom).Height))));
// Finish drawing.
return b;
}
if (m_TypeRender == BorderTypes.HorizontalBorder)
{
// Draw the top borders.
finalg.DrawImageUnscaledAndClipped(GetImagePart(ImagePart.TopLeft), new Rectangle(new Point(0, 0), GetImagePart(ImagePart.TopLeft).Size));
finalg.DrawImageUnscaledAndClipped(GetImagePart(ImagePart.TopRight), new Rectangle(new Point((b.Width - GetImagePart(ImagePart.TopRight).Width), 0), GetImagePart(ImagePart.TopLeft).Size));
finalg.DrawImage(GetImagePart(ImagePart.Top), new Rectangle(new Point(DivX, 0), new Size((b.Width - GetImagePart(ImagePart.TopLeft).Width - GetImagePart(ImagePart.TopRight).Width), GetImagePart(ImagePart.Top).Height)));
// Draw the side borders.
finalg.DrawImage(GetImagePart(ImagePart.Left), new Rectangle(new Point(0, DivY), new Size(GetImagePart(ImagePart.Left).Width, b.Height - GetImagePart(ImagePart.TopLeft).Height - GetImagePart(ImagePart.BottomLeft).Height)));
finalg.DrawImage(GetImagePart(ImagePart.Right), new Rectangle(new Point(b.Width - GetImagePart(ImagePart.Right).Width, DivY), new Size(GetImagePart(ImagePart.Left).Width, b.Height - GetImagePart(ImagePart.TopRight).Height - GetImagePart(ImagePart.BottomRight).Height)));
// Draw the bottom borders.
finalg.DrawImageUnscaledAndClipped(GetImagePart(ImagePart.BottomLeft), new Rectangle(new Point(0, (b.Height - GetImagePart(ImagePart.BottomLeft).Height)), GetImagePart(ImagePart.BottomLeft).Size));
finalg.DrawImageUnscaledAndClipped(GetImagePart(ImagePart.BottomRight), new Rectangle(new Point((b.Width - GetImagePart(ImagePart.BottomRight).Width), (b.Height - GetImagePart(ImagePart.BottomLeft).Height)), GetImagePart(ImagePart.BottomRight).Size));
finalg.DrawImage(GetImagePart(ImagePart.Bottom), new Rectangle(new Point(DivX, b.Height - GetImagePart(ImagePart.Bottom).Height), new Size((b.Width - GetImagePart(ImagePart.BottomLeft).Width - GetImagePart(ImagePart.BottomRight).Width), GetImagePart(ImagePart.Bottom).Height)));
// Draw the center.
finalg.DrawImage(GetImagePart(ImagePart.Center), new Rectangle(new Point(DivX, DivX), new Size((b.Width - GetImagePart(ImagePart.Left).Width - GetImagePart(ImagePart.Right).Width), (b.Height - GetImagePart(ImagePart.Top).Height - GetImagePart(ImagePart.Bottom).Height))));
// Finish drawing.
return b;
}
if (m_TypeRender == BorderTypes.Normal)
{
finalg.DrawImage(OriginalImage, 0, 0, p_Size.Width, p_Size.Height);
return b;
}
if (m_TypeRender == BorderTypes.EndCap)
{
finalg.DrawImageUnscaledAndClipped(GetImagePart(ImagePart.Left), new Rectangle(new Point(0, 0), GetImagePart(ImagePart.Left).Size));
DrawImageFill(finalg, (Bitmap)GetImagePart(ImagePart.Center), new Rectangle(new Point(DivX, 0), new Size(b.Width - GetImagePart(ImagePart.Left).Width - GetImagePart(ImagePart.Right).Width, GetImagePart(ImagePart.Center).Height)));
finalg.DrawImageUnscaledAndClipped(GetImagePart(ImagePart.Right), new Rectangle(new Point(b.Width - GetImagePart(ImagePart.Right).Width, 0), GetImagePart(ImagePart.Right).Size));
}
return b;
}
private void DrawImageFill(Graphics p_Graphics, Bitmap p_Image, Rectangle p_Rectangle)
{
int curx = p_Rectangle.X;
int maxx = p_Rectangle.X + p_Rectangle.Width;
int cury = p_Rectangle.Y;
int maxy = p_Rectangle.Y + p_Rectangle.Height;
while (cury < maxy)
{
while (curx < maxx)
{
if (curx + p_Image.Width <= maxx && cury + p_Image.Height <= maxy)
{
p_Graphics.DrawImageUnscaled(p_Image, new Point(curx, cury));
if (Properties.Settings.Default.ShowLimitersRender)
p_Graphics.DrawRectangle(new Pen(new SolidBrush(Properties.Settings.Default.FullAreaRender), 2f), new Rectangle(new Point(curx, cury), new Size(Math.Min(maxx - curx, p_Image.Width), Math.Min(maxy - cury, p_Image.Height))));
}
else
{
p_Graphics.DrawImageUnscaledAndClipped(p_Image, new Rectangle(new Point(curx, cury), new Size(Math.Min(maxx - curx, p_Image.Width), Math.Min(maxy - cury, p_Image.Height))));
if (Properties.Settings.Default.ShowLimitersRender)
p_Graphics.DrawRectangle(new Pen(new SolidBrush(Properties.Settings.Default.PartAreaRender), 2f), new Rectangle(new Point(curx, cury), new Size(Math.Min(maxx - curx,p_Image.Width), Math.Min(maxy - cury, p_Image.Height))));
}
curx = curx + p_Image.Width;
}
curx = p_Rectangle.X;
cury = cury + p_Image.Height;
}
}
}
}
Comments