ResourceManager.GetStream 方法

定义

从指定资源返回非托管内存流对象。

重载

GetStream(String)

从指定资源返回非托管内存流对象。

GetStream(String, CultureInfo)

使用指定的区域性从指定的资源返回非托管内存流对象。

GetStream(String)

重要

此 API 不符合 CLS。

从指定资源返回非托管内存流对象。

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

参数

name
String

资源的名称。

返回

UnmanagedMemoryStream

表示资源的非托管内存流对象。

属性

例外

指定资源的值不是 MemoryStream 对象。

namenull

未找到可用的资源集,并且没有默认资源。 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。

默认区域性的资源位于无法找到的附属程序集。 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。

示例

下面的示例使用 GetStream(String) 方法检索应用启动初始窗口中使用的位图。 适用于 C#) 的名为 CreateResources.cs (的文件的以下源代码或用于Visual Basic) 的 CreateResources.vb (生成名为 AppResources.resx 的 .resx 文件,该文件包含序列化映像。 在这种情况下,图片从一个名为 SplashScreen.jpg 的文件中加载;可以修改文件名以替换你自己的图像。

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#) 或 GetStream.vb (的文件,用于Visual Basic) 然后检索资源并在控件中System.Windows.Forms.PictureBox显示图像。

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# 示例。 对于 Visual Basic,将 csc 更改为 vbc,并将源代码文件的扩展名由 .cs 更改为 .vb

csc CreateResources.cs
CreateResources

resgen AppResources.resx

csc GetStream.cs /resource:AppResources.resources

注解

该方法 GetStream 采用存储为 MemoryStream 对象的资源的名称,获取资源的值 Object ,并返回对象 UnmanagedMemoryStream 。 它要求你直接使用字节流,然后转换为对象。 此方法主要用于性能原因:将资源检索为字节流而不是显式对象可以提高性能。

返回的资源已本地化为当前线程的 UI 区域性,该区域性由 CultureInfo.CurrentUICulture 属性定义。 如果资源未针对该区域性进行本地化,则资源管理器使用回退规则来加载适当的资源。 如果未找到任何可用本地化资源集,则 ResourceManager 回退默认区域性的资源。 如果未找到默认区域性的资源集,该方法将 MissingManifestResourceException 引发异常,或者,如果资源集应驻留在附属程序集中,则 MissingSatelliteAssemblyException 为异常。 如果资源管理器可以加载适当的资源集,但找不到名为 name的资源,该方法将 null返回。

IgnoreCase 属性确定与资源名称的比较 name 是否不区分大小写, (默认) 还是区分大小写。

适用于

GetStream(String, CultureInfo)

重要

此 API 不符合 CLS。

使用指定的区域性从指定的资源返回非托管内存流对象。

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

参数

name
String

资源的名称。

culture
CultureInfo

要用于资源查找的区域性。 如果 culturenull,则使用当前线程的区域性。

返回

UnmanagedMemoryStream

表示资源的非托管内存流对象。

属性

例外

指定资源的值不是 MemoryStream 对象。

namenull

未找到可用的资源集,并且没有默认资源。 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。

默认区域性的资源位于无法找到的附属程序集。 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。

注解

该方法 GetStream 采用存储为 MemoryStream 对象的资源的名称,获取资源的值 Object ,并返回对象 UnmanagedMemoryStream 。 它要求你直接使用字节流,然后转换为对象。 此方法主要用于性能原因:将资源检索为字节流而不是显式对象可以提高性能。

返回的资源针对指定的 culture区域性或属性指定的 CultureInfo.CurrentUICulture 区域性(如果 culture 为) null进行本地化。 如果资源未针对该区域性进行本地化,资源管理器将使用回退规则来加载适当的资源。 如果未找到任何可用本地化资源集,则 ResourceManager 回退默认区域性的资源。 如果未找到默认区域性的资源集,该方法将 MissingManifestResourceException 引发异常,或者,如果资源集应驻留在附属程序集中,则为 MissingSatelliteAssemblyException 异常。 如果资源管理器可以加载适当的资源集,但找不到名为 name的资源,该方法将 null返回。

IgnoreCase 属性确定与资源名称的 name 比较是否区分大小写, (默认) 还是区分大小写。

适用于