获取要在自定义ButtonImage控件c中显示的图像

获取要在自定义ButtonImage控件c中显示的图像,image,button,Image,Button,我是c语言的新手,我正在尝试创建一个ButtonImage控件。我已经设法让大部分代码正常工作,包括文本对齐和图像资源选择,但图像不会显示。我尝试过添加TextImageRelation属性以及ImageAlignment属性,但没有效果,甚至不确定是否所有操作都正确。我花了几个小时搜索MSDN和互联网,请帮助,tnx。代码如下: namespace ImageButton { //[System.ComponentModel.DefaultBindingProperty("Button

我是c语言的新手,我正在尝试创建一个ButtonImage控件。我已经设法让大部分代码正常工作,包括文本对齐和图像资源选择,但图像不会显示。我尝试过添加TextImageRelation属性以及ImageAlignment属性,但没有效果,甚至不确定是否所有操作都正确。我花了几个小时搜索MSDN和互联网,请帮助,tnx。代码如下:

namespace ImageButton
{
    //[System.ComponentModel.DefaultBindingProperty("ButtonText")]
    public partial class ImageButton : UserControl
    {
        private String name = "btn1";
        private String btnText = "Button1";
        private TextImageRelation textImage = TextImageRelation.Overlay;
        private ContentAlignment alignmentValue = ContentAlignment.MiddleRight;
        private ContentAlignment imageAlignmentValue = ContentAlignment.MiddleLeft;

        public ImageButton()
        {
            InitializeComponent();
        }
        //
        // Properties
        //
        [Description("Sets the Text Label"),
         Category("Custom")]
        public String ButtonText
        {
            get
            {
                return btnText;
            }
            set
            {
                btnText = value;
                btn1.Text = btnText;
            }
        }
        [Description("Sets the Button Image"),
         Category("Custom")]
        public Image Image
        {
            get;
            set;
        }
        [Description("Specifies the relationship of text to Image."),
         Category("Custom")]
        public TextImageRelation TextImageRelation { 
            get{
                return textImage;
            }
            set{
                textImage =value;
                btn1.TextImageRelation = textImage;
            }
        }
        [Category("Custom"),
         Description("Specifies the alignment of text.")]
        public ContentAlignment TextAlignment
        {
            get
            {
                return alignmentValue;
            }
            set
            {
                alignmentValue = value;
                btn1.TextAlign = alignmentValue;
                //Invalidate();
            }
        }
        [Category("Custom"),
         Description("Specifies the alignment of text.")]
        public ContentAlignment ImageAlignment
        {
            get
            {
                return imageAlignmentValue;
            }
            set
            {
                imageAlignmentValue = value;
                btn1.ImageAlign = imageAlignmentValue;
            }
        }    
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            StringFormat style = new StringFormat();
            style.Alignment = StringAlignment.Far;
            switch (alignmentValue)
            {
                case ContentAlignment.MiddleLeft:
                    style.Alignment = StringAlignment.Near;
                    break;
                case ContentAlignment.MiddleRight:
                    style.Alignment = StringAlignment.Far;
                    break;
                case ContentAlignment.MiddleCenter:
                    style.Alignment = StringAlignment.Center;
                    break;
            }
            // Call the DrawString method of the System.Drawing class to write   
            // text. Text and ClientRectangle are properties inherited from
            // Control.
            e.Graphics.DrawString(
                Text,
                Font,
                new SolidBrush(ForeColor),
                ClientRectangle, style);
        }
    }
}
已解决:

而不是:

    public Image Image{ get; set;}
正如其他ppl所建议的,我使用

    public Image Image{
        get { return image; }
        set
        {
            image = value;
            btn1.Image = image;
        }
    }