| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 11880 人关注过本帖, 5 人收藏
标题:GDI+实例开源
只看楼主 加入收藏
hebingbing
Rank: 6Rank: 6
来 自:黄土高坡
等 级:贵宾
威 望:27
帖 子:3417
专家分:371
注 册:2007-10-22
结帖率:84.21%
收藏(5)
 问题点数:0 回复次数:36 
GDI+实例开源
本来是学c#的,但是由于喜欢web开发,所以winform学的不是怎么样,对GDI+一直是知道理论也没有什么实践,最近找了点资料,做了点东西,和大家分享分享,有什么地方做的不好,还希望大家指点……
谢谢……
开发工具:visual studio 2005
Form1.cs页面:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication6
{
    public partial class Form1 : Form
    {
        private Bitmap smallBmp;
        private Bitmap bmp;  //holds original loaded image
        private Bitmap newbmp;  //holds latest version of image
        private bool imageStatus = false;  //indicates image is loaded
        private int resizeLevel;  //level image magnified/reduced
        private Point lastPoint = Point.Empty;//tracks mouse movement
        private Point origPoint = Point.Empty;//mouse down coordinates
        private Rectangle rectSel;//select area
        private bool selectStatus;//true if area selected
        public Form1()
        {
            InitializeComponent();
            panel1.MouseDown+=new MouseEventHandler(panel1_MouseDown);
            panel1.MouseUp+=new MouseEventHandler(panel1_MouseUp);
            panel1.MouseMove+=new MouseEventHandler(panel1_MouseMove);
            panel1.Paint+=new PaintEventHandler(panel1_Paint);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void openMenuItem_Click(object sender, EventArgs e)
        {
            //load image from file
            OpenFileDialog fd = new OpenFileDialog();
            fd.InitialDirectory = @"D:\My Documents\My Pictures";
            fd.Filter = "Image File|*.jpg;*.gif";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                string fname = fd.FileName;
                using (Graphics g = panel1.CreateGraphics())
                {
                    bmp = new Bitmap(fname);
                    newbmp = bmp;
                    g.FillRectangle(Brushes.White, 0, 0, panel1.Width, panel1.Height);
                    Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
                    g.DrawImage(bmp, r);
                    imageStatus = true;
                    Graphics gClear = panel2.CreateGraphics();

                    gClear.FillRectangle(Brushes.White, 0, 0, panel2.Width, panel2.Height);
                    gClear.Dispose();
                }
            }
        }

        private void mirrorMenuItem_Click(object sender, EventArgs e)
        {
            Graphics g = panel1.CreateGraphics();
            int h = newbmp.Height;
            int w = newbmp.Width;
            Point[] destPts ={
                new Point(w,0),
                new Point(0,0),
                new Point(w,h)
            };
            Bitmap tempBmp = new Bitmap(w, h);
            Graphics gr = Graphics.FromImage(tempBmp);
            gr.DrawImage(newbmp, destPts);
            g.DrawImage(tempBmp, 0, 0);
            newbmp = tempBmp;
            g.Dispose();
            gr.Dispose();
        }

        private void 翻转VToolStripMenuItem_Click(object sender, EventArgs e)
        {
            newbmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
            Graphics g = panel1.CreateGraphics();
            g.DrawImage(newbmp, 0, 0);
            g.Dispose();
        }

        private void 刷屏RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Invalidate();
            panel1.Update();
            selectStatus = false;
        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            //redraw part of current image to panel
            if (imageStatus)
            {
                g.DrawImage(newbmp, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel);
            }
            base.OnPaint(e);
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (lastPoint != Point.Empty)
            {
                panel1.Invalidate(rectSel);
                panel1.Update();
            }
            lastPoint.X = e.X;
            lastPoint.Y = e.Y;
            origPoint = lastPoint;
            selectStatus = true;
        }
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            rectSel.X = e.X;
            if (e.X > origPoint.X)
            {
                rectSel.X = origPoint.X;
            }
            rectSel.Y = origPoint.Y;
            rectSel.Width = Math.Abs(e.X - origPoint.X) + 1;
            rectSel.Height = Math.Abs(e.Y - origPoint.Y) + 1;
            origPoint = Point.Empty;
            if (rectSel.Width < 2)
            {
                selectStatus = false;
            }
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (origPoint != Point.Empty)
            {
                Rectangle r;
                Rectangle rd;
                int xop = origPoint.X;
                if (xop > lastPoint.X)
                {
                    xop = lastPoint.X;
                }
                int w = Math.Abs(origPoint.X - lastPoint.X) + 1;
                int h = lastPoint.Y - origPoint.Y + 1;
                r = new Rectangle(xop, origPoint.Y, w, h);
                xop = e.X >= origPoint.X ? origPoint.X : e.X;
                w = Math.Abs(origPoint.X - e.X);
                h = e.Y - origPoint.Y;
                rd = new Rectangle(xop, origPoint.Y, w, h);
                Graphics g = panel1.CreateGraphics();
               
                g.DrawImage(newbmp,r);
                //newbmp.MakeTransparent();
                g.DrawRectangle(Pens.Red, rd);
                g.Dispose();
                lastPoint.X = e.X;
                lastPoint.Y = e.Y;
            }
        }

        private void 查看cToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectStatus)
            {
                Graphics g = panel2.CreateGraphics();
                g.FillRectangle(Brushes.White, panel2.ClientRectangle);
                Rectangle rd = new Rectangle(0, 0, rectSel.Width, rectSel.Height);
                Bitmap temp = new Bitmap(rectSel.Width, rectSel.Height);
                Graphics gi = Graphics.FromImage(temp);
                gi.DrawImage(newbmp, rd, rectSel, GraphicsUnit.Pixel);
                smallBmp = temp;
                g.DrawImage(smallBmp, rd);
                g.Dispose();
                resizeLevel = 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = panel2.CreateGraphics();
            if (smallBmp != null)
            {
                resizeLevel = resizeLevel + 1;
                float fac = (float)(1.0 + (resizeLevel * .25));
                int w = (int)(smallBmp.Width * fac);
                int h = (int)(smallBmp.Height * fac);
                Rectangle rd = new Rectangle(0, 0, w, h);
                Bitmap tempBmp = new Bitmap(w, h);
                Graphics gi = Graphics.FromImage(tempBmp);
                gi.DrawImage(smallBmp, rd);
                g.DrawImage(tempBmp,rd);
                gi.Dispose();
            }
            g.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics g = panel2.CreateGraphics();
            if (smallBmp != null)
            {
                resizeLevel = (resizeLevel > -3) ? resizeLevel - 1 : resizeLevel;
                float fac = (float)(1.0 + (resizeLevel * .25));
                int w = (int)(smallBmp.Width * fac);
                int h = (int)(smallBmp.Height * fac);
                Rectangle rd = new Rectangle(0, 0, w, h);
                Bitmap tempBmp = new Bitmap(w, h);
         
              
                Graphics gi = Graphics.FromImage(tempBmp);
                g.FillRectangle(Brushes.White, panel2.ClientRectangle);
                gi.DrawImage(smallBmp, rd);
                g.DrawImage(tempBmp, rd);
           
                gi.Dispose();
                //panel2.Invalidate();
                //panel2.Update();
            }
         
            g.Dispose();
           
        }
    }
}
Form1.Designer.cs页面
namespace WindowsApplication6
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
             resources = new (typeof(Form1));
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.文件FToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.openMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.图像IToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.mirrorMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.屏幕SToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel2 = new System.Windows.Forms.Panel();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.翻转VToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.刷屏RToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.查看cToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            //
            // menuStrip1
            //
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.文件FToolStripMenuItem,
            this.图像IToolStripMenuItem,
            this.屏幕SToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(655, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            //
            // 文件FToolStripMenuItem
            //
            this.文件FToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.openMenuItem});
            this.文件FToolStripMenuItem.Name = "文件FToolStripMenuItem";
            this.文件FToolStripMenuItem.Size = new System.Drawing.Size(73, 20);
            this.文件FToolStripMenuItem.Text = "文件(&F)";
            //
            // openMenuItem
            //
            this.openMenuItem.Name = "openMenuItem";
            this.openMenuItem.Size = new System.Drawing.Size(114, 22);
            this.openMenuItem.Text = "打开(&O)";
            this.openMenuItem.Click += new System.EventHandler(this.openMenuItem_Click);
            //
            // 图像IToolStripMenuItem
            //
            this.图像IToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mirrorMenuItem,
            this.翻转VToolStripMenuItem,
            this.查看cToolStripMenuItem});
            this.图像IToolStripMenuItem.Name = "图像IToolStripMenuItem";
            this.图像IToolStripMenuItem.Size = new System.Drawing.Size(71, 20);
            this.图像IToolStripMenuItem.Text = "图像(&I)";
            //
            // mirrorMenuItem
            //
            this.mirrorMenuItem.Name = "mirrorMenuItem";
            this.mirrorMenuItem.Size = new System.Drawing.Size(152, 22);
            this.mirrorMenuItem.Text = "镜像(&M)";
            this.mirrorMenuItem.Click += new System.EventHandler(this.mirrorMenuItem_Click);
            //
            // 屏幕SToolStripMenuItem
            //
            this.屏幕SToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.刷屏RToolStripMenuItem});
            this.屏幕SToolStripMenuItem.Name = "屏幕SToolStripMenuItem";
            this.屏幕SToolStripMenuItem.Size = new System.Drawing.Size(73, 20);
            this.屏幕SToolStripMenuItem.Text = "屏幕(&S)";
            //
            // panel1
            //
            this.panel1.Location = new System.Drawing.Point(12, 27);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(327, 327);
            this.panel1.TabIndex = 1;
            //
            // panel2
            //
            this.panel2.Location = new System.Drawing.Point(364, 27);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(273, 262);
            this.panel2.TabIndex = 2;
            //
            // button1
            //
            this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button1.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.button1.Location = new System.Drawing.Point(437, 315);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(39, 23);
            this.button1.TabIndex = 3;
            this.button1.Text = "+";
            this.button1.UseVisualStyleBackColor = false;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
            this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button2.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.button2.Location = new System.Drawing.Point(539, 315);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(39, 23);
            this.button2.TabIndex = 4;
            this.button2.Text = "-";
            this.button2.UseVisualStyleBackColor = false;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // 翻转VToolStripMenuItem
            //
            this.翻转VToolStripMenuItem.Name = "翻转VToolStripMenuItem";
            this.翻转VToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.翻转VToolStripMenuItem.Text = "翻转(&V)";
            this.翻转VToolStripMenuItem.Click += new System.EventHandler(this.翻转VToolStripMenuItem_Click);
            //
            // 刷屏RToolStripMenuItem
            //
            this.刷屏RToolStripMenuItem.Name = "刷屏RToolStripMenuItem";
            this.刷屏RToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.刷屏RToolStripMenuItem.Text = "刷屏(&R)";
            this.刷屏RToolStripMenuItem.Click += new System.EventHandler(this.刷屏RToolStripMenuItem_Click);
            //
            // 查看cToolStripMenuItem
            //
            this.查看cToolStripMenuItem.Name = "查看cToolStripMenuItem";
            this.查看cToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.查看cToolStripMenuItem.Text = "查看(&c)";
            this.查看cToolStripMenuItem.Click += new System.EventHandler(this.查看cToolStripMenuItem_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(655, 375);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.menuStrip1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem 文件FToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 图像IToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 屏幕SToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem openMenuItem;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.ToolStripMenuItem mirrorMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 翻转VToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 刷屏RToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 查看cToolStripMenuItem;
    }
}
Progrem.cs页面
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication6
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
效果图:
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册


