Image Manipulation

gbertoli3 gbertoli3 is offline Offline Nov 26th, 2008, 1:34 pm |
0
Set the Image's Gamma, Brightness, Contrast, Color; Enhance an Image's Edge; Turn it to a Grayscale Image; and Invert the Image.
Quick reply to this message  
C# Syntax
  1. #region Effects
  2. #region Invert Image
  3. /// <summary>
  4. /// Inverts an Image with the option to Invert it's Transparency.
  5. /// </summary>
  6. /// <param name="bitmap">The Bitmap Image you want to Invert.</param>
  7. /// <param name="InvertAlpha">True to Invert it's Transparency. False to Invert the Image Only.</param>
  8. /// <returns>An Inverted Image</returns>
  9. public Bitmap Invert(Bitmap bitmap)
  10. {
  11. //X Axis
  12. int x;
  13. //Y Axis
  14. int y;
  15.  
  16. //For the Width
  17. for (x = 0; x <= bitmap.Width - 1; x++)
  18. {
  19. //For the Height
  20. for (y = 0; y <= bitmap.Height - 1; y += 1)
  21. {
  22. //The Old Color to Replace
  23. Color oldColor = bitmap.GetPixel(x, y);
  24. //The New Color to Replace the Old Color
  25. Color newColor;
  26.  
  27. //Set the Color for newColor
  28. newColor = System.Drawing.Color.FromArgb(oldColor.A, 255 - oldColor.R, 255 - oldColor.G, 255 - oldColor.B);
  29.  
  30. //Replace the Old Color with the New Color
  31. bitmap.SetPixel(x, y, newColor);
  32. }
  33. }
  34. //Return the Inverted Bitmap
  35. return bitmap;
  36. }
  37.  
  38. /// <summary>
  39. /// Inverts an Image with the option to Invert it's Transparency.
  40. /// </summary>
  41. /// <param name="bitmap">The Bitmap Image you want to Invert.</param>
  42. /// <param name="InvertAlpha">True to Invert it's Transparency. False to Invert the Image Only.</param>
  43. /// <returns>An Inverted Image</returns>
  44. public Bitmap Invert(Bitmap bitmap, bool InvertAlpha)
  45. {
  46. //X Axis
  47. int x;
  48. //Y Axis
  49. int y;
  50. //For the Width
  51. for (x = 0; x <= bitmap.Width - 1; x++)
  52. {
  53. //For the Height
  54. for (y = 0; y <= bitmap.Height - 1; y += 1)
  55. {
  56. //The Old Color to Replace
  57. Color oldColor = bitmap.GetPixel(x, y);
  58. //The New Color to Replace the Old Color
  59. Color newColor;
  60. //If the user has chosen to Invert the Transparency
  61. if (InvertAlpha)
  62. {
  63. //Set the Color for newColor
  64. newColor = System.Drawing.Color.FromArgb(255 - oldColor.A, 255 - oldColor.R, 255 - oldColor.G, 255 - oldColor.B);
  65. }
  66. else
  67. {
  68. //Set the Color for newColor
  69. newColor = System.Drawing.Color.FromArgb(oldColor.A, 255 - oldColor.R, 255 - oldColor.G, 255 - oldColor.B);
  70. }
  71. //Replace the Old Color with the New Color
  72. bitmap.SetPixel(x, y, newColor);
  73. }
  74. }
  75. //Return the Inverted Bitmap
  76. return bitmap;
  77. }
  78. #endregion
  79.  
  80. #region Grayscale
  81. // <summary>
  82. /// Convert an Image to a Grayscale Image.
  83. /// </summary>
  84. /// <param name="Bitmap">The Bitmap to Convert to Grayscale.</param>
  85. /// <returns>A Grayscale Image.</returns>
  86. public Bitmap MakeGrayscale(Bitmap Bitmap)
  87. {
  88. //Declare myBitmap as a new Bitmap with the same Width & Height
  89. Bitmap myBitmap = new Bitmap(Bitmap.Width, Bitmap.Height);
  90.  
  91. for (int x = 0; x < Bitmap.Width; x++)
  92. {
  93. for (int y = 0; y < Bitmap.Height; y++)
  94. {
  95. //Get the Pixel
  96. Color BitmapColor = Bitmap.GetPixel(x, y);
  97.  
  98. //Declare grayScale as the Grayscale Pixel
  99. int grayScale = (int)((BitmapColor.R * 0.3) + (BitmapColor.G * 0.59) + (BitmapColor.B * 0.11));
  100.  
  101. //Declare myColor as a Grayscale Color
  102. Color myColor = System.Drawing.Color.FromArgb(grayScale, grayScale, grayScale);
  103.  
  104. //Set the Grayscale Pixel
  105. myBitmap.SetPixel(x, y, myColor);
  106. }
  107. }
  108. return myBitmap;
  109. }
  110. #endregion
  111.  
  112. #region Gamma
  113. private static Bitmap Gamma(Bitmap b, double red, double green, double blue)
  114. {
  115. if (red < .2 || red > 5) return b;
  116. if (green < .2 || green > 5) return b;
  117. if (blue < .2 || blue > 5) return b;
  118.  
  119. byte[] redGamma = new byte[256];
  120. byte[] greenGamma = new byte[256];
  121. byte[] blueGamma = new byte[256];
  122.  
  123. for (int i = 0; i < 256; ++i)
  124. {
  125. redGamma[i] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(i / 255.0, 1.0 / red)) + 0.5));
  126. greenGamma[i] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(i / 255.0, 1.0 / green)) + 0.5));
  127. blueGamma[i] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(i / 255.0, 1.0 / blue)) + 0.5));
  128. }
  129.  
  130. // GDI+ still lies to us - the return format is BGR, NOT RGB.
  131. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  132.  
  133. int stride = bmData.Stride;
  134. System.IntPtr Scan0 = bmData.Scan0;
  135.  
  136. unsafe
  137. {
  138. byte* p = (byte*)(void*)Scan0;
  139.  
  140. int nOffset = stride - b.Width * 3;
  141.  
  142. for (int y = 0; y < b.Height; ++y)
  143. {
  144. for (int x = 0; x < b.Width; ++x)
  145. {
  146. p[2] = redGamma[p[2]];
  147. p[1] = greenGamma[p[1]];
  148. p[0] = blueGamma[p[0]];
  149.  
  150. p += 3;
  151. }
  152. p += nOffset;
  153. }
  154. }
  155.  
  156. b.UnlockBits(bmData);
  157.  
  158. return b;
  159. }
  160. #endregion
  161.  
  162. #region Brightness
  163. private static Bitmap Brightness(Bitmap b, int nBrightness)
  164. {
  165. if (nBrightness < -255 || nBrightness > 255)
  166. return b;
  167.  
  168. // GDI+ still lies to us - the return format is BGR, NOT RGB.
  169. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  170.  
  171. int stride = bmData.Stride;
  172. System.IntPtr Scan0 = bmData.Scan0;
  173.  
  174. int nVal = 0;
  175.  
  176. unsafe
  177. {
  178. byte* p = (byte*)(void*)Scan0;
  179.  
  180. int nOffset = stride - b.Width * 3;
  181. int nWidth = b.Width * 3;
  182.  
  183. for (int y = 0; y < b.Height; ++y)
  184. {
  185. for (int x = 0; x < nWidth; ++x)
  186. {
  187. nVal = (int)(p[0] + nBrightness);
  188.  
  189. if (nVal < 0) nVal = 0;
  190. if (nVal > 255) nVal = 255;
  191.  
  192. p[0] = (byte)nVal;
  193.  
  194. ++p;
  195. }
  196. p += nOffset;
  197. }
  198. }
  199.  
  200. b.UnlockBits(bmData);
  201.  
  202. return b;
  203. }
  204. #endregion
  205.  
  206. #region Contrast
  207. private static Bitmap Contrast(Bitmap b, sbyte nContrast)
  208. {
  209. if (nContrast < -100) return b;
  210. if (nContrast > 100) return b;
  211.  
  212. double pixel = 0, contrast = (100.0 + nContrast) / 100.0;
  213.  
  214. contrast *= contrast;
  215.  
  216. int red, green, blue;
  217.  
  218. // GDI+ still lies to us - the return format is BGR, NOT RGB.
  219. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  220.  
  221. int stride = bmData.Stride;
  222. System.IntPtr Scan0 = bmData.Scan0;
  223.  
  224. unsafe
  225. {
  226. byte* p = (byte*)(void*)Scan0;
  227.  
  228. int nOffset = stride - b.Width * 3;
  229.  
  230. for (int y = 0; y < b.Height; ++y)
  231. {
  232. for (int x = 0; x < b.Width; ++x)
  233. {
  234. blue = p[0];
  235. green = p[1];
  236. red = p[2];
  237.  
  238. pixel = red / 255.0;
  239. pixel -= 0.5;
  240. pixel *= contrast;
  241. pixel += 0.5;
  242. pixel *= 255;
  243. if (pixel < 0) pixel = 0;
  244. if (pixel > 255) pixel = 255;
  245. p[2] = (byte)pixel;
  246.  
  247. pixel = green / 255.0;
  248. pixel -= 0.5;
  249. pixel *= contrast;
  250. pixel += 0.5;
  251. pixel *= 255;
  252. if (pixel < 0) pixel = 0;
  253. if (pixel > 255) pixel = 255;
  254. p[1] = (byte)pixel;
  255.  
  256. pixel = blue / 255.0;
  257. pixel -= 0.5;
  258. pixel *= contrast;
  259. pixel += 0.5;
  260. pixel *= 255;
  261. if (pixel < 0) pixel = 0;
  262. if (pixel > 255) pixel = 255;
  263. p[0] = (byte)pixel;
  264.  
  265. p += 3;
  266. }
  267. p += nOffset;
  268. }
  269. }
  270.  
  271. b.UnlockBits(bmData);
  272.  
  273. return b;
  274. }
  275. #endregion
  276.  
  277. #region Color
  278. private static Bitmap Color(Bitmap b, int red, int green, int blue)
  279. {
  280. if (red < -255 || red > 255) return b;
  281. if (green < -255 || green > 255) return b;
  282. if (blue < -255 || blue > 255) return b;
  283.  
  284. // GDI+ still lies to us - the return format is BGR, NOT RGB.
  285. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  286.  
  287. int stride = bmData.Stride;
  288. System.IntPtr Scan0 = bmData.Scan0;
  289.  
  290. unsafe
  291. {
  292. byte* p = (byte*)(void*)Scan0;
  293.  
  294. int nOffset = stride - b.Width * 3;
  295. int nPixel;
  296.  
  297. for (int y = 0; y < b.Height; ++y)
  298. {
  299. for (int x = 0; x < b.Width; ++x)
  300. {
  301. nPixel = p[2] + red;
  302. nPixel = Math.Max(nPixel, 0);
  303. p[2] = (byte)Math.Min(255, nPixel);
  304.  
  305. nPixel = p[1] + green;
  306. nPixel = Math.Max(nPixel, 0);
  307. p[1] = (byte)Math.Min(255, nPixel);
  308.  
  309. nPixel = p[0] + blue;
  310. nPixel = Math.Max(nPixel, 0);
  311. p[0] = (byte)Math.Min(255, nPixel);
  312.  
  313. p += 3;
  314. }
  315. p += nOffset;
  316. }
  317. }
  318.  
  319. b.UnlockBits(bmData);
  320.  
  321. return b;
  322. }
  323. #endregion
  324.  
  325. #region EdgeEnhance
  326. private static Bitmap EdgeEnhance(Bitmap b, byte nThreshold)
  327. {
  328. // This one works by working out the greatest difference between a nPixel and it's eight neighbours.
  329. // The threshold allows softer edges to be forced down to black, use 0 to negate it's effect.
  330. Bitmap b2 = (Bitmap)b.Clone();
  331.  
  332. // GDI+ still lies to us - the return format is BGR, NOT RGB.
  333. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  334. BitmapData bmData2 = b2.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  335.  
  336. int stride = bmData.Stride;
  337. System.IntPtr Scan0 = bmData.Scan0;
  338. System.IntPtr Scan02 = bmData2.Scan0;
  339.  
  340. unsafe
  341. {
  342. byte* p = (byte*)(void*)Scan0;
  343. byte* p2 = (byte*)(void*)Scan02;
  344.  
  345. int nOffset = stride - b.Width * 3;
  346. int nWidth = b.Width * 3;
  347.  
  348. int nPixel = 0, nPixelMax = 0;
  349.  
  350. p += stride;
  351. p2 += stride;
  352.  
  353. for (int y = 1; y < b.Height - 1; ++y)
  354. {
  355. p += 3;
  356. p2 += 3;
  357.  
  358. for (int x = 3; x < nWidth - 3; ++x)
  359. {
  360. nPixelMax = Math.Abs((p2 - stride + 3)[0] - (p2 + stride - 3)[0]);
  361.  
  362. nPixel = Math.Abs((p2 + stride + 3)[0] - (p2 - stride - 3)[0]);
  363.  
  364. if (nPixel > nPixelMax) nPixelMax = nPixel;
  365.  
  366. nPixel = Math.Abs((p2 - stride)[0] - (p2 + stride)[0]);
  367.  
  368. if (nPixel > nPixelMax) nPixelMax = nPixel;
  369.  
  370. nPixel = Math.Abs((p2 + 3)[0] - (p2 - 3)[0]);
  371.  
  372. if (nPixel > nPixelMax) nPixelMax = nPixel;
  373.  
  374. if (nPixelMax > nThreshold && nPixelMax > p[0])
  375. p[0] = (byte)Math.Max(p[0], nPixelMax);
  376.  
  377. ++p;
  378. ++p2;
  379. }
  380.  
  381. p += nOffset + 3;
  382. p2 += nOffset + 3;
  383. }
  384. }
  385.  
  386. b.UnlockBits(bmData);
  387. b2.UnlockBits(bmData2);
  388.  
  389. return b;
  390. }
  391. #endregion
  392. #endregion

Message:


Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC