Here's something I've written in my pass-time coding sessions. What it basically does is redraw any Image, draws a border around it and returns it back to you.
The only perimeter that requires explanation is the
borderType (BorderType) perimeter.
BorderType.Overlay draws the originalImage on a plane of equal size and draw the border over it.
BorderType.Additive will draw the originalImage on a larger plane (depending on the pen's width) and then draw the border in the empty space.
BorderType.Subtractive will draw the originalImage on a smaller plane (depending on the pen's width) and draw a border over it.
Required namespace:
Method:
Code:
public enum BorderType { Overlay, Additive, Subtractive }
public Image DrawBorder(Image originalImage, Pen pen, BorderType borderType)
{
int penWidth = (int)pen.Width;
float halfPenWidth = (float)(penWidth / 2);
Image newImage = (Image)null;
switch (borderType)
{
case BorderType.Overlay:
if ((penWidth * 2) >= originalImage.Width || (penWidth * 2) >= originalImage.Height)
{
throw new Exception("The pen's width is too large and will cover the entire image. Plase reduce the pen width.");
}
else
{
newImage = (Image)new Bitmap(originalImage.Width, originalImage.Height);
}
break;
case BorderType.Additive:
newImage = (Image)new Bitmap(originalImage.Width + (penWidth * 2), originalImage.Height + (penWidth * 2));
break;
case BorderType.Subtractive:
newImage = (Image)new Bitmap(originalImage.Width - (penWidth * 2), originalImage.Height - (penWidth * 2));
break;
}
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.DrawImage(originalImage, new Point(penWidth, penWidth));
RectangleF borderRectangle = new RectangleF();
borderRectangle.Location = new PointF(halfPenWidth, halfPenWidth);
borderRectangle.Size = new SizeF((float)newImage.Width - pen.Width, (float)newImage.Width - pen.Width);
graphics.DrawRectangle(pen, borderRectangle.X, borderRectangle.Y, borderRectangle.Width, borderRectangle.Height);
}
return newImage;
}
- - - Updated - - -
Also, I haven't tested the code so you might encounter some problems... let me know.