imageComponent.NET - image processing .NET assemblies
Write borders to an image in .NET

BorderBuilder

The BorderBuilder is the class provided functionality to generate borders (frame) around your image. The object is very easy to use; it has only three properties. You can specify which edge to draw the border by using Alignment property. You can specify how large the border is by using Length property. You can specify the border style by using Brush property.

Usage of BorderBuilder

To use this class is very easy, just following the below steps:

  1. Intialize a new instance of BorderBuilder object
  2. Specify the border alignment if needed. By default it is ALL, which will draw borders to all edges. You can use any combination of four alignments: East, South, West, or North. For example, BorderAlignment.SOUTH | BorderAlignment.EAST will draw borders to only south and east edges.
  3. Specify the border length, which defines the size of the border.
  4. Assign a Brush to draw the border, you can get create a brush via:
    1. Use BorderBuilder static methods to create a built-in brush, currently it provides 10 kinds of special styles. All of them return PathGradientBrush.
    2. Create a custom brush, HatchBrush, SolidBrush, or TextureBrush. Just use your creativity to create anyone you like.
  5. Pass the BorderBuilder object to Controller.SetBorder method. It's done.

Sample Code

The following code demonstrates how to write borders in your image by using PhotoController:

   1:  Dim img As Image = pic.Image
   2:  Dim pc As New Controller(img)
   3:  Dim bb As New BorderBuilder()
   4:  bb.Alignment = BorderAlignment.ALL
   5:   
   6:  ' customize the brush
   7:  Dim mybrush As System.Drawing.Drawing2D.PathGradientBrush = BorderBuilder.CreateSilkBrush(20)
   8:   
   9:  ' I just randomly assing the color here, not sure they will have a good-looking
  10:  mybrush.CenterColor = Color.SkyBlue
  11:  mybrush.SurroundColors = New Color() {Color.Coral, Color.Azure, Color.BlanchedAlmond, Color.Gold}
  12:  bb.Brush = mybrush
  13:   
  14:  ' the border length is different to the brush length so that we can see two border
  15:  bb.Length = 40
  16:  pic.Image = pc.SetBorder(bb)
  17:   
  18:  pc.Dispose()

This is VB.NET sample code, for c# reference, please look at the open source demo application PhotoGear.

Brush Length v.s. Border Length

You might have noticed when creating the brush, you need to pass a length, which is the brush length. And it's different to the border length. The brush length is the size of your brush, while the border length is the size of the border, which means the area that is allowed to paint with the brush. Hence, if the border length is half of the brush length, it's obviously you can only see half size of the brush. In contrast, if the border length is double of the brush length, that means it will paint with the brush twice. To better understand the whole concept, we can see some sample figures:

The first figure shows a silk brush border using Border Length 20 and Brush Length 20

Image Border and Brush

The second figure shows a silk brush border using Border Length 40 and Brush Length 20, you can see since the Border Length is larger than Brush Length, so the silk is the same as above, but there are two silks this time.

Image Border and Brush

The third figure shows a silk brush border using Border Length 40 and Brush Length 40, you can see since Brush size is bigger, we have a larger silk this time. But since the Border Length is equal to Brush Length, we have only one silk.

Image Border and Brush

Customize Border Brush

It's easy to customize a border brush by using SolidBrush, HatchBrush, or TextureBrush. Their reference can be found in MSDN. If you're having accessing MDSN, it may be your broadband speed, which can be improved by signing up to any decent broadband package, i.e. o2 broadband. The following code snippets briefly demonstrates some simple usages:

A solid red color border (C#)

   1:  BorderBuilder bb = new BorderBuilder();
   2:  bb.Brush = new SolidBrush(Color.DarkRed);

A solid red color border (VB.NET)

   1:  Dim bb As New BorderBuilder()
   2:  bb.Brush = New SolidBrush(Color.DarkRed)

A large grid border (C#)

   1:  BorderBuilder bb = new BorderBuilder();
   2:  bb.Brush = new HatchBrush(HatchStyle.LargeGrid, Color.DarkBlue, Color.Red);

A large grid border (VB.NET)

   1:  Dim bb As New BorderBuilder()
   2:  bb.Brush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.LargeGrid, Color.DarkBlue, Color.Red)

A texture border (VB.NET)

   1:  Dim bb As New BorderBuilder()
   2:  bb.Brush = New System.Drawing.TextureBrush(Image.FromFile("C:\\texture.jpg"))

[Back to Top]
Comments

If you feel this page can be improved or has any mistake, please enter your valuable opinion here. Or if you cannot find the information you are after, feel free to suggest a subject too. I appreciate any voice.

Your Name:
  
Your Email Address:
  
Comments: