Skip to content

A Bunch of useful GDI+ methods and extensions that can be used for many purposes.

License

Notifications You must be signed in to change notification settings

N-a-r-w-i-n/GDIHelper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GDIHelper

A Bunch of useful GDI+ methods and extensions that can be used for many purposes.

Dependency

.NET Framework 3.5 or higher

Contents

Methods

Extensions

Methods

GetFont

using GDIHelper;

    private readonly Methods _instance = new Methods();
    Font fontfile = _instance.GetFont("fontbytearray /* e.g : a font stored in resources */, fontsize, fontstyle);

GetFont

using GDIHelper;

    private readonly Methods _instance = new Methods();
    Font fontfile = _instance.GetFont("Font path" /* e.g : C:fontfile.ttf */, fontsize, fontstyle);
    

SetAlignment

using GDIHelper;

    private readonly Methods _instance = new Methods();
    StringFormat stringFormat = _instance.SetAlignment(StringAlignment.Center, StringAlignment.Near);
    

GlowBrush

using GDIHelper;

    private readonly Methods _instance = new Methods();
    GraphicsPath GP = new GraphicsPath();
            GP.AddRectangle(new Rectangle(10, 10, 23, 45));
            Brush solidBrush = _instance.GlowBrush(Color.Brown, new Color[] {Color.Black, Color.DarkRed}, new PointF(5, 8), GP, WrapMode.Clamp);
    

GlowBrush

using GDIHelper;

    private readonly Methods _instance = new Methods();
    GraphicsPath GP = new GraphicsPath();
            GP.AddRectangle(new Rectangle(10, 10, 23, 45));
            Brush solidBrush = _instance.GlowBrush(Color.Brown, new Color[] {Color.Black, Color.DarkRed}, new PointF[]{new PointF(12,10), new PointF(0, 2) }, WrapMode.Tile);
    

Extensions

Rectangles

RoundRec

GraphicsPath GP = new Rectangle(0, 0, 10, 10).RoundRec(12, true, true, true, true);

Colors

ToHTML

string htmlColor = Color.Brown.ToHTML();

ToBrush

SolidBrush solidBrush = Color.MediumBlue.ToBrush();

ToPen

Pen pen = Color.Brown.ToPen();

ToRGBString

string rgb = Color.Aquamarine.ToRGBString();

Output :

rgb(127, 255, 212)

ToRGBAString

string rgba = Color.Aquamarine.ToRGBAString();

Output :

rgba(127, 255, 212, 255)

ToARGBString

string argb = Color.Aquamarine.ToARGBString();

Output :

argb(255, 127, 255, 212)

MixColors

Color color = new Color[]{Color.Black, Color.Blue}.MixColors();

Colors

DrawImageFromBase64

Graphics g = CreateGraphics();
/// DrawImageFromBase64(string base64Image, Rectangle rect)
g.DrawImageFromBase64("base64 image", new Rectangle(0, 0, 12, 12));

DrawColoredImage

Graphics g = CreateGraphics();
/// DrawColoredImage(Rectangle r, string base64Image, Color c)
g.DrawColoredImage(new Rectangle(0, 0, 12, 12), "base64 image", Color.Black);

DrawColoredImage

Graphics g = CreateGraphics();
Image image = Image.FromFile("image path");
// DrawColoredImage(Rectangle r, Image image, Color c)
g.DrawColoredImage(new Rectangle(0, 0, 12, 12), image, Color.Black);

DrawTransparentImage

Graphics g = CreateGraphics();
Image image = Image.FromFile("image path");
// DrawTransparentImage(float alpha, Image image, Rectangle rect)
g.DrawTransparentImage(0.1f, image, new Rectangle(0, 0, 12, 12));

DrawStrokedRectangle

Graphics g = CreateGraphics();
// DrawStrokedRectangle(Rectangle rect, Color bodyColor, Color strokeColor, int strokeThickness = 1)
g.DrawStrokedRectangle(new Rectangle(0, 0, 12, 12), Color.Brown, Color.Black, 2);

DrawStrokedEllipse

Graphics g = CreateGraphics();
// DrawStrokedEllipse(Rectangle rect, Color bodyColor, Color strokeColor, int strokeThickness = 1)
g.DrawStrokedEllipse(new Rectangle(0, 0, 12, 12), Color.Brown, Color.Black, 2);

DrawRoundedRectangleWithStroke

Graphics g = CreateGraphics();
// DrawRoundedRectangleWithStroke(Rectangle rect, Color bodyColor, Color strokeColor, int strokeThickness = 1, int curve = 1, bool topLeft = true, bool topRight = true,
            bool bottomLeft = true, bool bottomRight = true)
g.DrawRoundedRectangleWithStroke(new Rectangle(0, 0, 12, 12), Color.Brown, Color.Black, 2, 10, true, true ,true, true);

Images

ToBrush

Image image = Image.FromFile("image path");
TextureBrush pathGradientBrush = image.ToBrush();

ToBrush

Image image = Image.FromFile("image path");
TextureBrush pathGradientBrush = image.ToBrush(new Rectangle(0, 0, 22, 22));

ToPen

Image image = Image.FromFile("image path");
// ToPen(float width = 1, LineCap startCap = LineCap.Custom, LineCap endCap = LineCap.Custom)
Pen pen = image.ToPen(2, LineCap.Custom, LineCap.Custom);

ToBase64

Image image = Image.FromFile("image path");
string base64image = image.ToBase64();

Strings

ToImage

string base64Image = "imageinbase64string";
Image image = base64Image.ToImage();

ToColor

string HTML = "#fff";
// ToColor(int alpha = 255)
Color color = HTML.ToColor(80);

ToBrush

string HTML = "#fff";
// ToBrush(int alpha = 255)
SolidBrush solidBrush = HTML.ToBrush(90);

ToPen

string HTML = "#fff";
// ToPen(int alpha = 255, float size = 1, LineCap startCap = LineCap.Custom, LineCap endCap = LineCap.Custom)
Pen pen = HTML.ToPen(220, 4, LineCap.Custom, LineCap.Custom);

ToFont

string fontName = "Arial";
// ToFont(float size, FontStyle fontStyle = FontStyle.Regular, GraphicsUnit unit = GraphicsUnit.Pixel)
Font font = fontName.ToFont(8, FontStyle.Regular, GraphicsUnit.Display);