├── LICENSE ├── README.md └── RJCircularPictureBox.cs /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Circular-Picture-Box-CSharp 2 | How to create a Circular Picture Box - Border Gradient color + Styles with C# and Windows Forms 3 |

VIDEO:

4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /RJCircularPictureBox.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | using System.Windows.Forms; 8 | using System.Drawing; 9 | using System.Drawing.Drawing2D; 10 | using System.ComponentModel; 11 | 12 | namespace CustomControls.RJControls 13 | { 14 | class RJCircularPictureBox : PictureBox 15 | { 16 | //Fields 17 | private int borderSize = 2; 18 | private Color borderColor = Color.RoyalBlue; 19 | private Color borderColor2 = Color.HotPink; 20 | private DashStyle borderLineStyle = DashStyle.Solid; 21 | private DashCap borderCapStyle = DashCap.Flat; 22 | private float gradientAngle = 50F; 23 | 24 | //Constructor 25 | public RJCircularPictureBox() 26 | { 27 | this.Size = new Size(100,100); 28 | this.SizeMode = PictureBoxSizeMode.StretchImage; 29 | } 30 | 31 | //Properties 32 | [Category("RJ Code Advance")] 33 | public int BorderSize 34 | { 35 | get 36 | { 37 | return borderSize; 38 | } 39 | 40 | set 41 | { 42 | borderSize = value; 43 | this.Invalidate(); 44 | } 45 | } 46 | 47 | [Category("RJ Code Advance")] 48 | public Color BorderColor 49 | { 50 | get 51 | { 52 | return borderColor; 53 | } 54 | 55 | set 56 | { 57 | borderColor = value; 58 | this.Invalidate(); 59 | } 60 | } 61 | 62 | [Category("RJ Code Advance")] 63 | public Color BorderColor2 64 | { 65 | get 66 | { 67 | return borderColor2; 68 | } 69 | 70 | set 71 | { 72 | borderColor2 = value; 73 | this.Invalidate(); 74 | } 75 | } 76 | 77 | [Category("RJ Code Advance")] 78 | public DashStyle BorderLineStyle 79 | { 80 | get 81 | { 82 | return borderLineStyle; 83 | } 84 | 85 | set 86 | { 87 | borderLineStyle = value; 88 | this.Invalidate(); 89 | } 90 | } 91 | 92 | [Category("RJ Code Advance")] 93 | public DashCap BorderCapStyle 94 | { 95 | get 96 | { 97 | return borderCapStyle; 98 | } 99 | 100 | set 101 | { 102 | borderCapStyle = value; 103 | this.Invalidate(); 104 | } 105 | } 106 | 107 | [Category("RJ Code Advance")] 108 | public float GradientAngle 109 | { 110 | get 111 | { 112 | return gradientAngle; 113 | } 114 | 115 | set 116 | { 117 | gradientAngle = value; 118 | this.Invalidate(); 119 | } 120 | } 121 | 122 | //Overridden methods 123 | protected override void OnResize(EventArgs e) 124 | { 125 | base.OnResize(e); 126 | this.Size = new Size(this.Width,this.Width); 127 | } 128 | 129 | protected override void OnPaint(PaintEventArgs pe) 130 | { 131 | base.OnPaint(pe); 132 | //Fields 133 | var graph = pe.Graphics; 134 | var rectContourSmooth = Rectangle.Inflate(this.ClientRectangle, -1, -1); 135 | var rectBorder = Rectangle.Inflate(rectContourSmooth,-borderSize, -borderSize); 136 | var smoothSize = borderSize > 0 ? borderSize * 3 : 1; 137 | using (var borderGColor = new LinearGradientBrush(rectBorder, borderColor, borderColor2, gradientAngle)) 138 | using (var pathRegion = new GraphicsPath()) 139 | using (var penSmooth = new Pen(this.Parent.BackColor, smoothSize)) 140 | using (var penBorder = new Pen(borderGColor, borderSize)) 141 | { 142 | graph.SmoothingMode = SmoothingMode.AntiAlias; 143 | penBorder.DashStyle = borderLineStyle; 144 | penBorder.DashCap = borderCapStyle; 145 | pathRegion.AddEllipse(rectContourSmooth); 146 | //Set rounded region 147 | this.Region = new Region(pathRegion); 148 | 149 | //Drawing 150 | graph.DrawEllipse(penSmooth,rectContourSmooth);//Draw contour smoothing 151 | if (borderSize > 0) //Draw border 152 | graph.DrawEllipse(penBorder,rectBorder); 153 | } 154 | 155 | } 156 | } 157 | } 158 | --------------------------------------------------------------------------------