Function.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.Windows.Forms;
  5. namespace ShadowMerger
  6. {
  7. class Function
  8. {
  9. // int dimension = 500 - Image manipulation area, increase for larger images but must be static for while library
  10. public static Bitmap combineIMG(string image, string shadow, int[] imageOffsets, int[] shadowOffsets, int dimension = 500)
  11. {
  12. Bitmap finalImage = new Bitmap(dimension, dimension);
  13. int center = finalImage.Width / 2;
  14. using (Graphics g = Graphics.FromImage(finalImage))
  15. {
  16. //set background color
  17. g.Clear(Color.Black);
  18. Bitmap shadowImage = new Bitmap(shadow);
  19. g.DrawImage(shadowImage, new Rectangle(shadowOffsets[0] + center, shadowOffsets[1] + center, shadowImage.Width, shadowImage.Height));
  20. }
  21. finalImage = MirStyleTransparentBitLock(finalImage);
  22. finalImage = ChangeColorBitLock(finalImage);
  23. using (Graphics gr = Graphics.FromImage(finalImage))
  24. {
  25. Bitmap mobImage = new Bitmap(image);
  26. mobImage.MakeTransparent(Color.Black);
  27. gr.DrawImage(mobImage, new Rectangle(imageOffsets[0] + center, imageOffsets[1] + center, mobImage.Width, mobImage.Height));
  28. }
  29. return finalImage;
  30. }
  31. // Remove region fat
  32. public static Bitmap Crop(Bitmap bmp)
  33. {
  34. int w = bmp.Width, h = bmp.Height;
  35. Func<int, bool> allWhiteRow = row =>
  36. {
  37. for (int i = 0; i < w; ++i)
  38. if (bmp.GetPixel(i, row).R != 0)
  39. return false;
  40. return true;
  41. };
  42. Func<int, bool> allWhiteColumn = col =>
  43. {
  44. for (int i = 0; i < h; ++i)
  45. if (bmp.GetPixel(col, i).R != 0)
  46. return false;
  47. return true;
  48. };
  49. int topmost = 0;
  50. for (int row = 0; row < h; ++row)
  51. {
  52. if (allWhiteRow(row))
  53. topmost = row;
  54. else break;
  55. }
  56. int bottommost = 0;
  57. for (int row = h - 1; row >= 0; --row)
  58. {
  59. if (allWhiteRow(row))
  60. bottommost = row;
  61. else break;
  62. }
  63. int leftmost = 0, rightmost = 0;
  64. for (int col = 0; col < w; ++col)
  65. {
  66. if (allWhiteColumn(col))
  67. leftmost = col;
  68. else
  69. break;
  70. }
  71. for (int col = w - 1; col >= 0; --col)
  72. {
  73. if (allWhiteColumn(col))
  74. rightmost = col;
  75. else
  76. break;
  77. }
  78. int croppedWidth = rightmost - leftmost;
  79. int croppedHeight = bottommost - topmost;
  80. try
  81. {
  82. Bitmap target = new Bitmap(croppedWidth, croppedHeight);
  83. using (Graphics g = Graphics.FromImage(target))
  84. {
  85. g.DrawImage(bmp,
  86. new RectangleF(0, 0, croppedWidth, croppedHeight),
  87. new RectangleF(leftmost, topmost, croppedWidth, croppedHeight),
  88. GraphicsUnit.Pixel);
  89. }
  90. return target;
  91. }
  92. catch (Exception ex)
  93. {
  94. throw new Exception(
  95. string.Format("Values are topmost={0} btm={1} left={2} right={3}", topmost, bottommost, leftmost, rightmost),
  96. ex);
  97. }
  98. }
  99. public static bool IsOdd(int value)
  100. {
  101. return value % 2 != 0;
  102. }
  103. // Mir 2 standard colour
  104. public static Bitmap ChangeColor(Bitmap scrBitmap)
  105. {
  106. Color newColor = Color.FromArgb(16, 8, 8);
  107. for (int i = 0; i < scrBitmap.Width; i++)
  108. {
  109. for (int j = 0; j < scrBitmap.Height; j++)
  110. {
  111. if (scrBitmap.GetPixel(i, j) != Color.FromArgb(0, 0, 0)) //8 12 8
  112. {
  113. scrBitmap.SetPixel(i, j, newColor);
  114. }
  115. }
  116. }
  117. return scrBitmap;
  118. }
  119. // Convert Mir 3 shadow to Mir 2
  120. public static Bitmap MirStyleTransparent(Bitmap bmp)
  121. {
  122. Bitmap myBitmap = new Bitmap(bmp);
  123. for (int i = 0; i < myBitmap.Width - 1; i++)
  124. {
  125. for (int j = 0; j < myBitmap.Height - 1; j++)
  126. {
  127. if (IsOdd(i) & IsOdd(j))
  128. {
  129. myBitmap.SetPixel(i, j, Color.Black);
  130. }
  131. else if (!IsOdd(i) & !IsOdd(j))
  132. {
  133. myBitmap.SetPixel(i, j, Color.Black);
  134. }
  135. }
  136. }
  137. return myBitmap;
  138. }
  139. // Extra pish
  140. public static Bitmap ChangeColorBitLock(Bitmap scrBitmap)
  141. {
  142. Color newColor = Color.FromArgb(16, 8, 8);
  143. LockBitmap lockBitmap = new LockBitmap(scrBitmap);
  144. lockBitmap.LockBits();
  145. for (int i = 0; i < lockBitmap.Width; i++)
  146. {
  147. for (int j = 0; j < lockBitmap.Height; j++)
  148. {
  149. if (lockBitmap.GetPixel(i, j) != Color.FromArgb(0, 0, 0)) //8 12 8
  150. {
  151. lockBitmap.SetPixel(i, j, newColor);
  152. }
  153. }
  154. }
  155. lockBitmap.UnlockBits();
  156. return scrBitmap;
  157. }
  158. public static Bitmap MirStyleTransparentBitLock(Bitmap bmp)
  159. {
  160. LockBitmap lockBitmap = new LockBitmap(bmp);
  161. lockBitmap.LockBits();
  162. for (int i = 0; i < lockBitmap.Width - 1; i++)
  163. {
  164. for (int j = 0; j < lockBitmap.Height - 1; j++)
  165. {
  166. if (IsOdd(i) & IsOdd(j))
  167. {
  168. lockBitmap.SetPixel(i, j, Color.Black);
  169. }
  170. else if (!IsOdd(i) & !IsOdd(j))
  171. {
  172. lockBitmap.SetPixel(i, j, Color.Black);
  173. }
  174. }
  175. }
  176. lockBitmap.UnlockBits();
  177. return bmp;
  178. }
  179. // End Extra pish
  180. public static Bitmap Mir3StyleShadow(Color colour, Image Path)
  181. {
  182. Bitmap mi;
  183. mi = new Bitmap(Path);
  184. int tempWidth = mi.Width;//trueSize.Width
  185. int tempHeight = mi.Height;//trueSize.Height
  186. Bitmap Temp = new Bitmap(tempWidth + tempHeight / 2, tempHeight);
  187. // Render Mir 3 Shadow / Far
  188. int DestY, DestX;
  189. for (int Y = 0; Y < tempHeight; Y++)
  190. {
  191. if (Y % 2 == 1) continue;
  192. DestY = Y / 2 + Temp.Height / 2 - 3;
  193. for (int X = 0; X < tempWidth; X++)
  194. {
  195. if (mi.GetPixel(X, Y).Name == "0") continue;
  196. DestX = X + (Temp.Height - Y) / 2;
  197. Temp.SetPixel(DestX, DestY, colour);
  198. }
  199. }
  200. // Cut out shadow behind mob
  201. for (int X = 0; X < mi.Width; X++)
  202. {
  203. for (int Y = 0; Y < mi.Height; Y++)
  204. {
  205. if (mi.GetPixel(X, Y).Name != "0")
  206. Temp.SetPixel(X, Y, Color.Transparent);
  207. }
  208. }
  209. return Temp;
  210. }
  211. // Set image opacity, utter
  212. /// <summary>
  213. /// method for changing the opacity of an image
  214. /// </summary>
  215. /// <param name="image">image to set opacity on</param>
  216. /// <param name="opacity">percentage of opacity</param>
  217. /// <returns></returns>
  218. public static Image SetImageOpacity(Image image, float opacity)
  219. {
  220. try
  221. {
  222. //create a Bitmap the size of the image provided
  223. Bitmap bmp = new Bitmap(image.Width, image.Height);
  224. //create a graphics object from the image
  225. using (Graphics gfx = Graphics.FromImage(bmp))
  226. {
  227. //create a color matrix object
  228. ColorMatrix matrix = new ColorMatrix();
  229. //set the opacity
  230. matrix.Matrix33 = opacity;
  231. //create image attributes
  232. ImageAttributes attributes = new ImageAttributes();
  233. //set the color(opacity) of the image
  234. attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  235. //now draw the image
  236. gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
  237. }
  238. return bmp;
  239. }
  240. catch (Exception ex)
  241. {
  242. MessageBox.Show(ex.Message);
  243. return null;
  244. }
  245. }
  246. public class Benchmark
  247. {
  248. private static DateTime startDate = DateTime.MinValue;
  249. private static DateTime endDate = DateTime.MinValue;
  250. public static TimeSpan Span { get { return endDate.Subtract(startDate); } }
  251. public static void Start() { startDate = DateTime.Now; }
  252. public static void End() { endDate = DateTime.Now; }
  253. public static double GetSeconds()
  254. {
  255. if (endDate == DateTime.MinValue) return 0.0;
  256. else return Span.TotalSeconds;
  257. }
  258. }
  259. }
  260. }