c# winform pictureBox

1:

c# winform pictureBox如何突出显示,放大并给pictureBox边框变色

PictureBox old = null;
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            PictureBox p = (PictureBox)sender;
            if (p == old) return;

            if (old != null)
            {
                old.Width -= 10;
                old.Height -= 10;
                old.Location = new Point(old.Location.X + 5, old.Location.Y + 5);
            }

            old = p;
            p.Width += 10;
            p.Height += 10;
            p.Location = new Point(p.Location.X - 5, p.Location.Y - 5);
            p.BringToFront();
          

        }

        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            PictureBox p = (PictureBox)sender;
            if (p == old)
            {
                Pen pp = new Pen(Color.Red);
                e.Graphics.DrawRectangle(pp, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.X + e.ClipRectangle.Width - 1, e.ClipRectangle.Y + e.ClipRectangle.Height - 1);
            }
        }

转载:http://heisetoufa.iteye.com/blog/227802

2:

  1. /* 
  2.  * 主要是对 PictureBox 的操作 
  3.  * 截取屏幕图像 
  4.  * 获取屏幕指定像素颜色 
  5.  * 旋转和翻转图片 
  6.  * 虚拟选择框绘制 
  7.  * 控件绘图 
  8.  *  
  9.  * 为了方便,所有步骤在一个方法中完成 
  10.  * 创建了一个新的 Form 对象 
  11.  * 在该 Form 中添加了一个 PictureBox 对象 
  12. */  
  13. // 在 Form_Load 中调用   
  14. private void CSDNSample_PictureBoxHandle()  
  15. {  
  16.     // 复制屏幕,创建原始图片   
  17.     Bitmap image = new Bitmap(  
  18.         Screen.PrimaryScreen.Bounds.Width,  
  19.         Screen.PrimaryScreen.Bounds.Height);  
  20.     using (Graphics g = Graphics.FromImage(image))  
  21.     {  
  22.         g.CopyFromScreen(  
  23.             0, 0, 0, 0,  
  24.             image.Size,  
  25.             CopyPixelOperation.SourceCopy);  
  26.         g.Save();  
  27.     }  
  28.     // 创建 PictureBox 对象   
  29.     PictureBox box = new PictureBox();  
  30.     box.Location = new Point(0, 0);  
  31.     box.SizeMode = PictureBoxSizeMode.StretchImage;  
  32.     box.Size = image.Size;  
  33.     box.Image = image;  
  34.     // 创建 Form 对象   
  35.     Form f = new Form();  
  36.     f.AutoScroll = true// 自动滚动   
  37.     f.Size = new Size(800, 600);  
  38.     // From_FormClosed 用于释放资源   
  39.     f.FormClosed += (sender, e) => { image.Dispose(); };  
  40.     // From_MouseWheel 用于缩放图片   
  41.     // 由于滚动条和缩放同时进行 会有点不爽   
  42.     // 建议右键菜单或 Control/Shift/Alt + 鼠标滚轮组合实现   
  43.     f.MouseWheel += (sender, e) =>  
  44.     {  
  45.         if (e.Delta > 0)  
  46.         {  
  47.             // 限制放大效果 600% 以内   
  48.             if (box.Width / image.Width < 6)  
  49.             {  
  50.                 box.Size = new Size(  
  51.                     (int)(box.Width * 1.1),  
  52.                     (int)(box.Height * 1.1));  
  53.             }  
  54.         }  
  55.         else  
  56.         {  
  57.             // 限制缩小效果 15% 以内   
  58.             if ((float)box.Width / (float)image.Width > 0.15f)  
  59.             {  
  60.                 box.Size = new Size(  
  61.                     (int)(box.Width * 0.9),  
  62.                     (int)(box.Height * 0.9));  
  63.             }  
  64.         }  
  65.     };  
  66.     // 用于记录鼠标按下时的位置   
  67.     Point pt = Point.Empty;  
  68.     // PictureBox_MouseDown 用于记录鼠标位置 绘制屏幕十字标记   
  69.     box.MouseDown += (sender, e) =>  
  70.     {  
  71.         if (e.Button == MouseButtons.Left)  
  72.         {  
  73.             pt = e.Location;  
  74.             // Cursor.Position 获取全局鼠标位置(屏幕位置)   
  75.             // CopyFromScreen 拷贝 1 个屏幕像素并生成图片   
  76.             // Bitmap.GetPixel 获取图片上指定像素的颜色   
  77.             using (Bitmap bitmap = new Bitmap(1, 1))  
  78.             {  
  79.                 using (Graphics g = Graphics.FromImage(bitmap))  
  80.                 {  
  81.                     g.CopyFromScreen(  
  82.                         Cursor.Position.X, Cursor.Position.Y, 0, 0,   
  83.                         bitmap.Size, CopyPixelOperation.SourceCopy);  
  84.                     g.Save();  
  85.                 }  
  86.                 // 设置窗口文本   
  87.                 // 显示鼠标当前位置和鼠标位置的像素颜色   
  88.                 Color color = bitmap.GetPixel(0, 0);  
  89.                 f.Text = string.Format("Pos: {0}, Color: {1}",  
  90.                     e.Location, color);  
  91.             }  
  92.             using (Graphics g = Graphics.FromHwnd(box.Handle))  
  93.             {  
  94.                 using (Pen p = new Pen(Color.IndianRed, 3))  
  95.                 {  
  96.                     g.DrawLine( // 画十字图形的 X 轴   
  97.                         p, new Point(e.X - 10, e.Y), new Point(e.X + 10, e.Y));  
  98.                     g.DrawLine( // 画十字图形的 Y 轴   
  99.                         p, new Point(e.X, e.Y - 10), new Point(e.X, e.Y + 10));  
  100.                 }  
  101.             }  
  102.         }  
  103.     };  
  104.     // PictureBox_MouseUp 用于清除鼠标位置信息和屏幕十字标记   
  105.     box.MouseUp += (sender, e) =>  
  106.     {  
  107.         if (e.Button == MouseButtons.Left)  
  108.         {  
  109.             pt = Point.Empty;  
  110.             box.Refresh();  
  111.         }  
  112.     };  
  113.     // PictureBox_MouseMove 用于绘制选择框   
  114.     box.MouseMove += (sender, e) =>  
  115.     {  
  116.         // 设置窗口文本 显示鼠标当前位置   
  117.         f.Text = string.Format("Pos: {0}", e.Location);  
  118.         if (e.Button == MouseButtons.Left && pt != Point.Empty)  
  119.         {  
  120.             using (Graphics g = Graphics.FromHwnd(box.Handle))  
  121.             {  
  122.                 using (Pen p = new Pen(Color.IndianRed, 1))  
  123.                 {  
  124.                     // pt1 用于判断鼠标是否离开了 PictureBox   
  125.                     // 如果离开则重设 pt1   
  126.                     // 如此可保证被绘制的矩形一定出现在 PictureBox 中   
  127.                     Point pt1 = e.Location;  
  128.                     if (pt1.X < 0) pt1.X = 0;  
  129.                     if (pt1.Y < 0) pt1.Y = 0;  
  130.                     if (pt1.X > box.Width - 1) pt1.X = box.Width - 1;  
  131.                     if (pt1.Y > box.Height - 1) pt1.Y = box.Height - 1;  
  132.                     box.Refresh(); // 不刷新的话 会出现N个矩形   
  133.                     p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;  
  134.                     g.DrawPolygon(p, new Point[] {  
  135.                         pt,  
  136.                         new Point (pt1.X, pt.Y),  
  137.                         pt1,  
  138.                         new Point(pt.X, pt1.Y) });  
  139.                 }  
  140.             }  
  141.         }  
  142.     };  
  143.     // 记录当前翻转样式   
  144.     int rotate = 0;  
  145.     // PictureBox_MouseDoubleClick 用于翻转图片   
  146.     box.MouseDoubleClick += (sender, e) =>  
  147.     {  
  148.         if (e.Button == MouseButtons.Left)  
  149.         {  
  150.             // 由于是对 PictureBox 进行的操作   
  151.             // 图片只是进行了翻转,但大小未变   
  152.             // 所以需要通过图片的某个边判断   
  153.             // 图片是否因翻转而导致 Width / Height 互换   
  154.             int imgWidth = box.Image.Width;  
  155.             box.Image.RotateFlip((RotateFlipType)rotate);  
  156.             if (++rotate > 7) rotate = 0;  
  157.             if (imgWidth == box.Image.Width) return;  
  158.             box.Size = new Size(box.Height, box.Width);  
  159.         }  
  160.     };  
  161.     f.Controls.Add(box);  
  162.     f.Show();  
  163. }  

转载:http://blog.csdn.net/qqamoon/article/details/6079697

你可能感兴趣的:(object,C#,null,float,WinForm,图形)