[[it] 本帖最后由 hebingbing 于 2008-6-22 16:14 编辑 [/it]]
搜索更多相关主题的帖子: GDI using 实例 System web 
2008-05-28 18:02
killer_l
Rank: 2
等 级:新手上路
威 望:3
帖 子:1139
专家分:0
注 册:2007-5-25
收藏
得分:0 
回复可见,真邪恶啊

2008-06-22 15:25
hebingbing
Rank: 6Rank: 6
来 自:黄土高坡
等 级:贵宾
威 望:27
帖 子:3417
专家分:371
注 册:2007-10-22
收藏
得分:0 
不是,我在插入代码的时候可能是误操作,现在改过来了……
实在不好意思……
2008-06-22 16:15
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
可以
2008-06-22 18:07
andy820
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-12-9
收藏
得分:0 
123
看看
2008-06-29 18:54
wang1006tao
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-6-23
收藏
得分:0 
hao kankan!!!!!!!!
2008-06-29 21:35
dadongdong511
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-7-1
收藏
得分:0 
不错不错
路过学习.
2008-07-01 09:31
yoowa
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-7-17
收藏
得分:0 
学习中  不错 不错
2008-07-17 14:38
closey
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-7-20
收藏
得分:0 
学习。。。
2008-07-20 21:01
taoszyz
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-7-22
收藏
得分:0 
好东西,下下来研究研究
2008-07-22 09:33
快速回复:GDI+实例开源
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039243 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved