ResourceManager.GetStream Method (String)
Returns an unmanaged memory stream object from the specified resource.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- name
-
Type:
System.String
The name of a resource.
Return Value
Type: System.IO.UnmanagedMemoryStreamAn unmanaged memory stream object that represents a resource .
| Exception | Condition |
|---|---|
| InvalidOperationException | The value of the specified resource is not a MemoryStream object. |
| ArgumentNullException | name is null. |
| MissingManifestResourceException | 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. |
| MissingSatelliteAssemblyException | 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. |
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.
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.
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.
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
when invoked late-bound through mechanisms such as Type.InvokeMember. Associated enumeration: ReflectionPermissionFlag.MemberAccess.
Available since 2.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0