Edit

Share via


ResourceManager.GetStream Method

Definition

Returns an unmanaged memory stream object from a specified resource.

Overloads

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)

Source:
ResourceManager.cs
Source:
ResourceManager.cs
Source:
ResourceManager.cs

Important

This API is not CLS-compliant.

Returns an unmanaged memory stream object from the specified resource.

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

Parameters

name
String

The name of a resource.

Returns

An unmanaged memory stream object that represents a resource.

Attributes

Exceptions

The value of the specified resource is not a MemoryStream object.

name is null.

No usable set of resources is found, and there are no default resources. 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. For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

Examples

The following example uses the GetStream(String) method to retrieve a bitmap that is used in an app's opening splash window. 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. 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

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

You can use the following batch file to build the C# example. For 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

Remarks

The 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.

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. If no usable set of localized resources is found, the ResourceManager falls back on the default culture's resources. 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. If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns null.

The IgnoreCase property determines whether the comparison of name with the names of resources is case-insensitive (the default) or case-sensitive.

Applies to

GetStream(String, CultureInfo)

Source:
ResourceManager.cs
Source:
ResourceManager.cs
Source:
ResourceManager.cs

Important

This API is not CLS-compliant.

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);
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

Parameters

name
String

The name of a resource.

culture
CultureInfo

The culture to use for the resource lookup. If culture is null, the culture for the current thread is used.

Returns

An unmanaged memory stream object that represents a resource.

Attributes

Exceptions

The value of the specified resource is not a MemoryStream object.

name is null.

No usable set of resources is found, and there are no default resources. 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. For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

Remarks

The 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.

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. If no usable set of localized resources is found, the ResourceManager falls back on the default culture's resources. 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. If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns null.

The IgnoreCase property determines whether the comparison of name with the names of resources is case-insensitive (the default) or case-sensitive.

Applies to