WinForms,picturebox的图像重叠自定义绘制的图纸

时间:2017-07-04 03:09:07

标签: c# winforms gdi+

我有一个带有图片框的自定义用户控件。然后,在Paint事件中的控件上绘制一个圆圈。

var size = TextRenderer.MeasureText(this.UnreadCount.ToString(), lblDisplayname.Font);
var rec = new Rectangle(0, 0, size.Width, size.Width);
var smallFont = new Font(lblDisplayname.Font.Name, lblDisplayname.Font.Size - 1);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
format.LineAlignment = StringAlignment.Center;

using (LinearGradientBrush b = new LinearGradientBrush(
    rec,
    Color.FromArgb(242, 37, 37),
    Color.FromArgb(178, 30, 30),
    45F))
{                    
    e.Graphics.FillEllipse(b, rec);                    
    e.Graphics.DrawString(this.UnreadCount.ToString(), smallFont, Brushes.White, rec, format);
}

然后,在实现的后面,我设置了控件图片框的图像。

MyControl ctrl = new MyControl();
ctrl.picImage.Image = Image.FromFile(imagePath);
ctrl.Refresh();

问题在于:图片框的图像与绘制的圆圈重叠。 current issue 要求是:需要圆圈才能显示图像的完全重叠。可能导致我的问题的原因是什么?

1 个答案:

答案 0 :(得分:0)

以下功能将返回符合您要求的用户控件,发布代码以便您了解父子关系。

要么必须在图像容器的顶部或图像顶部的另一个透明容器上绘制图形。

以下样本有2个面板,panel1和panel2,其中面板2是面板的子节点。 Panel1是背景图像,面板2有图形。希望它有帮助。

    private UserControl create_MyControl( string  filenamepath)
    {
        //Create User Control

        UserControl MyControl = new UserControl();
        //Mention the size of control
        MyControl.Size = new Size(100, 100);
        //Create a panel to hold the background image that you wanted in the picture box
        Panel panel1 = new Panel();
        //dock the panel1 to fill the control background
        panel1.Dock = DockStyle.Fill;
        MyControl.Controls.Add(panel1);
        //Create another panel as overlay for panel1
        Panel panel2 = new Panel();
        //dock the panel2 to fill the panel1;
        panel2.Dock = DockStyle.Fill;
        //Add panel2 as child of panel1
        panel1.Controls.Add(panel2);
        //Set panel2 background as transparent 
        panel2.BackColor = Color.Transparent;
        // To replicate the variables that you have!
        Label lblDisplayname = new Label();
        lblDisplayname.Font = new Font("Arial", 24, FontStyle.Regular);
        lblDisplayname.Size = panel2.Size;
        lblDisplayname.TextAlign = ContentAlignment.TopCenter;
        lblDisplayname.Text = "25";
        lblDisplayname.Dock = DockStyle.Fill;
        panel2.Controls.Add(lblDisplayname);

        panel1.BackgroundImage = Image.FromFile(filenamepath);
        panel1.BackgroundImageLayout = ImageLayout.Stretch;

        //In Panel2 paint event put your code for stuff
        panel2.Paint += (s, e) =>
        {

            var size = TextRenderer.MeasureText("Hello", lblDisplayname.Font);
            var rec = new Rectangle(0, 0, size.Width, size.Width);

            var smallFont = new Font(lblDisplayname.Font.Name, lblDisplayname.Font.Size - 1);
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
            format.LineAlignment = StringAlignment.Center;

            using (System.Drawing.Drawing2D.LinearGradientBrush b = new System.Drawing.Drawing2D.LinearGradientBrush(
                rec,
                Color.FromArgb(242, 37, 37),
                Color.FromArgb(178, 30, 30),
                45F))
            {
                e.Graphics.FillEllipse(b, rec);
                e.Graphics.DrawString("Hello", smallFont, Brushes.White, rec, format);
            }
        };

        return MyControl;
    }