简体   繁体   English

将外部图像分配给Visual Studio 2008(C#)中的Picturebox?

[英]Assign an external image to a Picturebox in Visual Studio 2008 (C#)?

How can I assign an external image into the PictureBox in Visual Studio 2008 ? 如何在Visual Studio 2008中将外部图像分配到PictureBox中?
Typically, When we use ChooseImage in PictureBox , Visual Studio adds the image to the exe file and it causes increasing exe file's volume, I wanna add the image from a directory beside the exe file. 通常,当我们在PictureBox中使用ChooseImage时,Visual Studio将图像添加到exe文件中,这会导致exe文件的容量增加,我想从exe文件旁边的目录中添加图像。
Is it possible in Visual Studio 2008? 在Visual Studio 2008中可以吗?

PS: I don't want add the image with C# code, because VS2008 doesn't show it in developing time. PS:我不希望使用C#代码添加图像,因为VS2008在开发期间没有显示它。

I think you can do this in C# code using the Image.FromFile method. 我认为您可以使用Image.FromFile方法在C#代码中执行此操作。 As long as the C# code is in the initialization of the form / control, it should run at both design time and run time. 只要C#代码处于表单/控件的初始化中,它就应该在设计时和运行时同时运行。

You can get images from Resources 您可以从 Resources获取图像

 
 
 
 
  
  
  pictureBox1.Image = Properties.Resources.[image name];
 
 
  

see How to embed and access resources by using Visual C# 请参见 如何使用Visual C#嵌入和访问资源

Misread the question OP asked to load without using the Resources. 误读了OP要求不使用资源加载的问题。

you will need to do 你需要做

Image.FromFile("c:\\\\image.bmp")

If image if a JPEG then You can cast it to Bitmap 如果图像为JPEG,则可以将其投射到位图

Bitmap myJpeg=(Bitmap) Image.FromFile("myJpegPath");

OR, 要么,

 System.Drawing.Image loadImg = System.Drawing.Image.FromFile("c:\\\\image.JPG"); loadImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); 

You can use Linked resources. 您可以使用链接资源。

When you add a linked resource, the .resx file that stores your project resource information includes only a relative path to the resource file on disk. 添加链接资源时,用于存储项目资源信息的.resx文件仅包含磁盘上资源文件的相对路径。 If you add images, videos, or other complex files as linked resources, you can edit them using a default editor that you associate with that file type in the Resource Designer. 如果您将图像,视频或其他复杂文件添加为链接资源,则可以使用与资源设计器中与该文件类型关联的默认编辑器对其进行编辑。 When you add an embedded resource, the data is stored directly in the project's resource (.resx) file. 添加嵌入式资源时,数据直接存储在项目的资源(.resx)文件中。 Strings can only be stored as embedded resources.embedded resources. 字符串只能存储为嵌入式资源或嵌入式资源。

MSDN on Linked vs. Embedded Resources 有关链接与嵌入式资源的MSDN

PS I however still don't get why would you need it—even if .exe doesn't get bigger, the whole app package would have a bigger size, wouldn't it? PS但是,我仍然不明白为什么会需要它-即使.exe没有变大,整个应用程序包的大小也会变大,不是吗?

Best, 最好,
Dan

If this is WinForms, it's better to just set the ImageLocation of the PictureBox to the path to your image. 如果是WinForms,最好将PictureBox的ImageLocation设置为图像的路径。 Do this from the designer to have it show at design time. 从设计器执行此操作以使其在设计时显示。

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

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