Detailed explanation of the method of obtaining pictures in resource files by C programming
- 2021-12-19 06:34:23
- OfStack
Detailed explanation of the method of obtaining pictures in resource files by C # programming
This paper mainly introduces the method of C # programming to get pictures in resource files, and involves the related skills of C # aiming at the operation of resource files in the project for reference. The specific contents are as follows:
Examples:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Drawing;
namespace CL
{
public class RES
{
/// <summary>
/// Definition 1 Resource file names Resource file name = Default namespace for the project + Filename ( Without extension )
/// </summary>
private string PublicResourceFileName = "CL.Resources";
/// <summary>
/// Read from a resource file 1 Resources
/// </summary>
/// <param name="resFile"> Resource file name Namespace + File name </param>
/// <param name="resName"> Name of resource to read </param>
/// <returns> Return 1 Resources Read failure returns NULL</returns>
public System.Object ReadFromResourceFile(String resName)
{
try
{
Assembly myAssembly;
myAssembly = Assembly.GetExecutingAssembly();
System.Resources.ResourceManager rm = new
System.Resources.ResourceManager(PublicResourceFileName, myAssembly);
return rm.GetObject(resName);
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// Get a resource picture
/// </summary>
/// <param name="name"> Filename </param>
/// <returns> Resource picture </returns>
public Bitmap GetResourceImage(String name)
{
Object tempbitmap = null;
tempbitmap = ReadFromResourceFile(name);
if (tempbitmap.GetType().Equals(typeof(Bitmap)))
{
return (Bitmap)tempbitmap;
}
return null;
}
}
}
// Call GetResourceImage Method will do. name Is the name of the file without suffix .
Thank you for reading, hope to help everyone, thank you for your support to this site!