加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 标签要拉伸的图像模式

发布时间:2020-12-15 23:24:00 所属栏目:百科 来源:网络整理
导读:我写了这段代码来添加我的标签: JArray a = JArray.Parse(temp);Label[] labels = new Label[100];foreach (JObject o in a.ChildrenJObject()){ foreach (JProperty p in o.Properties()) { string name = p.Name; string value = p.Value.ToString(); if
我写了这段代码来添加我的标签:

JArray a = JArray.Parse(temp);
Label[] labels = new Label[100];
foreach (JObject o in a.Children<JObject>())
{
    foreach (JProperty p in o.Properties())
    {
        string name = p.Name;
        string value = p.Value.ToString();
        if (name == "name")
        {
            labels[counter] = new Label();
            //Image i = Image.FromFile("item.jpg");
            labels[counter].Text = value;
            labels[counter].Image =Image.FromFile("item.jpg");
            //labels[counter].Image
            //labels[counter].BackColor = Color.Blue;
            labels[counter].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            labels[counter].Top = height;      
            height += 50;
            Controls.Add(labels[counter]);
        }
    }
}

图像应拉伸到标签尺寸.我怎样才能做到这一点?

解决方法

显示和操作图像和文本的能力在Winforms控件中以相当狂野的方式展开.

>标签无法拉伸其图像.
> PictureBox和Panel可以但不显示文本
>按钮可以同时执行,但无论您如何设置样式,它都将始终为按钮.

因此,要获得组合,您需要拥有者绘制一些东西:

>在重载中绘制DrawImage以获得正确的图像大小,然后将图像添加到标签
>或者将文本绘制到面板上以将其显示在图像旁边

或者你可以将两个控制与正确的能力结合起来:

您可以创建一个Panel并将其BackgroundImage设置为您的Image及其BackgroundImageLayout = Stretch.然后,您可以将Label及其Text集添加到Panel的控件集合中:

// preparation for testing:
Image image = Image.FromFile("D:stop32.png");
Size size = new Size(77,77);

// create the combined control
// I assume your Label is already there
Panel pan = new Panel();
pan.Size = size;
// or,since the Label has the right size:
pan.Size = label.Size;  // use Clientsize,if borders are involved!
pan.BackgroundImage = image;
pan.BackgroundImageLayout = ImageLayout.Stretch;
label.Parent = pan;  // add the Label to the Panel
label.Location = Point.Empty;
label.Text = "TEXT";
label.BackColor = Color.Transparent;

// add to (e.g.) the form
pan.Parent = this;

根据需要设置边框..

还有一个选项:如果所有图像应具有相同的大小,如果它是256×256像素或更少,您可以将它们添加到ImageList.这将以非常简单的方式将它们拉伸到ImageList.ImageSize,您可以将它们添加到Label中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读