如何在 C# 中访问嵌入的 .Net 图像资源所需的示例代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/154262/
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-03 15:56:42  来源:igfitidea点击:

Example code required for how to access embedded .Net image resources in C#

提问by David Max

It's very easy to mark an image file to become an embedded resource however how does one access the image thereafter. Please can I have some example code?

将图像文件标记为嵌入资源非常容易,但是此后如何访问图像。请问我可以有一些示例代码吗?

回答by Franci Penov

回答by Franci Penov

The most direct method:

最直接的方法:

YourProjectsBaseNamespace.Properties.Resources.YourImageResourceName

回答by Gulzar Nazim

1) Adding and Editing Resources (Visual C#)

1)添加和编辑资源 (Visual C#)

System.Drawing.Bitmap bitmap1 = myProject.Properties.Resources.Image01; 

2) Accessing Embedded Resources using GetManifestResourceStream

2)使用 GetManifestResourceStream 访问嵌入式资源

Assembly _assembly = Assembly.GetExecutingAssembly();

Stream _imageStream = 
    _assembly.GetManifestResourceStream(
    "ThumbnailPictureViewer.resources.Image1.bmp");
Bitmap theDefaultImage = new Bitmap(_imageStream);

回答by Leahn Novash

//Get the names of the embedded resource files;

List<string> resources = new List<string>(AssemblyBuilder.GetExecutingAssembly().GetManifestResourceNames());

//Get the stream

StreamReader sr = new StreamReader(
                AssemblyBuilder.GetExecutingAssembly().GetManifestResourceStream(
                    resources.Find(target => target.ToLower().Contains("insert name here"))

You can convert from bitmap from the stream. The Bitmap class has a method that does this. LoadFromStream if my memory serves.

您可以从流中的位图进行转换。Bitmap 类有一个方法可以做到这一点。LoadFromStream 如果我没记错的话。