我如何将一个图像到我的PictureBox使用ImageLocation? [英] How do I put an image into my picturebox using ImageLocation?

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

问题描述

我已经创建了我的面板内的图片框,我想用本地的图像,以填补它。

I have created a picturebox within my panel and I would like to fill it up with an image locally.

这是我迄今所做及以下代码是我的面板创建代码只是一小部分。

this is what I have done so far and the following code is just a small part of my panel creation code.

PictureBox picture = new PictureBox
        {
            Name = "pictureBox",
            Size = new Size(100, 50),
            Location = new Point(14, 17)
        };
        p.Controls.Add(picture);
        picture.ImageLocation = @"..\Image\1.jpg";



它做的工作,但不能完全作为后我启动我的C#Windows窗体应用程序,它强制显示白色的小盒子中间,而不是像一个红色的叉。任何想法如何解决呢?

It does works but not fully as after I have launch my c# windows form application, it jus show a small white box with a red cross in the middle instead of the image. Any idea how to solve it?

推荐答案

使用设定图像 picture.ImageLocation()工作正常,但您使用的是相对路径。检查你的路径对 .exe文件它建成后。

Setting the image using picture.ImageLocation() works fine, but you are using a relative path. Check your path against the location of the .exe after it is built.

例如,如果你的<$的位置C $ C> .exe文件位于:

<项目文件夹> /斌/调试/应用程序。 exe文件

图片将不得不为:

<项目文件夹> /bin/Image/1.jpg

当然,你可以只设置在设计时的图像(图片财产上的图片框属性表)。

Of course, you could just set the image at design-time (the Image property on the PictureBox property sheet).

如果你必须在运行时设置它,一种方法,以确保您知道图像的位置是将图像文件添加到您的项目。例如,添加一个新的文件夹到您的项目,将其命名为图片。用鼠标右键单击该文件夹,选择添加现有项目,并浏览到您的图像(确保文件过滤器设置为显示图像文件)。添加映像后,在属性表设置复制到输出目录复制如果新

If you must set it at run-time, one way to make sure you know the location of the image is to add the image file to your project. For example, add a new folder to your project, name it Image. Right-click the folder, choose "Add existing item" and browse to your image (be sure the file filter is set to show image files). After adding the image, in the property sheet set the Copy to Output Directory to Copy if newer.

在这一点上,当你构建应用程序,您可以使用的图像文件将被复制

At this point the image file will be copied when you build the application and you can use

picture.ImageLocation = @"Image\1.jpg"; 

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

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