【问题标题】:C# Adding Image to a LabelC# 将图像添加到标签
【发布时间】:2014-05-27 13:44:50
【问题描述】:

现在我必须使用 Visual C# 2010 开发 WindowsForm,我需要做的是在标签上将它们变成图像。 我已将项目/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);

我只需要扩展名为 (*.jpg) 的文件

有人可以帮我吗?

【问题讨论】:

  • 什么意思?您的代码应该可以工作。
  • 您的代码运行良好..
  • 但是我的图片大小和标签大小一样>

标签: c# label


【解决方案1】:

由于您的图像在“图像”文件夹中,因此您必须修改此行

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

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

注意:原始行将搜索“调试”文件夹中的文件,您的程序可执行文件 (.exe) 文件所在的位置。

【讨论】:

  • 它的工作,但是,为什么图像的大小就像标签一样?不像真实图像的大小?
  • 除非其他代码正在改变标签的大小,否则行“lblImage.Size = new Size(img.Width, img.Height);”应该将标签的大小更改为真实图像的大小。请看一看。否则,请附上整个代码,包括任何事件(表单加载、按钮点击等)?
  • ahhhh,已经成功了,为什么?因为在标签的属性中,autosize=true,我只是把它设为假..
【解决方案2】:
【解决方案3】:

这对我有用:

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

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 2012-10-02
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多