PictureBox 读取图片及绘画



Form1.cs
  文件如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
// myself
using System.Collections.Specialized;
using System.Text;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;


namespace haopai
{
    /// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        /// <summary>
        
/// 必需的设计器变量。
        
/// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            //
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }

        /// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        Windows 窗体设计器生成的代码

        /// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }
        private void Form1_Load(object sender, System.EventArgs e)
        {
              // GrayByPixels();  //灰度处理
        }

        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Label r;
        private System.Windows.Forms.TextBox valueBox;
        private System.Windows.Forms.Button getbutton;
        private System.Windows.Forms.TrackBar trackBar1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txtStartX;
        private System.Windows.Forms.TextBox txtStartY;
        private System.Windows.Forms.TextBox txtEndX;
        private System.Windows.Forms.TextBox txtEndY;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button btn_SavePoint; //灰度值
        
        protected  int  nHuidu = 0;
        public  Bitmap bmpobj;
        private Cursor inCursor;  //鼠标样式
        private Cursor MyCursor;  //鼠标样式
        private Point m_ptStart = new Point(0,0);
        private Point m_ptEnd = new Point(00);
        private bool m_bMouseDown = false;


        //读取图片
        private void getbutton_Click(object sender, System.EventArgs e)
        {
            try
            {
                bmpobj=null//初始化为空
                string path = Application.ExecutablePath.Substring(0,Application.ExecutablePath.LastIndexOf("\\"))+ "\\aa.jpg";
                Image get_Image = Image.FromFile(path); 
                pictureBox1.Image =get_Image;

                 GrayByPixels();  //灰度处理
                
//初始化trackBar1
                trackBar1.Value = nHuidu;
            }
            catch(ArgumentException)
            {
                MessageBox.Show("There was an error.Check the path to the image file.");
            }

        }
        //滚动条
        private void trackBar1_Scroll(object sender, System.EventArgs e)
        {
            int r,g,b;
            //Bitmap memBmp = new Bitmap(pictureBox1.Image);//(pictureBox1.Width,pictureBox1.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            UnCodebase () ;
            Bitmap memBmp = bmpobj;
            //Image userDrawAreaImage = Bitmap memBmp
            int nChang = trackBar1.Value - nHuidu;

            for(int x=0; x<pictureBox1.Image.Width; x++)
            {
                for(int y=0; y<pictureBox1.Image.Height; y++)
                {
                    Color pixelColor = memBmp.GetPixel(x, y);
                    //byte i = 0x01;
                    if(( (pixelColor.R + nChang) >=0) && ((pixelColor.R + nChang) <= 250)) r = pixelColor.R + nChang;
                    else r = pixelColor.R;
                    if(( (pixelColor.G + nChang) >=0) && ((pixelColor.G + nChang) <= 250)) g = pixelColor.G + nChang;
                    else g = pixelColor.G;
                    if(( (pixelColor.B + nChang) >=0) && ((pixelColor.B + nChang) <= 250)) b = pixelColor.B + nChang;
                    else b = pixelColor.B; 

                    //Color newColor = Color.FromArgb(r,g ,b);
                    
//memBmp.SetPixel(x, y, newColor);
                    memBmp.SetPixel(x, y, Color.FromArgb(r, g, b));
                }  
            } 
            //bmpobj = memBmp; //把图片传给全局变量
            
// GrayByPixels();
            
//Graphics memGp = Graphics.FromImage(memBmp);
            pictureBox1.Image = memBmp;
            GrayByPixels();  //灰度处理
            valueBox.Text = nHuidu.ToString();
        }    
        //pictureBox1 按下鼠标
        private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            m_ptStart = new Point(e.X, e.Y);
            txtStartX.Text = m_ptStart.X.ToString();
            txtStartY.Text = m_ptStart.Y.ToString();

            //if (!m_bMouseDown)
            
//{
                
//m_ptEnd = new Point(e.X, e.Y);
                
//txtEndX.Text = m_ptEnd.X.ToString();
                
//txtEndY.Text = m_ptEnd.Y.ToString();
            
