如何在图像周围创建边框/框架?

时间:2013-01-29 22:18:59

标签: c#

我的代码:

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);
}

最后我有了新的动画gif文件。 现在我有边框,这些是位置:

  • 左下角:232.0,408.0
  • 左上角:232.0,211.0
  • 右上角:524.0,211.0
  • 右下角:524.0,408.0

我想在每个图像上添加一个框架,以标记周围的边框。假设边框为红色,边框线大小为5像素。

如何围绕现有位图或gif文件创建矩形? 它不必连接到我的示例代码,但如何使用我拥有的位置创建图像周围的框架/边框?

1 个答案:

答案 0 :(得分:7)

您可以在g.DrawImage(file2, newWidth, 0);

之后添加此行
g.DrawRectangle(new Pen(Brushes.Red, 5), new Rectangle(0, 0, newWidth, file2.Height));

这是一个小型测试方法,因此您可以看到它正常工作

    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");
    }

之前:enter image description here之后:enter image description here

您可以在任何地方添加rectagle,tou jst需要提供X,Y,Width,Height

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

使用你的4点结构,这应该可行

   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