1

我的代码:

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 文件创建矩形?它不必在这里连接到我的示例代码,但是如何使用我拥有的位置在图像周围创建框架/边框?

4

1 回答 1

8

您可以在之后添加此行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");
    }

之前:在此处输入图像描述之后: 在此处输入图像描述

你可以在任何你想要的地方添加矩形,你只需要提供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

在此处输入图像描述

于 2013-01-29T22:30:44.957 回答