0

Now i have to developing a WindowsForm using Visual C# 2010, What I need to be able to do is on a label make their be an image. I have got the images included in the project/bin/Debug/ in a folder named "images"

Image img = Image.FromFile("PR001.jpg");
Label lblImage = new Label();
lblImage.Parent = this;
lblImage.Image = img;
lblImage.Size = new Size(img.Width, img.Height);

i need only file with extension (*.jpg)

can someone help me ?

3 Answers3

0

You should take a look at the Label.BackgroundImage property.

And this links describes what you are looking for:

http://www.c-sharpcorner.com/uploadfile/mahesh/label-in-C-Sharp/

http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/AddimagetoLabel.htm

HABJAN
  • 9,212
  • 3
  • 35
  • 59
0

Since your images are in the "images" folder, you have to modify this line

Image img = Image.FromFile("PR001.jpg");

to

Image img = Image.FromFile("images/PR001.jpg");

Note: The original line would search the file in "debug" folder, where your program executable (.exe) file is located.

neutrino2039
  • 126
  • 10
  • its work, but, why the size of image just like label ? not like real image's size ? – Johanes Kevin L May 27 '14 at 14:08
  • Unless some other code is changing the size of the label, the line "lblImage.Size = new Size(img.Width, img.Height);" should change the size of the label to the size of the real image. Please have a look. Otherwise, please attach the whole code including any events (form load, button click etc.)? – neutrino2039 May 27 '14 at 14:15
  • ahhhh, its already success, why ? because in the properties of label, autosize=true, and i just make it to false.. – Johanes Kevin L May 27 '14 at 14:22
0

This works for me:

Label ilabel = new Label(); // create a label
Image i = Image.FromFile("image.png"); // read in image
ilabel.Size = new Size(i.Width, i.Height); //set label to correct size
ilabel.Image = i; // put image on label
this.Controls.Add(ilabel); // add label to container (a form, for instance)

https://stackoverflow.com/a/16888310/470868

Engineer
  • 834
  • 1
  • 13
  • 27