//}
            m_bMouseDown = !m_bMouseDown;
        }
        //pictureBox1 移动鼠标
        private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try 
            {
                if (!m_bMouseDown == truereturn;  //判断是否按下 pictureBox1_MouseDown
                m_ptEnd = new Point(e.X, e.Y);
                this.pictureBox1.Refresh();

                if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 
                {
                    //m_ptEnd = new Point(e.X, e.Y);
                    MyCursor = new Cursor("cross_m.cur");
                    Cursor.Current = MyCursor;
        
                    //Rectangle rec = new Rectangle(Convert.ToInt32(txtStartX),Convert.ToInt32(txtStartY),Convert.ToInt32(txtEndX),Convert.ToInt32(txtEndY));
                    
//pictureBox1.Image.Size
                    
//Bitmap memBmp = bmpobj;
                    
//pictureBox1.Image = System.Drawing.Image.FromFile(selection.SubItems[1].Text);
                }
                //显示"+"指针
                else
                {
                    inCursor = new Cursor("pen_rm.cur");
                    Cursor.Current = inCursor;
                }
            }
            catch
            {
                if (inCursor != null)
                    inCursor.Dispose();
                if (MyCursor != null)
                    MyCursor.Dispose();
            }
        }
        //鼠标离开 pictureBox1
        private void pictureBox1_MouseLeave(object sender, System.EventArgs e)
        {
            if (inCursor != null)
                inCursor.Dispose();
            if (MyCursor != null)
                MyCursor.Dispose();
        }
        //pictureBox1 释放鼠标
        private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (!m_bMouseDown == truereturn
            m_ptEnd = new Point(e.X, e.Y);
            txtEndX.Text = m_ptEnd.X.ToString();
            txtEndY.Text = m_ptEnd.Y.ToString();
            m_bMouseDown = !m_bMouseDown;
            //
            
//Bitmap memBmp = new Bitmap(pictureBox1.Image);
            
//Graphics myGraphics = Graphics.FromImage(pictureBox1.Image);
            
//Pen blackPen = new Pen(Color.Black, 3);
            
//Rectangle rec = new Rectangle(Convert.ToInt32(txtStartX),Convert.ToInt32(txtStartY),Convert.ToInt32(txtEndX),Convert.ToInt32(txtEndY));
            
//myGraphics.DrawRectangle(blackPen, rec);
            
//pictureBox1.Image = Image.FromStream(myGraphics);
            
//myGraphics.Dispose();
        }
        //pictureBox1 点击图片
        private void pictureBox1_Click(object sender, System.EventArgs e)
        {
        
        }
        //pictureBox1 绘制图片
        private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            if (m_ptStart.Equals(m_ptEnd)) return;
            //if(pictureBox1.Image == null || CurX == 0) return;
            
//Pen blackPen = new Pen(Color.Black, 3);
            e.Graphics.DrawRectangle(System.Drawing.Pens.Yellow,m_ptStart.X,m_ptStart.Y,m_ptEnd.X - m_ptStart.X, m_ptEnd.Y - m_ptStart.Y);
            //this.pictureBox1.Refresh();
        }
        //保存截取图片点
        private void btn_SavePoint_Click(object sender, System.EventArgs e)
        {
        
        }

        / xg //
        
        public void UnCodebase () //(Bitmap pic)
        {
            if(bmpobj == null)
            {
               //Bitmap memBmp = new Bitmap(pictureBox1.Image);//(pictureBox1.Width,pictureBox1.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
               bmpobj = new Bitmap (pictureBox1.Image);//(pic);    //转换为Format32bppRgb
            }
        }

        /// <summary>
        
/// 根据RGB,计算灰度值
        
/// </summary>
        
/// <param name="posClr">Color值</param>
        
/// <returns>灰度值,整型</returns>
        private int GetGrayNumColor(System.Drawing.Color posClr)
        {
            return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472)>> 16;
        }

        /// <summary>
        
/// 灰度转换,逐点方式
        
/// </summary>
        public void GrayByPixels()  //灰度处理
        {
            // xg //
            int tmpValue = 0
            UnCodebase ();
            Bitmap memBmp = bmpobj;

            for (int i = 0; i < bmpobj.Height; i++)
            {
                for (int j = 0; j < bmpobj.Width; j++)
                {
                    //int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));
                    tmpValue += GetGrayNumColor(bmpobj.GetPixel(j, i));
                    //bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));
                }
            }
            tmpValue = tmpValue/(bmpobj.Height * bmpobj.Width);
            valueBox.Text = tmpValue.ToString();
            nHuidu = tmpValue; //全局灰度
        }

        /// <summary>
        
/// 去图形边框
        
/// </summary>
        
/// <param name="borderWidth"></param>
        public void ClearPicBorder(int borderWidth)
        {
            for (int i = 0; i < bmpobj.Height; i++)
            {
                for (int j = 0; j < bmpobj.Width; j++)
                {
                    if (i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth)
                        bmpobj.SetPixel(j, i, Color.FromArgb(255255255));
                }
            }
        }

    }
}



本文转自钢钢博客园博客,原文链接:http://www.cnblogs.com/xugang/archive/2007/12/24/1012665.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值