如何在c#中找到我的项目文件夹中的图片 [英] How to locate the picture inside my project folder in c#

查看:338
本文介绍了如何在c#中找到我的项目文件夹中的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨Guyz,



我很难在图像对象中显示我的图像..



i将我的图像保存在一个文件夹中,这是我保存图像的路径。



C:\ Users \Documents \ Visual Studio 2010 \Projects\ProjectItems \ProjectItems \CopponLogo



实际上文件夹在我的项目中c#



i在asp.net中有一个图像对象,但是当我找到图片的路径时它不起作用



继承人我的代码:



  string  startupPath = AppDomain.CurrentDomain.BaseDirectory; 
string targetPath = startupPath + CouponLogo \\ \\\;
imgLogo.ImageUrl = startupPath + smile.png





提前感谢..

解决方案

这段代码很容易在调试器下检查。你应该总是使用调试器就是这种情况。您可以将计算的文件名与预期值进行比较。如果您发现差异并且不理解您,那么对于CodeProject来说这将是一个很好的正确问题。



两条建议:



计算可执行目录的最通用方法是:

  string  exePath = 
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetEntryAssembly()。Location));





请尝试。其他一些方法取决于您在应用程序,服务,Visual Studio运行时主机等中托管代码的位置。



现在,当您将图像放在可执行文件目录中时,这意味着你以只读方式使用它。 我建议你改用资源。



不要在你的目录中添加图像。创建.resx资源。使用添加资源编辑它=> 添加现有文件。添加文件。如果文件与资源文件不在同一目录中,则会将其复制到那里,因此请注意不要保留同一文件的多个副本。此外,此文件将添加到项目结构中,其引用将添加到您的资源中。打开表示图像文件的项目节点的属性。您将看到它不会被复制到构建的输出中。它将位于可执行文件中的资源中。您将在表示资源文件的节点的子节点中看到Visual Studio自动生成的代码文件。打开此文件并找到一个静态类及其静态属性,其名称类似于您在输入时使用的图像文件的名称。使用此属性访问资源;不要使用资源管理器和资源的字符串名称 - 这不是真正可支持的方式。



使用该属性。例如,如果您的图像文件是PNG,则该属性的类型为 System.Drawing.Bitmap 。您不需要匹配资源名称,从资源流中读取图像数据等。只需使用位图实例。



另一个好处:它将保护您的图像来自用户的修改。如果你也签署你的装配,它将使你的图像几乎不可能修改。



-SA


不应该将smile.png加在引号中,如



< code> 
imgLogo.ImageUrl = startupPath + smile.png;
< / 代码 >





以下是分号?



Gus


 System.Reflection.Assembly.GetExecutingAssembly()。GetManifestResourceNames()



会给你一个数组所有嵌入资源的路径(字符串[])。



获取名为smile.png的嵌入式资源文件的流,该文件位于名为MyResources的文件夹中:

 Assembly.GetExecutingAssembly()。GetManifestResourceStream(  AssemblyName.MyResources .smile.png); 


Hi Guyz,

I'm having a hard time for displaying my images in a image object..

i saved my images in in a folder, heres the path where im saving my images.

C:\Users\Documents\Visual Studio 2010\Projects\ProjectItems\ProjectItems\CouponLogo

actually the folder was inside my project in c#

i have a image object in asp.net, but when i locate the path of picture it doesnt work

heres my code:

string startupPath = AppDomain.CurrentDomain.BaseDirectory;
string targetPath = startupPath + "CouponLogo\\";
imgLogo.ImageUrl = startupPath + smile.png



thanks in advance..

解决方案

This code is easy to check up under the debugger. You should always use the debugger is such cases. You could compare the file name you calculated with what you expect. If you see the difference and don't understand you, it would make a good correct question for CodeProject.

Two recommendations for you:

The most universal way of calculating executable directory is this:

string exePath =
    System.IO.Path.GetDirectoryName(
        System.Reflection.Assembly.GetEntryAssembly().Location));



Please try. Some other methods depends on where you host the code, in application, service, Visual Studio runtime host, etc.

Now, as you place your image in the executable directory, it means you use it read-only. I suggest you use resources instead.

Don't add an image to your directory. Create a .resx resource. Edit it by using "Add resource" => "Add existing file". Add a file. If a file was not in the same directory with the resource file, it will be copied there, so be careful not to keep multiple copies of the same file. Also, this file will be added to the project structure, and its reference will be added to your resource. Open the property of the project node representing your image file. You will see that it is not to be copied in output of the build. It will be places in resource embedded in your executable file. You will see that Visual Studio auto-generated code file, in a child node of the node representing the resource file. Open this file and locate a static class and its static property with the name resembling the name of your image file you used on input. Use this property to access the resource; don't use resource manager and string name of the resource — this is not really supportable way.

Use the property. If, for example, your image file is PNG, the property will be of the type System.Drawing.Bitmap. You won't need to match resource name, read image data from resource stream, etc. Just use the bitmap instance.

One more benefit: it will protect your images from modification by the user. If you also sign your assembly it would make the modification of your images nearly impossible.

—SA


Shouldn't smile.png be in quotation marks as in

<code>
    imgLogo.ImageUrl = startupPath + "smile.png";
</code>



with a semicolon following?

Gus


System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()


will give you an array of paths (string[]) to all your embedded resources.

to get a stream for an embedded resource file called smile.png located in a folder called MyResources:

Assembly.GetExecutingAssembly().GetManifestResourceStream("AssemblyName.MyResources.smile.png");


这篇关于如何在c#中找到我的项目文件夹中的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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