Winform图片局部放大效果


     转载地址: http://blog.csdn.net/catshitone/article/details/41513915

【C#】winform 图片局部放大类似淘宝                      

  •  整体效果:

有两个picturebox控件,name默认没有修改。其中picturebox1的img赋予初始值,picturebox2的visible=false;其余不变,程序比较简单,看以参考下。



  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading;  
  9. using System.Threading.Tasks;  
  10. using System.Windows.Forms;  
  11.   
  12. namespace 局部放大  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         Thread thDraw;  
  17.         delegate void myDrawRectangel();  
  18.         myDrawRectangel mydraw;  
  19.         private Point ptBegin = new Point();  
  20.         private bool blIsDrawRectangle = false;  
  21.   
  22.         public Form1()  
  23.         {  
  24.             InitializeComponent();  
  25.         }  
  26.   
  27.         private void pictureBox1_Paint(object sender, PaintEventArgs e)  
  28.         {  
  29.             //画矩形 初始值为false否则 一开始就会显示矩形框  
  30.             if (blIsDrawRectangle)  
  31.             {  
  32.                 e.Graphics.DrawRectangle(new Pen(Brushes.Black, 1), ptBegin.X, ptBegin.Y, 50, 50);  
  33.             }  
  34.         }  
  35.   
  36.         private void Form1_Load(object sender, EventArgs e)  
  37.         {  
  38.             mydraw = new myDrawRectangel(ShowDrawRectangle);  
  39.             thDraw = new Thread(Run);  
  40.             thDraw.Start();  
  41.   
  42.         }  
  43.         private void Run()  
  44.         {  
  45.             while (true)  
  46.             {  
  47.                 if (pictureBox1.Image != null)  
  48.                 {  
  49.                     this.BeginInvoke(mydraw);  
  50.                 }  
  51.                 Thread.Sleep(50);  
  52.             }  
  53.         }  
  54.   
  55.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)  
  56.         {  
  57.             if (thDraw != null)  
  58.             {  
  59.                 thDraw.Abort();  
  60.             }  
  61.              
  62.         }  
  63.   
  64.         private void ShowDrawRectangle()  
  65.         {  
  66.             Rectangle rec = new Rectangle(ptBegin.X * pictureBox1.Image.Size.Width / 460, ptBegin.Y * pictureBox1.Image.Size.Height / 350,  
  67.                                            50 * pictureBox1.Image.Size.Width / 460, 50 * pictureBox1.Image.Size.Height / 350);  
  68.             Graphics g = pictureBox2.CreateGraphics();  
  69.             g.DrawImage(pictureBox1.Image, pictureBox2.ClientRectangle, rec, GraphicsUnit.Pixel);  
  70.             g.Flush();  
  71.         }  
  72.   
  73.         private void pictureBox1_MouseLeave(object sender, EventArgs e)  
  74.         {  
  75.             pictureBox2.Visible = false;  
  76.             blIsDrawRectangle = false;  
  77.             pictureBox1.Refresh();  
  78.   
  79.         }  
  80.   
  81.         private void pictureBox1_MouseEnter(object sender, EventArgs e)  
  82.         {  
  83.             pictureBox2.Visible = true;  
  84.             blIsDrawRectangle = true;  
  85.   
  86.         }  
  87.   
  88.         private void pictureBox1_MouseMove(object sender, MouseEventArgs e)  
  89.         {  
  90.             if (e.X - 25 <= 0)  
  91.             {  
  92.                 ptBegin.X = 0;  
  93.             }  
  94.             else if (pictureBox1.Size.Width - e.X <= 25)  
  95.             {  
  96.                 ptBegin.X = pictureBox1.Size.Width - 50;  
  97.             }  
  98.             else  
  99.             {  
  100.                 ptBegin.X = e.X - 25;  
  101.             }  
  102.   
  103.             if (e.Y - 25 <= 0)  
  104.             {  
  105.                 ptBegin.Y = 0;  
  106.             }  
  107.             else if (pictureBox1.Size.Height - e.Y <= 25)  
  108.             {  
  109.                 ptBegin.Y = pictureBox1.Size.Height - 50;  
  110.             }  
  111.             else  
  112.             {  
  113.                 ptBegin.Y = e.Y - 25;  
  114.             }  
  115.             pictureBox1.Refresh();  
  116.   
  117.         }  
  118.     }  
  119. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值