【问题标题】:How can I correctly align an image to the right edge of a "Label"?如何正确地将图像与“标签”的右边缘对齐?
【发布时间】:2014-07-18 11:44:31
【问题描述】:

使用标准标签,我将图像对齐到右侧。 但是有一个我无法删除的小缩进(标记为红色)。

所以我的问题是:有没有一种简单的方法可以将图像正确对齐/捕捉到标签的右边缘?还是我需要编辑标签绘制方法以便手动绘制图像?

有问题的标签位于“面板”内,以下是我的代码:

    label1.BackColor = System.Drawing.Color.Red;
    label1.Image = global::TestProject.Properties.Resources.Header;
    label1.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
    label1.Size = new System.Drawing.Size(200, 40);

【问题讨论】:

    标签: c# image alignment label


    【解决方案1】:

    简单的答案似乎是:不,没有简单的方法可以正确对齐图像。

    “PictureBox”似乎是处理图像的首选组件,但是可以通过编辑标签的绘制方法正确对齐图像:

    label1.Paint += new System.Windows.Forms.PaintEventHandler(this.label1_Paint);
    

    以及绘制方法:

        private void label1_Paint(object sender, PaintEventArgs e)
        {
            Image image = global::TestProject.Properties.Resources.Header;
            Graphics g = e.Graphics;
            //Align to far right: label width - image width
            g.DrawImage(image, this.label1.Width - image.Width, 0);
        }
    

    【讨论】:

      猜你喜欢
      • 2014-10-18
      • 2015-11-28
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      相关资源
      最近更新 更多