Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 4k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Drawing.Imaging;
  8. namespace ch2_10
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. /// <summary>
  16. /// 必需的设计器变量。
  17. /// </summary>
  18. private System.ComponentModel.Container components = null;
  19. public Form1()
  20. {
  21. //
  22. // Windows 窗体设计器支持所必需的
  23. //
  24. InitializeComponent();
  25. //
  26. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  27. //
  28. }
  29. /// <summary>
  30. /// 清理所有正在使用的资源。
  31. /// </summary>
  32. protected override void Dispose( bool disposing )
  33. {
  34. if( disposing )
  35. {
  36. if (components != null) 
  37. {
  38. components.Dispose();
  39. }
  40. }
  41. base.Dispose( disposing );
  42. }
  43. #region Windows Form Designer generated code
  44. /// <summary>
  45. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  46. /// 此方法的内容。
  47. /// </summary>
  48. private void InitializeComponent()
  49. {
  50. // 
  51. // Form1
  52. // 
  53. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  54. this.ClientSize = new System.Drawing.Size(368, 273);
  55. this.Name = "Form1";
  56. this.Text = "图形颜色变换";
  57. }
  58. #endregion
  59. protected override void OnPaint(PaintEventArgs e)
  60. {
  61. Graphics g=e.Graphics;
  62.        
  63. //载入图片
  64. Image image = new Bitmap("original.bmp");
  65. //创建一个ImageAttributes类实例
  66. ImageAttributes imageAttributes = new ImageAttributes();
  67.             float[][] colorMatrixElements = {  
  68. //将红色因子乘以2
  69. new float[] {2,  0,  0,  0, 0},       
  70. //绿色因子不变
  71. new float[] {0,  1,  0,  0, 0},        
  72. //蓝色因子乘以1.5
  73. new float[] {0,  0,  1,  0, 0},        
  74. new float[] {0,  0,  0,  1, 0},      
  75. //每个分量都添加0.2
  76. new float[] {0.2f, 0.2f, 0.2f, 0, 1}
  77. };    
  78. ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
  79. //使用ImageAttributes类的SetColorMatrix方法为colorMatrix对象设置ColorMatrixFlag枚举
  80. imageAttributes.SetColorMatrix(
  81. colorMatrix, 
  82. ColorMatrixFlag.Default,
  83. ColorAdjustType.Bitmap);
  84. //显示原来的图片
  85. g.DrawImage(image, 10, 10);
  86. g.DrawString("原来的图片",this.Font,new SolidBrush(Color.Blue),10,100);
  87. //显示变换后的图片
  88. g.DrawImage(
  89. image, 
  90. //画图区
  91. new Rectangle(120, 10, image.Width, image.Height),  
  92. //源图的左上角
  93. 0, 0,       
  94. //源图的宽度
  95. image.Width,
  96. //源图的高度
  97. image.Height,    
  98. GraphicsUnit.Pixel,
  99. imageAttributes);
  100. g.DrawString("单色变换后的图片",this.Font,new SolidBrush(Color.Blue),120,100);
  101.     
  102. Image image1 = new Bitmap("color.bmp");
  103. ImageAttributes imageAttributes1 = new ImageAttributes();
  104.             float[][] colorMatrixElements1 = { 
  105. new float[] {1,  0,  0,  0, 0},
  106. new float[] {0,  1,  0,  0, 0},
  107. new float[] {0,  0,  1,  0, 0},
  108. new float[] {0,  0,  0,  1, 0},
  109. //其它因子不变,仅仅红色分量添加0.80
  110. new float[] {0.80f, 0, 0, 0, 1}};
  111. ColorMatrix colorMatrix1 = new ColorMatrix(colorMatrixElements1);
  112. //使用ImageAttributes类的SetColorMatrix方法为colorMatrix对象设置ColorMatrixFlag枚举
  113. imageAttributes1.SetColorMatrix(
  114. colorMatrix1, 
  115. ColorMatrixFlag.Default,
  116. ColorAdjustType.Bitmap);
  117.             //显示原来的图片
  118. g.DrawImage(image1, 10, 140, image1.Width, image1.Height);
  119. g.DrawString("原来的图片",this.Font,new SolidBrush(Color.Blue),10,230);
  120. //显示变换后的图片
  121. g.DrawImage(
  122. image1,
  123. //画图区
  124. new Rectangle(150, 140, image1.Width, image1.Height), 
  125. //源图的左上角
  126. 0, 0,        
  127. //源图的宽度
  128. image1.Width,       
  129. //源图的宽度
  130. image1.Height,     
  131. GraphicsUnit.Pixel,
  132. imageAttributes1);
  133.    g.DrawString("颜色平移后的图片",this.Font,new SolidBrush(Color.Blue),150,230);
  134. }
  135. /// <summary>
  136. /// 应用程序的主入口点。
  137. /// </summary>
  138. [STAThread]
  139. static void Main() 
  140. {
  141. Application.Run(new Form1());
  142. }
  143. }
  144. }