ResourceManager.GetStream 方法

定义

从指定资源返回非托管内存流对象。Returns an unmanaged memory stream object from a specified resource.

重载

GetStream(String)

从指定资源返回非托管内存流对象。Returns an unmanaged memory stream object from the specified resource.

GetStream(String, CultureInfo)

使用指定的区域性从指定的资源返回非托管内存流对象。Returns an unmanaged memory stream object from the specified resource, using the specified culture.

GetStream(String)

重要

此 API 不符合 CLS。

从指定资源返回非托管内存流对象。Returns an unmanaged memory stream object from the specified resource.

public:
 System::IO::UnmanagedMemoryStream ^ GetStream(System::String ^ name);
[System.Runtime.InteropServices.ComVisible(false)]
[System.CLSCompliant(false)]
public System.IO.UnmanagedMemoryStream GetStream (string name);
member this.GetStream : string -> System.IO.UnmanagedMemoryStream
Public Function GetStream (name As String) As UnmanagedMemoryStream

参数

name
String

资源的名称。The name of a resource.

返回

表示资源的非托管内存流对象。An unmanaged memory stream object that represents a resource.

属性

异常

指定资源的值不是 MemoryStream 对象。The value of the specified resource is not a MemoryStream object.

namenullname is null.

未找到可用的资源集,并且没有默认资源。No usable set of resources is found, and there are no default resources. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

默认区域性的资源位于无法找到的附属程序集。The default culture's resources reside in a satellite assembly that could not be found. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

示例

下面的示例使用 GetStream(String) 方法检索应用启动初始窗口中使用的位图。The following example uses the GetStream(String) method to retrieve a bitmap that is used in an app's opening splash window. 以下来自名为 CreateResources.cs (适用于C#)或位于(对于 Visual Basic)的文件的源代码将生成一个包含序列化图像的 .Resx 文件 AppResources。The following source code from a file named CreateResources.cs (for C#) or CreateResources.vb (for Visual Basic) generates a .resx file named AppResources.resx that contains the serialized image. 在这种情况下,图片从一个名为 SplashScreen.jpg 的文件中加载;可以修改文件名以替换你自己的图像。In this case, the image is loaded from a file named SplashScreen.jpg; you can modify the file name to substitute your own image.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Resources;

public class Example
{
   public static void Main()
   {
      Bitmap bmp = new Bitmap(@".\SplashScreen.jpg");
      MemoryStream imageStream = new MemoryStream();
      bmp.Save(imageStream, ImageFormat.Jpeg);
      
      ResXResourceWriter writer = new ResXResourceWriter("AppResources.resx");
      writer.AddResource("SplashScreen", imageStream);
      writer.Generate();
      writer.Close();      
   }
}
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Resources

Module Example
   Public Sub Main()
      Dim bmp As New Bitmap(".\SplashScreen.jpg")
      Dim imageStream As New MemoryStream()
      bmp.Save(imageStream, ImageFormat.Jpeg)
      
      Dim writer As New ResXResourceWriter("AppResources.resx")
      writer.AddResource("SplashScreen", imageStream)
      writer.Generate()
      writer.Close()      
   End Sub
End Module

以下来自名为 GetStream.cs (for C#)或 system.resources.resourcemanager.getstream (对于 Visual Basic)的文件中的代码检索资源,并在System.Windows.Forms.PictureBox控件中显示图像。The following code from a file named GetStream.cs (for C#) or GetStream.vb (for Visual Basic) then retrieves the resource and displays the image in a System.Windows.Forms.PictureBox control.

using System;
using System.Drawing;
using System.IO;
using System.Resources;
using System.Windows.Forms;

public class Example
{
   public static void Main()
   {
      ResourceManager rm = new ResourceManager("AppResources", typeof(Example).Assembly);
      Bitmap screen = (Bitmap) Image.FromStream(rm.GetStream("SplashScreen"));
      
      Form frm = new Form();
      frm.Size = new Size(300, 300);

      PictureBox pic = new PictureBox();
      pic.Bounds = frm.RestoreBounds;
      pic.BorderStyle = BorderStyle.Fixed3D; 
      pic.Image = screen;
      pic.SizeMode = PictureBoxSizeMode.StretchImage;

      frm.Controls.Add(pic);
      pic.Anchor = AnchorStyles.Top | AnchorStyles.Bottom |
                   AnchorStyles.Left | AnchorStyles.Right;

      frm.ShowDialog();
   }
}
Imports System.Drawing
Imports System.IO
Imports System.Resources
Imports System.Windows.Forms

Module Example
   Public Sub Main()
      Dim rm As New ResourceManager("AppResources", GetType(Example).Assembly)
      Dim screen As Bitmap = CType(Image.FromStream(rm.GetStream("SplashScreen")), Bitmap)
      
      Dim frm As New Form()
      frm.Size = new Size(300, 300)

      Dim pic As New PictureBox()
      pic.Bounds = frm.RestoreBounds
      pic.BorderStyle = BorderStyle.Fixed3D 
      pic.Image = screen
      pic.SizeMode = PictureBoxSizeMode.StretchImage

      frm.Controls.Add(pic)
      pic.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or
                   AnchorStyles.Left Or AnchorStyles.Right

      frm.ShowDialog()
   End Sub
End Module

可以使用以下批处理文件生成 C# 示例。You can use the following batch file to build the C# example. 对于 Visual Basic,将 csc 更改为 vbc,并将源代码文件的扩展名由 .cs 更改为 .vbFor Visual Basic, change csc to vbc, and change the extension of the source code file from .cs to .vb.

  
csc CreateResources.cs  
CreateResources  
  
resgen AppResources.resx  
  
csc GetStream.cs /resource:AppResources.resources  
  

注解

方法采用存储MemoryStream为对象的资源的名称, Object获取资源的值,并返回一个UnmanagedMemoryStream对象。 GetStreamThe GetStream method takes the name of a resource that is stored as a MemoryStream object, gets the value of the Object resource, and returns an UnmanagedMemoryStream object. 它要求您直接处理字节流,然后将其转换为对象。It requires that you work directly with a stream of bytes, which you then convert to an object. 此方法主要用于性能原因:以字节流而不是显式对象的形式检索资源可以提高性能。This method is useful primarily for performance reasons: Retrieving a resource as a byte stream instead of an explicit object can improve performance.

返回的资源针对当前线程的 UI 区域性进行了本地化,后者由CultureInfo.CurrentUICulture属性定义。The returned resource is localized for the UI culture of the current thread, which is defined by the CultureInfo.CurrentUICulture property. 如果没有为此区域性本地化该资源,资源管理器将使用回退规则加载相应的资源。If the resource is not localized for that culture, the resource manager uses fallback rules to load an appropriate resource. 如果未找到可用的本地化资源集,则ResourceManager回退到默认区域性的资源。If no usable set of localized resources is found, the ResourceManager falls back on the default culture's resources. 如果找不到默认区域性的资源集,则该方法将引发MissingManifestResourceException异常,或者,如果资源集预期位于附属程序集中MissingSatelliteAssemblyException ,则会引发异常。If a resource set for the default culture is not found, the method throws a MissingManifestResourceException exception or, if the resource set is expected to reside in a satellite assembly, a MissingSatelliteAssemblyException exception. 如果资源管理器可以加载相应的资源集,但找不到名name为的资源, null则该方法将返回。If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns null.

属性确定name与资源名称的比较是否不区分大小写(默认值)或区分大小写。 IgnoreCaseThe IgnoreCase property determines whether the comparison of name with the names of resources is case-insensitive (the default) or case-sensitive.

安全性

ReflectionPermission
当通过诸如InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])等机制后期绑定调用时。when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). 关联枚举:MemberAccessAssociated enumeration: MemberAccess.

