-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularPictureBox.cs
137 lines (121 loc) · 4.93 KB
/
CircularPictureBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Drawing;
namespace Instagram
{
public class CircularPictureBox : PictureBox
{
private Bitmap bitmap;
private Color borderColor;
private int penSize;
private Color AlphaColor = Color.FromArgb(0, 255, 255, 255);
private bool enhancedBuffering;
public CircularPictureBox()
{
InitializeComponent();
this.SetStyle(ControlStyles.SupportsTransparentBackColor |
ControlStyles.ResizeRedraw |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer, true);
}
private void InitializeComponent()
{
this.enhancedBuffering = true;
this.bitmap = null;
this.borderColor = Color.DodgerBlue;
this.penSize = 3;
this.BackColor = AlphaColor;
this.SizeMode = PictureBoxSizeMode.StretchImage;
this.Size = new Size(100, 100);
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.CompositingMode = CompositingMode.SourceOver;
//pe.Graphics.CompositingQuality = CompositingQuality.HighQuality;
//pe.Graphics.InterpolationMode = InterpolationMode.Bicubic;
pe.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if (this.Region != null)
pe.Graphics.Clip = this.Region;
Rectangle rect = this.ClientRectangle;
if (this.bitmap != null)
pe.Graphics.DrawImage(this.bitmap, rect);
rect.Inflate(-this.penSize / 2 + 1, -this.penSize / 2 + 1);
pe.Graphics.DrawEllipse(new Pen(this.borderColor, this.penSize), rect);
}
protected override void OnResize(EventArgs e)
{
using (GraphicsPath gpath = new GraphicsPath())
{
gpath.AddEllipse(this.ClientRectangle);
gpath.CloseFigure();
using (Region region = new Region(gpath))
this.Region = region.Clone();
}
}
//[Description("Gets or Sets the Image diplayed by the control."), Category("Appearance")]
//[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public Bitmap Bitmap
{
get { return this.bitmap; }
set { this.bitmap = value; this.Invalidate(); }
}
//[Description("Gets or Sets the size of the Border"), Category("Behavior")]
//[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public int BorderSize
{
get { return this.penSize; }
set { this.penSize = value; this.Invalidate(); }
}
//[Description("Gets or Sets the Color of Border drawn around the Image.")]
//[Category("Appearance")]
//[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public Color BorderColor
{
get { return this.borderColor; }
set { this.borderColor = value; this.Invalidate(); }
}
//[Description("Enables or disables the control OptimizedDoubleBuffering feature")]
//[Category("Useful Features")] //<= "Useful feature" is a custom category
//[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public bool EnhancedBuffering
{
get { return this.enhancedBuffering; }
set
{
this.enhancedBuffering = value;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, value);
}
}
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
//[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public new Image ErrorImage
{
get { return null; }
set { base.ErrorImage = null; }
}
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
//[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public new Image InitialImage
{
get { return null; }
set { base.InitialImage = null; }
}
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
//[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public new Image BackgroundImage
{
get { return null; }
set { base.BackgroundImage = null; }
}
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
//[EditorBrowsable(EditorBrowsableState.Never), BrowsableAttribute(false)]
public new Image Image
{
get { return null; }
set { base.Image = null; }
}
}
}