using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Thread thDraw;
delegate void myDrawRectangel();
myDrawRectangel mydraw;
private Point ptBegin = new Point();
private bool blIsDrawRectangle = true; public Form1()
{
InitializeComponent();
} private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (blIsDrawRectangle)
{
e.Graphics.DrawRectangle(new Pen(Brushes.Black, 1), ptBegin.X, ptBegin.Y, 50, 50);
}
} private void Form1_Load(object sender, EventArgs e)
{
//图片背景初始化
this.pictureBox1.Image = System.Drawing.Image.FromFile(@"D:\Image\MianPic.bmp"); mydraw = new myDrawRectangel(ShowDrawRectangle);
thDraw = new Thread(Run);
thDraw.Start(); }
private void Run()
{
while (true)
{
if (pictureBox1.Image != null)
{
this.BeginInvoke(mydraw);
}
Thread.Sleep(50);
}
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (thDraw != null)
{
thDraw.Abort();
} } private void ShowDrawRectangle()
{
Rectangle rec = new Rectangle(ptBegin.X * pictureBox1.Image.Size.Width / 460, ptBegin.Y * pictureBox1.Image.Size.Height / 350,
50 * pictureBox1.Image.Size.Width / 460, 50 * pictureBox1.Image.Size.Height / 350);
Graphics g = pictureBox2.CreateGraphics();
g.DrawImage(pictureBox1.Image, pictureBox2.ClientRectangle, rec, GraphicsUnit.Pixel);
g.Flush();
} private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
blIsDrawRectangle = false;
pictureBox1.Refresh(); } private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
blIsDrawRectangle = true; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.X - 25 <= 0)
{
ptBegin.X = 0;
}
else if (pictureBox1.Size.Width - e.X <= 25)
{
ptBegin.X = pictureBox1.Size.Width - 50;
}
else
{
ptBegin.X = e.X - 25;
} if (e.Y - 25 <= 0)
{
ptBegin.Y = 0;
}
else if (pictureBox1.Size.Height - e.Y <= 25)
{
ptBegin.Y = pictureBox1.Size.Height - 50;
}
else
{
ptBegin.Y = e.Y - 25;
}
pictureBox1.Refresh(); } }
}以上是我下载的一个可以实现像taobao一样实现局部图片放大的效果,我运行了该代码,确实可以
这里pictureBox1装载一副原图,pictureBox2显示局部放大的图片我想请教一下这里的高手,里面的50,25, 460,350等数字的意义。我不懂该程序的原理,所以我把代码复制到我的程序里时,虽然局部确实放大了,但是放大的位置有比较大的偏差,还望高人指点。
我早试过了,460 350应该确实是picturebox1的宽度和高度
25 和50就搞不明白了,改了以后放大的区域跟我在picturebox1上点击的位置有比较大的偏差
所以我就不明白了啊。要是清楚了还来100分求教?
我靠,这玩意到底有没有人懂啊。。
JS 或者JQUERY 放大镜效果!!
web里面
WebForm必须的
Silverlight不必的
desktop里面
WinForm WPF也不必的
老大,你说到点子上了,这个画框的你一说我懂了
另外,放大倍数是哪个控制的?应该是这个函数吧
private void ShowDrawRectangle()
{
Rectangle rec = new Rectangle(ptBegin.X * pictureBox1.Image.Size.Width / 460, ptBegin.Y * pictureBox1.Image.Size.Height / 350,
50 * pictureBox1.Image.Size.Width / 460, 50 * pictureBox1.Image.Size.Height / 350);
Graphics g = pictureBox2.CreateGraphics();
g.DrawImage(pictureBox1.Image, pictureBox2.ClientRectangle, rec, GraphicsUnit.Pixel);
g.Flush();
}
但是我不得要领啊,代码第一行rec对象右边,我放大效果总是跟原程序显示效果偏差好多。就是说我鼠标点击的地方要放大,结果离鼠标比较远的地方也放大了,且中心位置根本不在我鼠标点击的地方。
请问这是怎么回事
帖子是 “像淘宝”
图片的长 宽程序里面设置固定了。