jlimited
jlimited

Reputation: 685

Resize System Icon for ErrorProvider

I am trying to resize a SystemIcon for use within a ErrorProvider.

  Dim WarnProvider As New ErrorProvider
  WarnProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink
  WarnProvider.Icon = SystemIcons.Information.Clone()
  WarnProvider.Icon.Size = New Size(16,16)

But the SystemIcons has the size set as a read only property.

Been messing with it for the past hour and have not found any good methods to make this work.

Can someone help?

Thanks

Upvotes: 1

Views: 3098

Answers (3)

Dan
Dan

Reputation: 61

I changed Drew's solution a little to be an extension method for the errorprovider:

public static ErrorProvider SetIcon(this ErrorProvider errorProvider, Icon icon, Size size)
    {
        Bitmap bitmap = new Bitmap(size.Width, size.Height);
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(icon.ToBitmap(), new Rectangle(Point.Empty, size));
        }
        errorProvider.Icon = Icon.FromHandle(bitmap.GetHicon());
        return errorProvider;
    }

Then it's can be used like so:

ErrorProvider ep = new ErrorProvider();
ep.SetIcon(SystemIcons.Asterisk, new Size(16,16));

Upvotes: 0

Drew Chapin
Drew Chapin

Reputation: 7989

I was looking for the a method to do this as well, and came across this post. Here's what I ended up doing to resolve the issue.

I created a global static method to resize an icon.

public static class Global
{
    public static Icon ResizeIcon( Icon icon, Size size )
    {
        Bitmap bitmap = new Bitmap(size.Width,size.Height);
        using( Graphics g = Graphics.FromImage(bitmap) )
        {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(icon.ToBitmap(), new Rectangle(Point.Empty,size));
        }
        return Icon.FromHandle(bitmap.GetHicon());
    }
}

Then I applied the icon in the constructor of the form after InitializeComponent() was called.

public SpecificationsDialog( int pid )
{
    InitializeComponent();
    warningProvider1.Icon = Global.ResizeIcon(SystemIcons.Warning,SystemInformation.SmallIconSize);
}

Upvotes: 7

PabloInNZ
PabloInNZ

Reputation: 555

I was looking for the same thing and found the answer elsewhere, so I'll post here http://www.codeproject.com/Questions/242780/error-provider-problem

WarnProvider.Icon = new Icon (SystemIcons.Warning, 16, 16);

or

WarnProvider.Icon = new Icon (WarnProvider.Icon, 16, 16);

Upvotes: 0

Related Questions