Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. //adding reference to library
  10. using System.Drawing.Imaging;
  11.  
  12. namespace PicColorSetter
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. private void Form1_Load(object sender, EventArgs e)
  21. {
  22. //setting initial values
  23. RedSelection.Value = 128;
  24. GreenSelection.Value = 128;
  25. BlueSelection.Value = 128;
  26. BrightnessSelection.Value = 128;
  27. SelectedColor.BackColor = Color.FromArgb(RedSelection.Value, GreenSelection.Value, BlueSelection.Value);
  28. ColorPictur();
  29. }
  30. private void scrColorComponent_Scroll(object sender, ScrollEventArgs e)
  31. {
  32. //redrawing & adjusting the selected color
  33. SelectedColor.BackColor = Color.FromArgb(RedSelection.Value, GreenSelection.Value, BlueSelection.Value);
  34. ColorPictur();
  35. }
  36. private void ColorPictur()
  37. {
  38. //applyig color
  39. picToned.Image = ToColorTone(picOriginal.Image, SelectedColor.BackColor);
  40. }
  41. private Bitmap ToColorTone(Image image, Color color)
  42. {
  43. //creating a new bitmap image with selected color.
  44. float scale = BrightnessSelection.Value / 128f;
  45.  
  46. float r = color.R / 255f * scale;
  47. float g = color.G / 255f * scale;
  48. float b = color.B / 255f * scale;
  49.  
  50. // Color Matrix
  51. ColorMatrix cm = new ColorMatrix(new float[][]
  52. {
  53. new float[] {r, 0, 0, 0, 0},
  54. new float[] {0, g, 0, 0, 0},
  55. new float[] {0, 0, b, 0, 0},
  56. new float[] {0, 0, 0, 1, 0},
  57. new float[] {0, 0, 0, 0, 1}
  58. });
  59. ImageAttributes ImAttribute = new ImageAttributes();
  60. ImAttribute.SetColorMatrix(cm);
  61.  
  62. //Color Matrix on new bitmap image
  63. Point[] points =
  64. {
  65. new Point(0, 0),
  66. new Point(image.Width - 1, 0),
  67. new Point(0, image.Height - 1),
  68. };
  69. Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
  70.  
  71. Bitmap myBitmap = new Bitmap(image.Width, image.Height);
  72. using (Graphics graphics = Graphics.FromImage(myBitmap))
  73. {
  74. graphics.DrawImage(image, points, rect, GraphicsUnit.Pixel, ImAttribute);
  75. }
  76. return myBitmap;
  77. }
  78.  
  79. private void SavePicBtn_Click(object sender, EventArgs e)
  80. {
  81. //saving image file
  82. SaveFileDialog sfd = new SaveFileDialog();
  83. sfd.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
  84.  
  85. sfd.ShowDialog();
  86. string filename = sfd.FileName;
  87. picToned.Image.Save(filename);
  88.  
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement