Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 要拉伸的标签图像模式_C#_Image_Winforms_Label - Fatal编程技术网

C# 要拉伸的标签图像模式

C# 要拉伸的标签图像模式,c#,image,winforms,label,C#,Image,Winforms,Label,我编写此代码是为了添加我的标签: 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.ToStr

我编写此代码是为了添加我的
标签

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]);
        }
    }
}
JArray a=JArray.Parse(temp);
标签[]标签=新标签[100];
foreach(a.Children()中的JObject o)
{
foreach(o.Properties()中的JProperty p)
{
字符串名称=p.名称;
字符串值=p.value.ToString();
如果(名称=“名称”)
{
标签[计数器]=新标签();
//Image i=Image.FromFile(“item.jpg”);
标签[计数器]。文本=值;
标签[counter].Image=Image.FromFile(“item.jpg”);
//标签[计数器]。图像
//标签[计数器]。背景色=颜色。蓝色;
标签[计数器].TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
标签[计数器]。顶部=高度;
高度+=50;
添加(标签[计数器]);
}
}
}

图像
应延伸至
标签
大小。如何执行此操作?

如果您正在使用WinForms,请尝试以下操作:

labels[counter].Size = 
    new Size(labels[counter].Image.Width, labels[counter].Image.Height);

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

  • 标签
    无法拉伸其
    图像
  • PictureBox
    面板
    可以,但它们不显示其
    文本
  • 按钮
    可以同时执行这两项功能,但无论您如何设计,它始终是一个
    按钮
因此,要获得组合,您需要所有者绘制一些东西:

  • DrawImage
    在重载中获取图像的正确大小,然后将
    image
    添加到
    Label
  • 抽绳
    文本
    放在
    面板上
    与图像一起显示
您可以将两个具有正确功能的控件组合在一起:

您可以创建一个
面板
,并将其
背景图像
设置为您的图像及其
背景图像布局=拉伸
。然后,您可以将文本设置为
面板的
控件集合的
标签添加到

// 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;
按你的喜好设置边框

还有一个选项:如果所有的
图像
应该具有相同的大小,如果是
256x256
像素或更小,您可以将它们添加到
图像列表
。这将以非常简单的方式将它们扩展到
图像列表.ImageSize
,您可以将它们添加到
标签中。

非常简单:

VB

C#


用代码将“我的标签大小”更改为“图像大小”,但我希望我的图像大小为“标签大小”,如果需要,请在指定给“标签”之前调整图像大小。如何调整图像大小?位图大小bmp(image.FromFile(“item.jpg”)、标签[counter]。宽度、标签[counter]。高度){位图结果=新位图(宽度,高度);使用(Graphics g=Graphics.FromImage(result))g.DrawImage(sourceBMP,0,0,width,height);返回结果;}@elnazirani请参见:
Label1.Image = New Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size)
Label1.Image = new Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size);