How to read a embedded resource image in C# and VB.NET

To read a embedded resource image in C# and VB.NET you can use the following snippet.

Sample C#

public static Image ReadEmbeddedRessourceImage(Assembly assembly, string searchPattern)
{
	var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(x => x.Contains(searchPattern));
	using (var stream = assembly.GetManifestResourceStream(resourceName))
	{
		if (stream != null) return Image.FromStream(stream);
	}
	return null;
}

Sample VB.NET

Public Shared Function ReadEmbeddedRessourceImage(assembly As Assembly, searchPattern As String) As Image
	Dim resourceName = assembly.GetManifestResourceNames().FirstOrDefault(Function(x) x.Contains(searchPattern))
	Using stream = assembly.GetManifestResourceStream(resourceName)
		If stream IsNot Nothing Then
			Return Image.FromStream(stream)
		End If
	End Using
	Return Nothing
End Function

One thought on “How to read a embedded resource image in C# and VB.NET

Leave a Reply