IconBox.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3. namespace PaintDotNet
  4. {
  5. internal class IconBox : UserControl
  6. {
  7. private Bitmap renderSurface = null;
  8. private Bitmap icon = null;
  9. public Bitmap Icon
  10. {
  11. get
  12. {
  13. return icon;
  14. }
  15. set
  16. {
  17. if (value == null)
  18. {
  19. value = new Bitmap(1, 1);
  20. using (Graphics g = Graphics.FromImage(value))
  21. {
  22. g.Clear(Color.Transparent);
  23. }
  24. }
  25. icon = value;
  26. if (renderSurface != null)
  27. {
  28. renderSurface.Dispose();
  29. }
  30. renderSurface = null;
  31. Invalidate();
  32. }
  33. }
  34. protected override void OnPaint(PaintEventArgs e)
  35. {
  36. e.Graphics.Clear(this.BackColor);
  37. Rectangle srcBounds = new Rectangle(new Point(0, 0), this.icon.Size);
  38. Rectangle dstBounds = new Rectangle(new Point(0, 0), this.ClientSize);
  39. e.Graphics.DrawImage(this.Icon, dstBounds, srcBounds, GraphicsUnit.Pixel);
  40. base.OnPaint(e);
  41. }
  42. public IconBox()
  43. {
  44. this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
  45. // This call is required by the Windows.Forms Form Designer.
  46. InitializeComponent();
  47. this.ResizeRedraw = true;
  48. }
  49. #region Component Designer generated code
  50. /// <summary>
  51. /// Required method for Designer support - do not modify
  52. /// the contents of this method with the code editor.
  53. /// </summary>
  54. private void InitializeComponent()
  55. {
  56. }
  57. #endregion
  58. }
  59. }