0

I used an advise from this topic: Rounded edges in picturebox C# to make my picturebox edges rounded. So, I claimed a new control using this code:

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);
        }
    }
}

But there is my question: how could I add antialias to my control? I read some topics like this: Possible to have anti-aliasing when drawing a clipped image? but there is just drawing functions are used, and I need to implement antialias in my own control, which parent is PictureBox.

I also tried to override OnPaint method to get PaintEventArgs to use the SmoothingMode, but my code sucked and didn`t work properly. So I deleted it and this is what I have now:

protected override void OnPaint(PaintEventArgs pe)
{
    base.OnPaint(pe);

    using (var gp = new GraphicsPath())
    {
        gp.AddEllipse(new Rectangle(0, 0, this.Width - 1, this.Height - 1));
        this.Region = new Region(gp);
    }
}

What should I add to this method?

Community
  • 1
  • 1
CssHammer
  • 66
  • 7
  • Show us your code that uses `SmoothingMode` –  Oct 31 '15 at 15:55
  • http://pastebin.com/t052Un0d – CssHammer Oct 31 '15 at 16:02
  • but this code makes my control just white and antialiasing just first instance of this control, others are staying the same (omg) – CssHammer Oct 31 '15 at 16:09
  • oh, i was wrong about other instances: all of them become with antialias BUT just white color – CssHammer Oct 31 '15 at 16:20
  • Antialiasing works by mixing colors. But a pixel either belongs to a Region or it doesn't, so, no, I don't think you can do that. – TaW Oct 31 '15 at 16:50
  • Hmm that should work. What I normally do when testing anti-alias settings, is to draw a big X (I don't mean a _text_ "x") in the space provided and take note of any tell-tale "steps". Maybe try that first before drawing a filled ellipse as the latter will be hard to tell –  Oct 31 '15 at 16:53
  • Nope, it works...but randomly, not on all istances – CssHammer Oct 31 '15 at 16:53
  • Micky, to draw the X is not so complicated, but my problem relates to overrided control... and i spectate some strange behavior. For example: at this moment I have 5 such a controls with rounded edges. When I use **SolidBrush brush = new SolidBrush(Color.White)** anti-alias appears only on 2,3 and 5 instances... this is strange, 1 and 4 still rough cutted. And when I use **SolidBrush brush = new SolidBrush(Color.FromArgb(0,0,0,0));** to make the brush transparent - there is no effect at all – CssHammer Oct 31 '15 at 17:12
  • It's unusual that some instances work and some don't. "Transparency" in WinForms is a bit of an illusion (no pun intended) where one can not even set the background color of a form to `Transparent` compared to WPF where it is done correctly. Have you thought about doing away with the `PictureBox` and making a `UserControl` considering you are overriding the `OnPaint()` anyway? –  Nov 01 '15 at 00:49

0 Answers0