7

I wrote this code to add my 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]);
        }
    }
}

The Image should stretch to the Label Size. How can I do this?

TaW
  • 53,122
  • 8
  • 69
  • 111
elnaz irani
  • 277
  • 1
  • 3
  • 11

4 Answers4

13

The abilities to show and manipulate images and text are spread out in a rather wild fashion among Winforms controls.

  • A Label can not stretch its Image.
  • A PictureBox and a Panel can but they don't show their Text
  • A Button can do both but will always be a Button, no matter how you style it..

So to get a combination you will need to either owner-draw something:

  • DrawImage in an overload to get the right size of the image, then add Image to Label
  • Or DrawString the Text onto a Panel to show it alongside the Image

or you could combine two controls with the right abilities:

You can create a Panel and set its BackgroundImage to your Image and its BackgroundImageLayout=Stretch. Then you can add your Label with its Text set to the Panel's controls collection:

// 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;

Set Borders as you like..

One more option: If all Images should have the same Size and if it is 256x256 pixels or less you could add them to an ImageList. This will stretch them to the ImageList.ImageSize in a very simple way and you can add them to your Label..

TaW
  • 53,122
  • 8
  • 69
  • 111
4

Very simple:

VB

Label1.Image = New Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size)

C#

Label1.Image = new Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size);
Zibri
  • 9,096
  • 3
  • 52
  • 44
0

If you are using WinForms you try try below:

labels[counter].Size = 
    new Size(labels[counter].Image.Width, labels[counter].Image.Height);
Jack
  • 350
  • 2
  • 15
  • Jack with your code my label size change to image size,but i want my image size to labels size – elnaz irani May 01 '15 at 14:14
  • ahh, if you need this other way round then please resize your image before assigning to label. – Jack May 01 '15 at 14:20
  • how can i resize image? – elnaz irani May 01 '15 at 14:22
  • Bitmap ResizeBmp(Image.FromFile("item.jpg"), labels[counter].Width, labels[counter].Height ) { Bitmap result = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(result)) g.DrawImage(sourceBMP, 0, 0, width, height); return result; } – Jack May 01 '15 at 14:31
  • @elnazirani See: http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp – Anthony May 01 '15 at 17:29
0

This works perfect for me:

  • Just set the image in design mode (don't use imagelist) use the "Image" option

  • if we have label1 as the label where we want the image, put the next line inside the constructor:

    label1.Image = new Bitmap(label1.Image, label1.Size);

I was trying the solution of Zibri, but deform the image in my case.