Jhonatan Birdy
Jhonatan Birdy

Reputation: 227

How can I create a border/frame around an image?

My code:

private void CreateAnimatedGif(List<string> GifsFilesRadar , List<string> GifsFilesSatellite)//string FileName1 , string FileName2)
{
    Bitmap bitmap = null;
    DirectoryInfo inf = new DirectoryInfo(tempGifFiles);
    FileInfo[] fi = inf.GetFiles("*.gif");
    for (int i = 0; i < fi.Length; i++)
    {
        Bitmap file1 = new Bitmap(GifsFilesRadar[i]);
        Bitmap file2 = new Bitmap(GifsFilesSatellite[i]);
        //calculate the new width proportionally to the new height it will have
        int newWidth = file1.Width + file1.Width / (file2.Height / (file2.Height - file1.Height));

        bitmap = new Bitmap(newWidth + file2.Width, Math.Max(file1.Height, file2.Height));
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            //high quality rendering and interpolation mode
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            //resize the left image
            g.DrawImage(file1, new Rectangle(0, 0, newWidth, file2.Height));
            g.DrawImage(file2, newWidth, 0);
            string t = @"d:\GifsForAnimations" + "\\" + i.ToString("D6") + ".Gif";
            bitmap.Save(t, System.Drawing.Imaging.ImageFormat.Gif);
            if (i == 4)
            {
                break;
            }
        }
    }
    List<string> gif = new List<string>();
    DirectoryInfo info = new DirectoryInfo(@"d:\GifsForAnimations");
    FileInfo[] finfo = info.GetFiles();
    for (int i = 0; i < finfo.Length; i++)
    {
        gif.Add(finfo[i].FullName);
    }
    newFile.MakeGIF(gif, @"d:\newGifAnim.gif", 80, true);
}

In the end I have new animated gif file. Now I have the border and these are the locations:

I want to add on each image a frame around it to mark the border around. Let's say the border will be in Red and the border line size will be 5 pixels.

How can I create the rectangle around existing bitmap or gif file ? It doesn't have to be connected to my example code here but how do I create the frame/border around the image with the locations I have ?

Upvotes: 1

Views: 19598

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43596

You could add this line after g.DrawImage(file2, newWidth, 0);

g.DrawRectangle(new Pen(Brushes.Red, 5), new Rectangle(0, 0, newWidth, file2.Height));

Here is a small test method so you can see it working

    private void button1_Click(object sender, EventArgs e)
    {
        Bitmap bitmap = new Bitmap(@"C:\avatar63.jpg");
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.DrawRectangle(new Pen(Brushes.Red, 5), new Rectangle(0, 0, bitmap.Width, bitmap.Height));
        }
        bitmap.Save(@"C:\avatar63New.jpg");
    }

Before: enter image description here After: enter image description here

You can add the rectagle anywhere you want, tou jst need to supply the X,Y,Width,Height

g.DrawRectangle(new Pen(Brushes.LimeGreen, 5), new Rectangle(50, 50, 100, 100));

Using your 4 point structure this should work

   Point topLeft = new Point(232,211 );
   Point topRightr = new Point(232, 408);
   Point bottomLeft = new Point(524, 211);
   Point bottomRight = new Point(524, 408);

   g.DrawRectangle(new Pen(Brushes.LimeGreen, 5), new Rectangle(topLeft, new Size(topRightr.X - topLeft.X, bottomLeft.Y - topLeft.Y)));

  // TopLeft = rectangle location
  // TopRight.X - TopLeft.X = Width of rectangle
  // BottomLeft.Y - TopLeft.Y = height of rectangle

enter image description here

Upvotes: 11

Related Questions