Add Border to Image - CSharp System.Drawing

CSharp examples for System.Drawing:Image Effect

Description

Add Border to Image

Demo Code


using System.Drawing.Drawing2D;
using System.Drawing;
using System.Text;
using System.Collections.Generic;
using System;//from w ww .  j ava 2  s  .  c  o  m

public class Main{
        public static Image AddBorder(Image srcImage, int borderWidth)
        {
            Bitmap bmp = new Bitmap(srcImage.Width + borderWidth, srcImage.Height + borderWidth);
            Graphics g = Graphics.FromImage(bmp);
            g.DrawImage(srcImage, 0, 0);
            g.DrawImage(srcImage, new Rectangle(bmp.Width - 1, 0, 1, bmp.Height));
            g.DrawImage(srcImage, new Rectangle(0, bmp.Height - 1, bmp.Width, 1));
            return bmp;
        }

        public static Image AddBorder(Image srcImage)
        {
            return AddBorder(srcImage, 1);
        }
}

Related Tutorials