Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

c# - How do I add an image to a label?

I have to add image to my label, but I can't find solution how to do this. I'm trying by use this:

        InitializeComponent();
        url = Directory.GetCurrentDirectory() + @"/Cards/cardSkin.png";
        mylabel.Background = new ImageBrush(new BitmapImage(new Uri(url)));

I don't know even if I'm using this right, I just copied this from others project what we did with class. Anyway, I tried to create Image img = Image.FromFile("YourFile.bmp"); but I don't why, .FromFile wasn't working for me. Anyone of you guys have the other way to make label as picture(background) and help newbie do this? :D

Thrown Exception:

Error 1 'System.Windows.Controls.Image' does not contain a definition for 'FromFile.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...