2017-04-21 126 views
1

我有一個橢圓形的圖畫框(下面的代碼)。我想在畫框周圍添加邊框。我已經嘗試添加第二個矩形,但這隻會使我的區域變小。有什麼辦法可以製作邊框嗎?在橢圓形的圖畫框周圍繪製邊框

using System; 
using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.Windows.Forms; 

class OvalPictureBox : PictureBox 
{ 
    public OvalPictureBox() 
    { 

    } 

    protected override void OnResize(EventArgs e) 
    { 
     base.OnResize(e); 
     using (var gp = new GraphicsPath()) 
     { 
      gp.AddEllipse(new Rectangle(0, 0, this.Width - 1, this.Height - 1)); 

      this.Region = new Region(gp); 
     } 
    } 

    private void InitializeComponent() 
    { 
     ((System.ComponentModel.ISupportInitialize)(this)).BeginInit(); 
     this.SuspendLayout(); 
     // 
     // OvalPictureBox 
     // 
     ((System.ComponentModel.ISupportInitialize)(this)).EndInit(); 
     this.ResumeLayout(false); 
    } 
} 

編輯

我想通了。我只是在畫箱中畫一個橢圓。

float penWidth = 5F; 
Pen myPen = new Pen(Color.FromArgb(242, 141, 1), penWidth); 
e.Graphics.DrawEllipse(myPen, new RectangleF(new PointF(0, 0), new 
SizeF((float)(portraitPicture.Width - 1), portraitPicture.Height - 1))); 
myPen.Dispose(); 

有沒有更清潔或更好的方法?或者這是最好的方法?

+1

這是我給你的解決方案:) –

+0

哦,所以我確實想念你,哈哈:)謝謝! – Backs

回答

1

只需繪製比您已有的橢圓大的橢圓。但是應該先繪製這個橢圓,否則會覆蓋另一個橢圓。

它應該多大?邊框寬度:)

+0

如果我用你的awnser,好像我只有5px厚的圖片。我究竟做錯了什麼? 我想要一個厚度爲5px的人。 – Backs

+0

所以你應該畫這個橢圓大10px。 –

+0

當我繪製橢圓時它似乎並不重要,它只是讓我的區域像一個5px厚的圓圈 – Backs