2011-10-11 11 views
11

ピクチャボックスコントロールのエッジを丸くする方法。私は楕円のような角度を取得したいが、私はそれを行う方法を知らない。私はC#を使用します。ありがとう!ピクチャボックス内の四角形C#

答えて

14

チェックアウトする場合。プロジェクトに新しいクラスを追加し、以下に示すコードを貼り付けます。コンパイル。新しいコントロールをツールボックスの上部からフォームにドロップします。

フォーム上の1つのピクチャボックスを置く
using System; 
using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.Windows.Forms; 

class OvalPictureBox : PictureBox { 
    public OvalPictureBox() { 
     this.BackColor = Color.DarkGray; 
    } 
    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); 
     } 
    } 
} 
+0

ああ、ありがとう!クラスを拡張すると、実際には可能性のイメージが得られます。そして、あなたのツールボックスに変更されたコンポーネントをどれだけ簡単に入手するのが簡単です。 :) – Arndroid

9

ラウンドエッジラウンドコーナーのように?

ので、はい、何の問題は、あなたがコントロールにその地域の特性を有する任意の形状を与えることはできませんhttp://social.msdn.microsoft.com/forums/en-US/winforms/thread/603084bb-1aae-45d1-84ae-8544386d58fd

Rectangle r = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height); 
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath(); 
int d = 50; 
gp.AddArc(r.X, r.Y, d, d, 180, 90); 
gp.AddArc(r.X + r.Width - d, r.Y, d, d, 270, 90); 
gp.AddArc(r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90); 
gp.AddArc(r.X, r.Y + r.Height - d, d, d, 90, 90); 
pictureBox1.Region = new Region(gp); 
10

と、このコード を書くにも、あなたは

System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath(); 
      gp.AddEllipse(0, 0, pictureBox1.Width - 3, pictureBox1.Height - 3); 
      Region rg = new Region(gp); 
      pictureBox1.Region = rg; 

enter image description here

+0

ありがとう、これは必要な構文の量のための素晴らしい解決策です。私はこのリソースがどれくらいのリソースをテストしていませんが、今のところそれが好きです。また、素敵な画像:P – soulshined

0

最良の結果を得るために、幅と高さの横にマイナスの数を変更することができますありがとう、ハンス。しかし、私はまた、滑らかな外観が必要です。私はこの問題についていくつかの研究をしましたが、解決策を見つけることができませんでした。それから、私はそれを自分自身でやろうとしました。多分誰かがそれを必要とします。

protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     using (GraphicsPath gp = new GraphicsPath()) 
     { 
      gp.AddEllipse(0, 0, this.Width - 1, this.Height - 1); 
      Region = new Region(gp); 
      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; 
      e.Graphics.DrawEllipse(new Pen(new SolidBrush(this.BackColor), 1), 0, 0, this.Width - 1, this.Height - 1); 
     } 
    } 
関連する問題