GetStream(String, CultureInfo)

重要

此 API 不符合 CLS。

使用指定的区域性从指定的资源返回非托管内存流对象。Returns an unmanaged memory stream object from the specified resource, using the specified culture.

public:
 System::IO::UnmanagedMemoryStream ^ GetStream(System::String ^ name, System::Globalization::CultureInfo ^ culture);
[System.Runtime.InteropServices.ComVisible(false)]
[System.CLSCompliant(false)]
public System.IO.UnmanagedMemoryStream GetStream (string name, System.Globalization.CultureInfo culture);
member this.GetStream : string * System.Globalization.CultureInfo -> System.IO.UnmanagedMemoryStream
Public Function GetStream (name As String, culture As CultureInfo) As UnmanagedMemoryStream

参数

name
String

资源的名称。The name of a resource.

culture
CultureInfo

指定要用于资源查找的区域性的对象。An object that specifies the culture to use for the resource lookup. 如果 culturenull,则使用当前线程的区域性。If culture is null, the culture for the current thread is used.

返回

表示资源的非托管内存流对象。An unmanaged memory stream object that represents a resource.

属性

异常

指定资源的值不是 MemoryStream 对象。The value of the specified resource is not a MemoryStream object.

namenullname is null.

未找到可用的资源集,并且没有默认资源。No usable set of resources is found, and there are no default resources. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

默认区域性的资源位于无法找到的附属程序集。The default culture's resources reside in a satellite assembly that could not be found. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

注解

方法采用存储MemoryStream为对象的资源的名称, Object获取资源的值,并返回一个UnmanagedMemoryStream对象。 GetStreamThe GetStream method takes the name of a resource that is stored as a MemoryStream object, gets the value of the Object resource, and returns an UnmanagedMemoryStream object. 它要求您直接处理字节流,然后将其转换为对象。It requires that you work directly with a stream of bytes, which you then convert to an object. 此方法主要用于性能原因:以字节流而不是显式对象的形式检索资源可以提高性能。This method is useful primarily for performance reasons: Retrieving a resource as a byte stream instead of an explicit object can improve performance.

返回的资源culture针对指定的区域性进行了本地化; 如果culturenull,则为CultureInfo.CurrentUICulture属性所指定的区域性。The returned resource is localized for the culture that is specified by culture, or for the culture that is specified by the CultureInfo.CurrentUICulture property if culture is null. 如果没有为此区域性本地化该资源,资源管理器将使用回退规则加载相应的资源。If the resource is not localized for that culture, the resource manager uses fallback rules to load an appropriate resource. 如果未找到可用的本地化资源集,则ResourceManager回退到默认区域性的资源。If no usable set of localized resources is found, the ResourceManager falls back on the default culture's resources. 如果找不到默认区域性的资源集,则该方法将引发MissingManifestResourceException异常,或者,如果资源集预期位于附属程序集中MissingSatelliteAssemblyException ,则会引发异常。If a resource set for the default culture is not found, the method throws a MissingManifestResourceException exception or, if the resource set is expected to reside in a satellite assembly, a MissingSatelliteAssemblyException exception. 如果资源管理器可以加载相应的资源集,但找不到名name为的资源, null则该方法将返回。If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns null.

属性确定name与资源名称的比较是否不区分大小写(默认值)或区分大小写。 IgnoreCaseThe IgnoreCase property determines whether the comparison of name with the names of resources is case-insensitive (the default) or case-sensitive.

安全性

ReflectionPermission
当通过诸如InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])等机制后期绑定调用时。when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). 关联的枚举:MemberAccessAssociated enumeration: MemberAccess.

适用于