ResourceUtil.cs
Upload User: nnpulika
Upload Date: 2013-02-15
Package Size: 597k
Code Size: 6k
Category:

StatusBar

Development Platform:

C#

  1. using System;
  2. using System.IO;
  3. using System.Drawing;
  4. using System.Reflection;
  5. using System.Windows.Forms;
  6. using System.Resources;
  7. namespace UtilityLibrary.General
  8. {
  9. public class ResourceUtil
  10. {
  11. #region Methods
  12. public static Icon LoadIconStream(Type assemblyType, string iconName)
  13. {
  14. // Get the assembly that contains the bitmap resource
  15. Assembly myAssembly = Assembly.GetAssembly(assemblyType);
  16. // Get the resource stream containing the images
  17. Stream iconStream = myAssembly.GetManifestResourceStream(iconName);
  18. // Load the Icon from the stream
  19. return new Icon(iconStream);
  20. }
  21. public static Icon LoadIconStream(Type assemblyType, string iconName, Size iconSize)
  22. {
  23. // Load the entire Icon requested (may include several different Icon sizes)
  24. Icon rawIcon = LoadIconStream(assemblyType, iconName);
  25. // Create and return a new Icon that only contains the requested size
  26. return new Icon(rawIcon, iconSize); 
  27. }
  28. public static Bitmap LoadBitmapStream(Type assemblyType, string imageName)
  29. {
  30. return LoadBitmapStream(assemblyType, imageName, false, new Point(0,0));
  31. }
  32. public static Bitmap LoadBitmapStream(Type assemblyType, string imageName, Point transparentPixel)
  33. {
  34. return LoadBitmapStream(assemblyType, imageName, true, transparentPixel);
  35. }
  36. public static ImageList LoadImageListStream(Type assemblyType, 
  37. string imageName, 
  38. Size imageSize)
  39. {
  40. return LoadImageListStream(assemblyType, imageName, imageSize, false, new Point(0,0));
  41. }
  42. public static ImageList LoadImageListStream(Type assemblyType, 
  43. string imageName, 
  44. Size imageSize,
  45. Point transparentPixel)
  46. {
  47. return LoadImageListStream(assemblyType, imageName, imageSize, true, transparentPixel);
  48. }
  49. public static ImageList LoadImageListStream(Type assemblyType, 
  50.    string imageName, 
  51.    Size imageSize,
  52.    bool makeTransparent,
  53.    Point transparentPixel)
  54. {
  55. // Create storage for bitmap strip
  56. ImageList images = new ImageList();
  57. // Define the size of images we supply
  58. images.ImageSize = imageSize;
  59. // Get the assembly that contains the bitmap resource
  60. Assembly myAssembly = Assembly.GetAssembly(assemblyType);
  61. // Get the resource stream containing the images
  62. Stream imageStream = myAssembly.GetManifestResourceStream(imageName);
  63. // Load the bitmap strip from resource
  64. Bitmap pics = new Bitmap(imageStream);
  65. if (makeTransparent)
  66. {
  67. Color backColor = pics.GetPixel(transparentPixel.X, transparentPixel.Y);
  68.     
  69. // Make backColor transparent for Bitmap
  70. pics.MakeTransparent(backColor);
  71. }
  72.     
  73. // Load them all !
  74. images.Images.AddStrip(pics);
  75. return images;
  76. }
  77.         
  78. // The difference between the "LoadXStream" and "LoadXResource" functions is that
  79. // the load stream functions will load a embedded resource -- a file that you choose
  80. // to "embed as resource" while the load resource functions work with resource files
  81. // that have structure (.resX and .resources) and thus can hold several different 
  82. // resource items.
  83. public static Icon LoadIconResource(Type assemblyType, string resourceHolder, string imageName) 
  84. {
  85. // Get the assembly that contains the bitmap resource
  86. Assembly thisAssembly = Assembly.GetAssembly(assemblyType);
  87. ResourceManager rm = new ResourceManager(resourceHolder, thisAssembly);
  88. Icon icon = (Icon)rm.GetObject(imageName);
  89. return icon;
  90. }
  91. public static Bitmap LoadBitmapResource(Type assemblyType, string resourceHolder, string imageName) 
  92. {
  93. // Get the assembly that contains the bitmap resource
  94. Assembly thisAssembly = Assembly.GetAssembly(assemblyType);
  95. ResourceManager rm = new ResourceManager(resourceHolder, thisAssembly);
  96. Bitmap bitmap = (Bitmap)rm.GetObject(imageName);
  97. return bitmap;
  98. }
  99. public static ImageList LoadImageListResource(Type assemblyType, string resourceHolder,
  100. string imageName, Size imageSize)
  101. {
  102. return LoadImageListResource(assemblyType, resourceHolder, imageName, imageSize, false, new Point(0,0));
  103. }
  104. public static ImageList LoadImageListResource(Type assemblyType, string resourceHolder,
  105. string imageName, 
  106. Size imageSize,
  107. bool makeTransparent,
  108. Point transparentPixel)
  109. {
  110. // Create storage for bitmap strip
  111. ImageList images = new ImageList();
  112. // Define the size of images we supply
  113. images.ImageSize = imageSize;
  114. // Get the assembly that contains the bitmap resource
  115. Assembly thisAssembly = Assembly.GetAssembly(assemblyType);
  116. ResourceManager rm = new ResourceManager(resourceHolder, thisAssembly);
  117. Bitmap pics = (Bitmap)rm.GetObject(imageName);
  118. if (makeTransparent)
  119. {
  120. Color backColor = pics.GetPixel(transparentPixel.X, transparentPixel.Y);
  121.     
  122. // Make backColor transparent for Bitmap
  123. pics.MakeTransparent(backColor);
  124. }
  125.     
  126. // Load the image
  127. images.Images.AddStrip(pics);
  128. return images;
  129. }
  130. #endregion
  131. #region Implementation
  132. protected static Bitmap LoadBitmapStream(Type assemblyType, string imageName, 
  133. bool makeTransparent, Point transparentPixel)
  134. {
  135. // Get the assembly that contains the bitmap resource
  136. Assembly myAssembly = Assembly.GetAssembly(assemblyType);
  137. // Get the resource stream containing the images
  138. Stream imageStream = myAssembly.GetManifestResourceStream(imageName);
  139. // Load the bitmap from stream
  140. Bitmap image = new Bitmap(imageStream);
  141. if (makeTransparent)
  142. {
  143. Color backColor = image.GetPixel(transparentPixel.X, transparentPixel.Y);
  144.     
  145. // Make backColor transparent for Bitmap
  146. image.MakeTransparent(backColor);
  147. }
  148.     
  149. return image;
  150. }
  151. #endregion
  152. }
  153. }