Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 3k
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. using System.Drawing.Drawing2D;
  9. namespace ch2_6
  10. {
  11. /// <summary>
  12. /// Form1 的摘要说明。
  13. /// </summary>
  14. public class Form1 : System.Windows.Forms.Form
  15. {
  16. /// <summary>
  17. /// 必需的设计器变量。
  18. /// </summary>
  19. private System.ComponentModel.Container components = null;
  20. private System.Windows.Forms.Button button1;
  21. private Image bm;
  22. public Form1()
  23. {
  24. //
  25. // Windows 窗体设计器支持所必需的
  26. //
  27. InitializeComponent();
  28. string s="football.jpg";
  29.             bm= new Bitmap(s);
  30. //
  31. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  32. //
  33. }
  34. /// <summary>
  35. /// 清理所有正在使用的资源。
  36. /// </summary>
  37. protected override void Dispose( bool disposing )
  38. {
  39. if( disposing )
  40. {
  41. if (components != null) 
  42. {
  43. components.Dispose();
  44. }
  45. }
  46. base.Dispose( disposing );
  47. }
  48. #region Windows Form Designer generated code
  49. /// <summary>
  50. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  51. /// 此方法的内容。
  52. /// </summary>
  53. private void InitializeComponent()
  54. {
  55. this.button1 = new System.Windows.Forms.Button();
  56. this.SuspendLayout();
  57. // 
  58. // button1
  59. // 
  60. this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  61. this.button1.Location = new System.Drawing.Point(400, 344);
  62. this.button1.Name = "button1";
  63. this.button1.Size = new System.Drawing.Size(96, 48);
  64. this.button1.TabIndex = 0;
  65. this.button1.Text = "保存图像";
  66. this.button1.Click += new System.EventHandler(this.button1_Click);
  67. // 
  68. // Form1
  69. // 
  70. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  71. this.ClientSize = new System.Drawing.Size(544, 513);
  72. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  73.   this.button1});
  74. this.Name = "Form1";
  75. this.Text = "图像显示与保存";
  76. this.ResumeLayout(false);
  77. }
  78. #endregion
  79. /// <summary>
  80. /// 应用程序的主入口点。
  81. /// </summary>
  82. [STAThread]
  83.     protected override void OnPaint(PaintEventArgs e) 
  84. {
  85. Graphics g=e.Graphics;
  86. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  87. g.FillRectangle(new SolidBrush(Color.White), this.ClientRectangle);
  88. //绘制图片
  89.     g.DrawImage(bm,0,0,bm.Width,bm.Height);
  90.     //绘制一个只有原图1/3大小的图片
  91. g.DrawImage(bm,0,340,(int)(bm.Width/3),(int)(bm.Height/3));
  92. //现在旋转该图像
  93. g.RotateTransform(-30);  
  94. //设置平移距离
  95. g.TranslateTransform(210,420,MatrixOrder.Append);
  96. //显示图像
  97. g.DrawImage(bm,0, 0,(int)(bm.Width/3),(int)(bm.Height/3));
  98.     g.ResetTransform();
  99. }
  100. static void Main() 
  101. {
  102. Application.Run(new Form1());
  103. }
  104. private void button1_Click(object sender, System.EventArgs e)
  105. {
  106. //构造一个和要保存图片尺寸相同的图像
  107. Bitmap image = new Bitmap(this.bm.Width, this.bm.Height);
  108. //获取Graphics对象
  109. Graphics g = Graphics.FromImage(image);
  110. g.Clear(this.BackColor);
  111. //调用Form1_Paint方法在图像上绘制图形
  112.     Rectangle rect=new Rectangle(0,0,this.bm.Width,this.bm.Height);
  113. OnPaint(new PaintEventArgs(g,rect));
  114. //保存图像
  115. image.Save("SavedFootball.jpg", ImageFormat.Jpeg);
  116. MessageBox.Show("图片已经保存到当前项目的bin\Debug目录下,名为SavedFootball.jpg");
  117. }
  118. }
  119. }