标签图像模式拉伸
Label Image mode to stretch
我写这段代码是为了添加我的 Labels
:
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]);
}
}
}
Image
应该伸展到 Label
大小。我该怎么做?
如果您使用的是 WinForms,请尝试以下操作:
labels[counter].Size =
new Size(labels[counter].Image.Width, labels[counter].Image.Height);
显示和操作图像和文本的能力在 Winforms
控件中以相当疯狂的方式分布。
- A
Label
无法拉伸其 Image
。
- 一个
PictureBox
和一个Panel
可以但是他们没有显示他们的Text
Button
可以两者兼顾,但无论您如何设计它,它始终是 Button
..
因此,要获得组合,您需要或者所有者绘制一些东西:
DrawImage
在重载中以获得图像的正确大小,然后将 Image
添加到 Label
- 或
DrawString
将 Text
放到 Panel
上以与图片一起显示
或您可以将两个控件与正确的能力结合起来:
您可以创建一个 Panel
并将其 BackgroundImage
设置为您的图像及其 BackgroundImageLayout=Stretch
。然后您可以添加您的 Label
并将其文本设置为 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;
根据需要设置边框..
还有一个选项:如果 所有 Images
应该具有相同的大小,如果它是 256x256
像素或更少,您可以将它们添加到 ImageList
。这将以一种非常简单的方式将它们拉伸到 ImageList.ImageSize
,您可以将它们添加到您的 Label
..
很简单:
VB
Label1.Image = New Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size)
C#
Label1.Image = new Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size);
我写这段代码是为了添加我的 Labels
:
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]);
}
}
}
Image
应该伸展到 Label
大小。我该怎么做?
如果您使用的是 WinForms,请尝试以下操作:
labels[counter].Size =
new Size(labels[counter].Image.Width, labels[counter].Image.Height);
显示和操作图像和文本的能力在 Winforms
控件中以相当疯狂的方式分布。
- A
Label
无法拉伸其Image
。 - 一个
PictureBox
和一个Panel
可以但是他们没有显示他们的Text
Button
可以两者兼顾,但无论您如何设计它,它始终是Button
..
因此,要获得组合,您需要或者所有者绘制一些东西:
DrawImage
在重载中以获得图像的正确大小,然后将Image
添加到Label
- 或
DrawString
将Text
放到Panel
上以与图片一起显示
或您可以将两个控件与正确的能力结合起来:
您可以创建一个 Panel
并将其 BackgroundImage
设置为您的图像及其 BackgroundImageLayout=Stretch
。然后您可以添加您的 Label
并将其文本设置为 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;
根据需要设置边框..
还有一个选项:如果 所有 Images
应该具有相同的大小,如果它是 256x256
像素或更少,您可以将它们添加到 ImageList
。这将以一种非常简单的方式将它们拉伸到 ImageList.ImageSize
,您可以将它们添加到您的 Label
..
很简单:
VB
Label1.Image = New Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size)
C#
Label1.Image = new Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size);