如何将图像添加到标签? [英] How do I add an image to a label?

查看:185
本文介绍了如何将图像添加到标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的标签上添加图片,但我找不到解决方法如何做到这一点。
我正在尝试使用这个:

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

我不知道即使我使用这个权利,我只是从别人的项目中复制了这个我们在上课时做了什么。
无论如何,我试图创建 Image img = Image.FromFile(YourFile.bmp); 但我不知道为什么, .FromFile 对我不起作用。
你们中的任何人都有另一种方式将标签作为图片(背景)并帮助新手做到这一点? :D

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

抛出异常:

Error 1 'System.Windows.Controls.Image' does not contain a definition for 'FromFile.


推荐答案

这对我有用:

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)

这篇关于如何将图像添加到标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