using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using Branding;
namespace Branding
{
public class Images
{
public Images()
{
}
#region Settings
public struct Settings
{
public Color BackGroundColor;
public Color BackColor;
public Color ForeColor;
public Color TextColor;
public Color BorderColor;
public Font TextFont;
public string Text;
public int Width;
public int Height;
public bool ApplyGlass;
public bool ApplyInternalFill;
public bool ApplyBorder;
}
#endregion
#region Get Rounded Rectangle
public static Bitmap GetRoundedRectangle(Branding.Images.Settings ImageSetting)
{
// Define the size of the rectangle used for each of the 4 arcs.
Size CornerSize = new Size(10,10);
// Define the buffer between the rounded rectangle and the image itself.
int x = 5;
int y = 5;
// Set positions for the arcs and center positions for the rectangle
int w = ImageSetting.Width;
int h = ImageSetting.Height;
int xr = Convert.ToInt32(w - (x + CornerSize.Width));
int yr = Convert.ToInt32(h - (y + CornerSize.Height));
int iw = Convert.ToInt32(w - xr);
int ih = Convert.ToInt32(h - yr);
// Standard angle for each arc.
float sweepAngle = 90.0f;
Bitmap bm = null;
SolidBrush fillBrush;
Color FillColor;
try
{
// Create a new bitmap from scratch
bm = new Bitmap(w,h,PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bm);
// Give us fairly high line quality.
g.SmoothingMode = SmoothingMode.AntiAlias;
// Create a rectangle for the background of the image
Rectangle bgImg = new Rectangle(0,0,w,h);
// Fill the entire image with our image background color
SolidBrush bgBrush = new SolidBrush(ImageSetting.BackGroundColor);
g.FillRectangle(bgBrush,bgImg);
bgBrush.Dispose();
// Create 4 10 x 10 rectangles for our arcs to fit in. tl=topleft, br=bottomright
Rectangle tl = new Rectangle(x,y,CornerSize.Width,CornerSize.Height);
Rectangle tr = new Rectangle(xr,y,CornerSize.Width,CornerSize.Height);
Rectangle bl = new Rectangle(x,yr,CornerSize.Width,CornerSize.Height);
Rectangle br = new Rectangle(xr,yr,CornerSize.Width,CornerSize.Height);
// Create an inner rectangle to fill the middle
Rectangle innerRect = new Rectangle(x,CornerSize.Width,x + xr,yr - y);
// Here's how it is all bound together. We need to add the arcs and the
// inner rectangle to a single GraphicsPath object. This allows us to call
// the .FillPath method to only fill in the section inside the arcs and our rectangle.
GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddArc(tl, 180, sweepAngle);
path.AddArc(tr, 270, sweepAngle);
path.AddRectangle(innerRect);
path.AddArc(bl, 90, sweepAngle);
path.AddArc(br, 360, sweepAngle);
if (ImageSetting.ApplyInternalFill)
{
FillColor = ImageSetting.BackColor;
}
else
{
FillColor = ImageSetting.BackGroundColor;
}
fillBrush = new SolidBrush(FillColor);
// Connect all border lines drawn in the entire path
path.CloseAllFigures();
g.FillPath(fillBrush,path);
fillBrush.Dispose();
if (ImageSetting.ApplyBorder)
{
// If we want a border, we'll need draw the path first and then
// draw a horizontal line at the top of our inner rectangle and again
// at the bottom with the same color as our rectangle fill color to hide
// extra lines drawn above in the path.CloseAllFigures() method.
Pen borderPen = new Pen(ImageSetting.BorderColor, 1);
Pen fillPen = new Pen(FillColor, 1);
g.DrawPath(borderPen,path);
g.DrawLine(fillPen,innerRect.Left + 1,innerRect.Top,innerRect.Width + (x-1),innerRect.Top);
g.DrawLine(fillPen,innerRect.Left + 1,innerRect.Bottom,innerRect.Width + (x-1),innerRect.Bottom);
fillPen.Dispose();
borderPen.Dispose();
}
if (ImageSetting.Text.Length > 0)
{
// If our image has text, draw it using the Font passed in and center the
// text vertically and horizontally against our innerRect rectangle.
SolidBrush TextBrush = new SolidBrush(ImageSetting.TextColor);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;
sf.LineAlignment = StringAlignment.Center;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
g.DrawString(ImageSetting.Text,ImageSetting.TextFont,TextBrush,innerRect,sf);
TextBrush.Dispose();
sf.Dispose();
}
g.Dispose();
}
catch (Exception e) { throw e; }
return bm;
}
#endregion
}
}
}
|