Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 7k
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. //要用到ImageFormat
  8. using System.Drawing.Imaging;
  9. namespace ch2_9
  10. {
  11. /// <summary>
  12. /// Form1 的摘要说明。
  13. /// </summary>
  14. public class Form1 : System.Windows.Forms.Form
  15. {
  16. private System.Windows.Forms.ToolBar toolBar1;
  17. private System.Windows.Forms.GroupBox groupBox1;
  18. private System.Windows.Forms.ImageList imageList1;
  19. private System.Windows.Forms.ToolBarButton toolBarButton1;
  20. private System.Windows.Forms.ToolBarButton toolBarButton2;
  21. private System.Windows.Forms.OpenFileDialog openFileDialog1;
  22. private System.ComponentModel.IContainer components;
  23. public Form1()
  24. {
  25. //
  26. // Windows 窗体设计器支持所必需的
  27. //
  28. InitializeComponent();
  29. //
  30. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  31. //
  32. }
  33. /// <summary>
  34. /// 清理所有正在使用的资源。
  35. /// </summary>
  36. protected override void Dispose( bool disposing )
  37. {
  38. if( disposing )
  39. {
  40. if (components != null) 
  41. {
  42. components.Dispose();
  43. }
  44. }
  45. base.Dispose( disposing );
  46. }
  47. #region Windows Form Designer generated code
  48. /// <summary>
  49. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  50. /// 此方法的内容。
  51. /// </summary>
  52. private void InitializeComponent()
  53. {
  54. this.components = new System.ComponentModel.Container();
  55. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  56. this.toolBar1 = new System.Windows.Forms.ToolBar();
  57. this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
  58. this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
  59. this.imageList1 = new System.Windows.Forms.ImageList(this.components);
  60. this.groupBox1 = new System.Windows.Forms.GroupBox();
  61. this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  62. this.SuspendLayout();
  63. // 
  64. // toolBar1
  65. // 
  66. this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
  67. this.toolBarButton1,
  68. this.toolBarButton2});
  69. this.toolBar1.DropDownArrows = true;
  70. this.toolBar1.ImageList = this.imageList1;
  71. this.toolBar1.Name = "toolBar1";
  72. this.toolBar1.ShowToolTips = true;
  73. this.toolBar1.Size = new System.Drawing.Size(1016, 37);
  74. this.toolBar1.TabIndex = 1;
  75. this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
  76. // 
  77. // toolBarButton1
  78. // 
  79. this.toolBarButton1.ImageIndex = 0;
  80. this.toolBarButton1.Text = "打开";
  81. // 
  82. // toolBarButton2
  83. // 
  84. this.toolBarButton2.Enabled = false;
  85. this.toolBarButton2.ImageIndex = 1;
  86. this.toolBarButton2.Text = "转换";
  87. // 
  88. // imageList1
  89. // 
  90. this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
  91. this.imageList1.ImageSize = new System.Drawing.Size(16, 15);
  92. this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
  93. this.imageList1.TransparentColor = System.Drawing.Color.White;
  94. // 
  95. // groupBox1
  96. // 
  97. this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
  98. this.groupBox1.Location = new System.Drawing.Point(0, 37);
  99. this.groupBox1.Name = "groupBox1";
  100. this.groupBox1.Size = new System.Drawing.Size(1016, 8);
  101. this.groupBox1.TabIndex = 2;
  102. this.groupBox1.TabStop = false;
  103. // 
  104. // Form1
  105. // 
  106. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  107. this.ClientSize = new System.Drawing.Size(1016, 741);
  108. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  109.   this.groupBox1,
  110.   this.toolBar1});
  111. this.IsMdiContainer = true;
  112. this.Name = "Form1";
  113. this.Text = "图片格式转换器";
  114. this.ResumeLayout(false);
  115. }
  116. #endregion
  117. /// <summary>
  118. /// 应用程序的主入口点。
  119. /// </summary>
  120. [STAThread]
  121. static void Main() 
  122. {
  123. Application.Run(new Form1());
  124. }
  125. public  void UpdateToolBar()
  126. {
  127. //说明是最后一个子窗体了
  128. if (this.MdiChildren.Length==1)
  129. {
  130. this.toolBarButton2.Enabled=false;
  131. }
  132. }
  133. private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  134. {
  135. //点击的是打开按钮的处理
  136. if (e.Button==this.toolBarButton1)
  137. {
  138. //设置文件类型过滤器 
  139. this.openFileDialog1.Filter="Image files (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.png" 
  140. + "|JPeg files (*.jpg;*.jpeg)|*.jpg;*.jpeg" 
  141. + "|GIF files (*.gif)|*.gif" 
  142. + "|BMP files (*.bmp)|*.bmp" 
  143. + "|Tiff files (*.tif;*.tiff)|*.tif;*.tiff" 
  144. + "|Png files (*.png)|*.png" 
  145. + "|All files (*.*)|*.*";
  146. if (this.openFileDialog1.ShowDialog()==DialogResult.OK)
  147. {
  148. //获取文件的详细路径
  149. string sFileName=this.openFileDialog1.FileName;
  150. //去掉路径获得文件名
  151. string sShortName=sFileName.Substring(sFileName.LastIndexOf("\")+1);
  152. Bitmap bm=new Bitmap(sFileName);
  153. Form2 fm=new Form2();
  154. //将文件名数据传给子窗体的pictureBox
  155. fm.pictureBox1.Image=bm;
  156. fm.Width=bm.Width;
  157. fm.Height=bm.Height+20;
  158. fm.Text=sShortName;
  159. //定义此窗体的父窗体,从而此窗体成为一个MDI窗体 
  160. fm.MdiParent=this;
  161. fm.Show();
  162. //显示图片之后就可以使用转换按钮了
  163. this.toolBarButton2.Enabled=true;
  164. }
  165. }
  166. //点击的是转换按钮的处理程序
  167. if (e.Button==this.toolBarButton2)
  168. {
  169. Form3 fm3=new Form3();
  170. if (fm3.ShowDialog()==DialogResult.OK)
  171. {
  172. //进行图片转换,获取从fm3中取得的信息,设定文件转换格式
  173. string  fileFormat=fm3.listView1.SelectedItems[0].Text;
  174. ImageFormat iFormat=null;
  175. switch  (fileFormat[0].ToString())
  176. {
  177. case "B":
  178. iFormat=ImageFormat.Bmp;
  179. break;
  180. case "J":
  181. iFormat=ImageFormat.Jpeg;
  182. break;
  183. case "T":
  184. iFormat=ImageFormat.Tiff;
  185. break;
  186. case "P":
  187. iFormat=ImageFormat.Png;
  188. break;
  189. case  "G":
  190. iFormat=ImageFormat.Gif;
  191. break;
  192. }
  193. string  filePath=fm3.textBox1.Text;
  194. //获取当前子窗体的图像对象
  195. Form2 fm2=(Form2)this.ActiveMdiChild;
  196. Bitmap bm=(Bitmap)fm2.pictureBox1.Image;
  197. try
  198. {
  199. //将文件格式转化
  200. bm.Save(filePath,iFormat);
  201. }
  202. catch(Exception error)
  203. {
  204. MessageBox.Show(" 错误信息是:"+error.Message,"警告",
  205. MessageBoxButtons.OK,MessageBoxIcon.Error);
  206. }
  207. }    
  208.             
  209.  }
  210.          
  211.  }//end toolBar1_ButtonClick
  212. }
  213. }