C# 从路径名获取 Image 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/432245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:09:36  来源:igfitidea点击:

get Image object from path name

c#.netimage

提问by leora

Given a path and filename how can I get the Imageobject:

给定路径和文件名,如何获取Image对象:

Image image = ...(filename)

采纳答案by casperOne

回答by BFree

Another alternative is to use a Bitmap object (which inherits from Image) like so:

另一种选择是使用 Bitmap 对象(从 Image 继承),如下所示:

Bitmap bitmap = new Bitmap(imagePath);

(This works for all image formats, not just *.bmp as the name might imply.)

(这适用于所有图像格式,而不仅仅是名称可能暗示的 *.bmp。)

回答by Jerry

If you're playing with images in memory, I've found that bobpowell.net has a GREAT site for GDI work in C#.

如果您正在使用内存中的图像,我发现 bobpowell.net 有一个很棒的站点,用于在 C# 中进行 GDI 工作。

No.. I'm not related to, associated with or hired by Bob Powell. I just really enjoy his work. =)

不......我与鲍勃鲍威尔没有任何关系、关联或受雇于鲍威尔。我真的很喜欢他的工作。=)

回答by rd42

// Get original filename with extention
            string filenameWithPath = "C:\pictures\peanutbutterjellytime.jpg;
            filename = System.IO.Path.GetFileName(filenameWithPath); 

回答by Crumblenautjs

Also, if you're having trouble getting the path of the resource (trickiest part) you can do a :

此外,如果您在获取资源路径(最棘手的部分)时遇到问题,您可以执行以下操作:

Assembly myAssembly = Assembly.GetExecutingAssembly();
string[] names = myAssembly.GetManifestResourceNames();
foreach (string name in names)
{
   Console.WriteLine( name );
} 

Which shows all paths for resources.

它显示了资源的所有路径。