简体   繁体   English

C# 将图像添加到标签

[英]C# Adding Image to a Label

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.现在我必须使用 Visual C# 2010 开发 WindowsForm,我需要能够做的是在标签上使它们成为图像。 I have got the images included in the project/bin/Debug/ in a folder named "images"我已将项目/bin/Debug/ 中的图像包含在名为“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)我只需要带扩展名的文件 (*.jpg)

can someone help me ?有人能帮我吗 ?

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.注意:原始行将搜索“debug”文件夹中的文件,您的程序可执行文件 (.exe) 位于该文件夹中。

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 https://stackoverflow.com/a/16888310/470868

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM