GDI+实例开源

来源:互联网 发布:小米6 手机网络不佳 编辑:程序博客网 时间:2024/06/14 23:16
  1. 原帖及讨论:http://bbs.bccn.net/thread-216343-1-1.html
  2. 本来是学c#的,但是由于喜欢web开发,所以winform学的不是怎么样,对GDI+一直是知道理论也没有什么实践,最近找了点资料,做了点东西,和大家分享分享,有什么地方做的不好,还希望大家指点……
  3. 谢谢……
  4. 开发工具:visual studio 2005
  5. Form1.cs页面:
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.Text;
  12. using System.Windows.Forms;
  13. namespace WindowsApplication6
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         private Bitmap smallBmp;
  18.         private Bitmap bmp;  //holds original loaded image
  19.         private Bitmap newbmp;  //holds latest version of image
  20.         private bool imageStatus = false;  //indicates image is loaded
  21.         private int resizeLevel;  //level image magnified/reduced
  22.         private Point lastPoint = Point.Empty;//tracks mouse movement
  23.         private Point origPoint = Point.Empty;//mouse down coordinates
  24.         private Rectangle rectSel;//select area
  25.         private bool selectStatus;//true if area selected
  26.         public Form1()
  27.         {
  28.             InitializeComponent();
  29.             panel1.MouseDown+=new MouseEventHandler(panel1_MouseDown);
  30.             panel1.MouseUp+=new MouseEventHandler(panel1_MouseUp);
  31.             panel1.MouseMove+=new MouseEventHandler(panel1_MouseMove);
  32.             panel1.Paint+=new PaintEventHandler(panel1_Paint);
  33.         }
  34.         private void Form1_Load(object sender, EventArgs e)
  35.         {
  36.         }
  37.         private void openMenuItem_Click(object sender, EventArgs e)
  38.         {
  39.             //load image from file
  40.             OpenFileDialog fd = new OpenFileDialog();
  41.             fd.InitialDirectory = @"D:/My Documents/My Pictures";
  42.             fd.Filter = "Image File|*.jpg;*.gif";
  43.             if (fd.ShowDialog() == DialogResult.OK)
  44.             {
  45.                 string fname = fd.FileName;
  46.                 using (Graphics g = panel1.CreateGraphics())
  47.                 {
  48.                     bmp = new Bitmap(fname);
  49.                     newbmp = bmp;
  50.                     g.FillRectangle(Brushes.White, 0, 0, panel1.Width, panel1.Height);
  51.                     Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
  52.                     g.DrawImage(bmp, r);
  53.                     imageStatus = true;
  54.                     Graphics gClear = panel2.CreateGraphics();
  55.                     gClear.FillRectangle(Brushes.White, 0, 0, panel2.Width, panel2.Height);
  56.                     gClear.Dispose();
  57.                 }
  58.             }
  59.         }
  60.         private void mirrorMenuItem_Click(object sender, EventArgs e)
  61.         {
  62.             Graphics g = panel1.CreateGraphics();
  63.             int h = newbmp.Height;
  64.             int w = newbmp.Width;
  65.             Point[] destPts ={
  66.                 new Point(w,0),
  67.                 new Point(0,0),
  68.                 new Point(w,h)
  69.             };
  70.             Bitmap tempBmp = new Bitmap(w, h);
  71.             Graphics gr = Graphics.FromImage(tempBmp);
  72.             gr.DrawImage(newbmp, destPts);
  73.             g.DrawImage(tempBmp, 0, 0);
  74.             newbmp = tempBmp;
  75.             g.Dispose();
  76.             gr.Dispose();
  77.         }
  78.         private void 翻转VToolStripMenuItem_Click(object sender, EventArgs e)
  79.         {
  80.             newbmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
  81.             Graphics g = panel1.CreateGraphics();
  82.             g.DrawImage(newbmp, 0, 0);
  83.             g.Dispose();
  84.         }
  85.         private void 刷屏RToolStripMenuItem_Click(object sender, EventArgs e)
  86.         {
  87.             panel1.Invalidate();
  88.             panel1.Update();
  89.             selectStatus = false;
  90.         }
  91.         private void panel1_Paint(object sender, PaintEventArgs e)
  92.         {
  93.             Graphics g = e.Graphics;
  94.             //redraw part of current image to panel
  95.             if (imageStatus)
  96.             {
  97.                 g.DrawImage(newbmp, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel);
  98.             }
  99.             base.OnPaint(e);
  100.         }
  101.         private void panel1_MouseDown(object sender, MouseEventArgs e)
  102.         {
  103.             if (lastPoint != Point.Empty)
  104.             {
  105.                 panel1.Invalidate(rectSel);
  106.                 panel1.Update();
  107.             }
  108.             lastPoint.X = e.X;
  109.             lastPoint.Y = e.Y;
  110.             origPoint = lastPoint;
  111.             selectStatus = true;
  112.         }
  113.         private void panel1_MouseUp(object sender, MouseEventArgs e)
  114.         {
  115.             rectSel.X = e.X;
  116.             if (e.X > origPoint.X)
  117.             {
  118.                 rectSel.X = origPoint.X;
  119.             }
  120.             rectSel.Y = origPoint.Y;
  121.             rectSel.Width = Math.Abs(e.X - origPoint.X) + 1;
  122.             rectSel.Height = Math.Abs(e.Y - origPoint.Y) + 1;
  123.             origPoint = Point.Empty;
  124.             if (rectSel.Width < 2)
  125.             {
  126.                 selectStatus = false;
  127.             }
  128.         }
  129.         private void panel1_MouseMove(object sender, MouseEventArgs e)
  130.         {
  131.             if (origPoint != Point.Empty)
  132.             {
  133.                 Rectangle r;
  134.                 Rectangle rd;
  135.                 int xop = origPoint.X;
  136.                 if (xop > lastPoint.X)
  137.                 {
  138.                     xop = lastPoint.X;
  139.                 }
  140.                 int w = Math.Abs(origPoint.X - lastPoint.X) + 1;
  141.                 int h = lastPoint.Y - origPoint.Y + 1;
  142.                 r = new Rectangle(xop, origPoint.Y, w, h);
  143.                 xop = e.X >= origPoint.X ? origPoint.X : e.X;
  144.                 w = Math.Abs(origPoint.X - e.X);
  145.                 h = e.Y - origPoint.Y;
  146.                 rd = new Rectangle(xop, origPoint.Y, w, h);
  147.                 Graphics g = panel1.CreateGraphics();
  148.                
  149.                 g.DrawImage(newbmp,r);
  150.                 //newbmp.MakeTransparent();
  151.                 g.DrawRectangle(Pens.Red, rd);
  152.                 g.Dispose();
  153.                 lastPoint.X = e.X;
  154.                 lastPoint.Y = e.Y;
  155.             }
  156.         }
  157.         private void 查看cToolStripMenuItem_Click(object sender, EventArgs e)
  158.         {
  159.             if (selectStatus)
  160.             {
  161.                 Graphics g = panel2.CreateGraphics();
  162.                 g.FillRectangle(Brushes.White, panel2.ClientRectangle);
  163.                 Rectangle rd = new Rectangle(0, 0, rectSel.Width, rectSel.Height);
  164.                 Bitmap temp = new Bitmap(rectSel.Width, rectSel.Height);
  165.                 Graphics gi = Graphics.FromImage(temp);
  166.                 gi.DrawImage(newbmp, rd, rectSel, GraphicsUnit.Pixel);
  167.                 smallBmp = temp;
  168.                 g.DrawImage(smallBmp, rd);
  169.                 g.Dispose();
  170.                 resizeLevel = 0;
  171.             }
  172.         }
  173.         private void button1_Click(object sender, EventArgs e)
  174.         {
  175.             Graphics g = panel2.CreateGraphics();
  176.             if (smallBmp != null)
  177.             {
  178.                 resizeLevel = resizeLevel + 1;
  179.                 float fac = (float)(1.0 + (resizeLevel * .25));
  180.                 int w = (int)(smallBmp.Width * fac);
  181.                 int h = (int)(smallBmp.Height * fac);
  182.                 Rectangle rd = new Rectangle(0, 0, w, h);
  183.                 Bitmap tempBmp = new Bitmap(w, h);
  184.                 Graphics gi = Graphics.FromImage(tempBmp);
  185.                 gi.DrawImage(smallBmp, rd);
  186.                 g.DrawImage(tempBmp,rd);
  187.                 gi.Dispose();
  188.             }
  189.             g.Dispose();
  190.         }
  191.         private void button2_Click(object sender, EventArgs e)
  192.         {
  193.             Graphics g = panel2.CreateGraphics();
  194.             if (smallBmp != null)
  195.             {
  196.                 resizeLevel = (resizeLevel > -3) ? resizeLevel - 1 : resizeLevel;
  197.                 float fac = (float)(1.0 + (resizeLevel * .25));
  198.                 int w = (int)(smallBmp.Width * fac);
  199.                 int h = (int)(smallBmp.Height * fac);
  200.                 Rectangle rd = new Rectangle(0, 0, w, h);
  201.                 Bitmap tempBmp = new Bitmap(w, h);
  202.           
  203.               
  204.                 Graphics gi = Graphics.FromImage(tempBmp);
  205.                 g.FillRectangle(Brushes.White, panel2.ClientRectangle);
  206.                 gi.DrawImage(smallBmp, rd);
  207.                 g.DrawImage(tempBmp, rd);
  208.            
  209.                 gi.Dispose();
  210.                 //panel2.Invalidate();
  211.                 //panel2.Update();
  212.             }
  213.          
  214.             g.Dispose();
  215.            
  216.         }
  217.     }
  218. Form1.Designer.cs页面
  219. namespace WindowsApplication6
  220. {
  221.     partial class Form1
  222.     {
  223.         /// <summary>
  224.         /// 必需的设计器变量。
  225.         /// </summary>
  226.         private System.ComponentModel.IContainer components = null;
  227.         /// <summary>
  228.         /// 清理所有正在使用的资源。
  229.         /// </summary>
  230.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  231.         protected override void Dispose(bool disposing)
  232.         {
  233.             if (disposing && (components != null))
  234.             {
  235.                 components.Dispose();
  236.             }
  237.             base.Dispose(disposing);
  238.         }
  239.         #region Windows 窗体设计器生成的代码
  240.         /// <summary>
  241.         /// 设计器支持所需的方法 - 不要
  242.         /// 使用代码编辑器修改此方法的内容。
  243.         /// </summary>
  244.         private void InitializeComponent()
  245.         {
  246.             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
  247.             this.menuStrip1 = new System.Windows.Forms.MenuStrip();
  248.             this.文件FToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  249.             this.openMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  250.             this.图像IToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  251.             this.mirrorMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  252.             this.屏幕SToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  253.             this.panel1 = new System.Windows.Forms.Panel();
  254.             this.panel2 = new System.Windows.Forms.Panel();
  255.             this.button1 = new System.Windows.Forms.Button();
  256.             this.button2 = new System.Windows.Forms.Button();
  257.             this.翻转VToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  258.             this.刷屏RToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  259.             this.查看cToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  260.             this.menuStrip1.SuspendLayout();
  261.             this.SuspendLayout();
  262.             // 
  263.             // menuStrip1
  264.             // 
  265.             this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
  266.             this.文件FToolStripMenuItem,
  267.             this.图像IToolStripMenuItem,
  268.             this.屏幕SToolStripMenuItem});
  269.             this.menuStrip1.Location = new System.Drawing.Point(0, 0);
  270.             this.menuStrip1.Name = "menuStrip1";
  271.             this.menuStrip1.Size = new System.Drawing.Size(655, 24);
  272.             this.menuStrip1.TabIndex = 0;
  273.             this.menuStrip1.Text = "menuStrip1";
  274.             // 
  275.             // 文件FToolStripMenuItem
  276.             // 
  277.             this.文件FToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
  278.             this.openMenuItem});
  279.             this.文件FToolStripMenuItem.Name = "文件FToolStripMenuItem";
  280.             this.文件FToolStripMenuItem.Size = new System.Drawing.Size(73, 20);
  281.             this.文件FToolStripMenuItem.Text = "文件(&F)";
  282.             // 
  283.             // openMenuItem
  284.             // 
  285.             this.openMenuItem.Name = "openMenuItem";
  286.             this.openMenuItem.Size = new System.Drawing.Size(114, 22);
  287.             this.openMenuItem.Text = "打开(&O)";
  288.             this.openMenuItem.Click += new System.EventHandler(this.openMenuItem_Click);
  289.             // 
  290.             // 图像IToolStripMenuItem
  291.             // 
  292.             this.图像IToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
  293.             this.mirrorMenuItem,
  294.             this.翻转VToolStripMenuItem,
  295.             this.查看cToolStripMenuItem});
  296.             this.图像IToolStripMenuItem.Name = "图像IToolStripMenuItem";
  297.             this.图像IToolStripMenuItem.Size = new System.Drawing.Size(71, 20);
  298.             this.图像IToolStripMenuItem.Text = "图像(&I)";
  299.             // 
  300.             // mirrorMenuItem
  301.             // 
  302.             this.mirrorMenuItem.Name = "mirrorMenuItem";
  303.             this.mirrorMenuItem.Size = new System.Drawing.Size(152, 22);
  304.             this.mirrorMenuItem.Text = "镜像(&M)";
  305.             this.mirrorMenuItem.Click += new System.EventHandler(this.mirrorMenuItem_Click);
  306.             // 
  307.             // 屏幕SToolStripMenuItem
  308.             // 
  309.             this.屏幕SToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
  310.             this.刷屏RToolStripMenuItem});
  311.             this.屏幕SToolStripMenuItem.Name = "屏幕SToolStripMenuItem";
  312.             this.屏幕SToolStripMenuItem.Size = new System.Drawing.Size(73, 20);
  313.             this.屏幕SToolStripMenuItem.Text = "屏幕(&S)";
  314.             // 
  315.             // panel1
  316.             // 
  317.             this.panel1.Location = new System.Drawing.Point(12, 27);
  318.             this.panel1.Name = "panel1";
  319.             this.panel1.Size = new System.Drawing.Size(327, 327);
  320.             this.panel1.TabIndex = 1;
  321.             // 
  322.             // panel2
  323.             // 
  324.             this.panel2.Location = new System.Drawing.Point(364, 27);
  325.             this.panel2.Name = "panel2";
  326.             this.panel2.Size = new System.Drawing.Size(273, 262);
  327.             this.panel2.TabIndex = 2;
  328.             // 
  329.             // button1
  330.             // 
  331.             this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
  332.             this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  333.             this.button1.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  334.             this.button1.Location = new System.Drawing.Point(437, 315);
  335.             this.button1.Name = "button1";
  336.             this.button1.Size = new System.Drawing.Size(39, 23);
  337.             this.button1.TabIndex = 3;
  338.             this.button1.Text = "+";
  339.             this.button1.UseVisualStyleBackColor = false;
  340.             this.button1.Click += new System.EventHandler(this.button1_Click);
  341.             // 
  342.             // button2
  343.             // 
  344.             this.button2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
  345.             this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  346.             this.button2.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  347.             this.button2.Location = new System.Drawing.Point(539, 315);
  348.             this.button2.Name = "button2";
  349.             this.button2.Size = new System.Drawing.Size(39, 23);
  350.             this.button2.TabIndex = 4;
  351.             this.button2.Text = "-";
  352.             this.button2.UseVisualStyleBackColor = false;
  353.             this.button2.Click += new System.EventHandler(this.button2_Click);
  354.             // 
  355.             // 翻转VToolStripMenuItem
  356.             // 
  357.             this.翻转VToolStripMenuItem.Name = "翻转VToolStripMenuItem";
  358.             this.翻转VToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
  359.             this.翻转VToolStripMenuItem.Text = "翻转(&V)";
  360.             this.翻转VToolStripMenuItem.Click += new System.EventHandler(this.翻转VToolStripMenuItem_Click);
  361.             // 
  362.             // 刷屏RToolStripMenuItem
  363.             // 
  364.             this.刷屏RToolStripMenuItem.Name = "刷屏RToolStripMenuItem";
  365.             this.刷屏RToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
  366.             this.刷屏RToolStripMenuItem.Text = "刷屏(&R)";
  367.             this.刷屏RToolStripMenuItem.Click += new System.EventHandler(this.刷屏RToolStripMenuItem_Click);
  368.             // 
  369.             // 查看cToolStripMenuItem
  370.             // 
  371.             this.查看cToolStripMenuItem.Name = "查看cToolStripMenuItem";
  372.             this.查看cToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
  373.             this.查看cToolStripMenuItem.Text = "查看(&c)";
  374.             this.查看cToolStripMenuItem.Click += new System.EventHandler(this.查看cToolStripMenuItem_Click);
  375.             // 
  376.             // Form1
  377.             // 
  378.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  379.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  380.             this.ClientSize = new System.Drawing.Size(655, 375);
  381.             this.Controls.Add(this.button2);
  382.             this.Controls.Add(this.button1);
  383.             this.Controls.Add(this.panel2);
  384.             this.Controls.Add(this.panel1);
  385.             this.Controls.Add(this.menuStrip1);
  386.             this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  387.             this.MainMenuStrip = this.menuStrip1;
  388.             this.Name = "Form1";
  389.             this.Text = "Form1";
  390.             this.Load += new System.EventHandler(this.Form1_Load);
  391.             this.menuStrip1.ResumeLayout(false);
  392.             this.menuStrip1.PerformLayout();
  393.             this.ResumeLayout(false);
  394.             this.PerformLayout();
  395.         }
  396.         #endregion
  397.         private System.Windows.Forms.MenuStrip menuStrip1;
  398.         private System.Windows.Forms.ToolStripMenuItem 文件FToolStripMenuItem;
  399.         private System.Windows.Forms.ToolStripMenuItem 图像IToolStripMenuItem;
  400.         private System.Windows.Forms.ToolStripMenuItem 屏幕SToolStripMenuItem;
  401.         private System.Windows.Forms.ToolStripMenuItem openMenuItem;
  402.         private System.Windows.Forms.Panel panel1;
  403.         private System.Windows.Forms.Panel panel2;
  404.         private System.Windows.Forms.Button button1;
  405.         private System.Windows.Forms.Button button2;
  406.         private System.Windows.Forms.ToolStripMenuItem mirrorMenuItem;
  407.         private System.Windows.Forms.ToolStripMenuItem 翻转VToolStripMenuItem;
  408.         private System.Windows.Forms.ToolStripMenuItem 刷屏RToolStripMenuItem;
  409.         private System.Windows.Forms.ToolStripMenuItem 查看cToolStripMenuItem;
  410.     }
  411. Progrem.cs页面
  412. using System;
  413. using System.Collections.Generic;
  414. using System.Windows.Forms;
  415. namespace WindowsApplication6
  416. {
  417.     static class Program
  418.     {
  419.         /// <summary>
  420.         /// 应用程序的主入口点。
  421.         /// </summary>
  422.         [STAThread]
  423.         static void Main()
  424.         {
  425.             Application.EnableVisualStyles();
  426.             Application.SetCompatibleTextRenderingDefault(false);
  427.             Application.Run(new Form1());
  428.         }
  429.     }