ResourceManager.GetStream 方法

定義

從指定的資源傳回 Unmanaged 記憶體資料流物件。Returns an unmanaged memory stream object from a specified resource.

多載

GetStream(String)

從指定的資源傳回 Unmanaged 記憶體資料流物件。Returns an unmanaged memory stream object from the specified resource.

GetStream(String, CultureInfo)

使用指定的文化特性,從指定的資源傳回 Unmanged 記憶體資料流物件。Returns an unmanaged memory stream object from the specified resource, using the specified culture.

GetStream(String)

重要

此 API 不符合 CLS 規範。

從指定的資源傳回 Unmanaged 記憶體資料流物件。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.

傳回

代表資源的 Unmanaged 記憶體資料流物件。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)之檔案的原始程式碼會產生名為 appresources.resx properties 的 .resx 檔案,其中包含已序列化的影像。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 (適用于C#)或 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. 如果資源未針對該文化特性進行當地語系化,則資源管理員會使用 fallback 規則來載入適當的資源。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[]). 相關聯的MemberAccess列舉:。Associated enumeration: MemberAccess.

GetStream(String, CultureInfo)

重要

此 API 不符合 CLS 規範。

使用指定的文化特性,從指定的資源傳回 Unmanged 記憶體資料流物件。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.

傳回

代表資源的 Unmanaged 記憶體資料流物件。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針對所指定的文化CultureInfo.CurrentUICulture特性進行當地語系化,如果culturenull,則為屬性所指定的文化特性。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. 如果資源未針對該文化特性進行當地語系化,則資源管理員會使用 fallback 規則來載入適當的資源。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.

適用於