日期:2014-05-18  浏览次数:20764 次

pictureBox1的图像上画矩形框
已经在pictureBox1画好一幅图,我想用一个矩形框把想要放大的部分框出来,就是标记出来,并能获得矩形四个角的坐标。
没有查到相关资料,望高手提示一下,给一点代码,谢谢

------解决方案--------------------
给pictureBox1添加pictureBox1_MouseUp,pictureBox1_MouseDown事件
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
label1.Text = e.X.ToString() + "," + e.Y.ToString(); 
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
label1.Text += " " + e.X.ToString() + "," + e.Y.ToString(); 
}

label1上就显示起点和终点坐标了。
------解决方案--------------------
最简单的其实知道两个点,起始点和终止点就够了。

mousedown: 记下起点当前坐标 p0(e.X,e.Y)

mouseup: 记下终点当前坐标 p1(e.X e.Y)
此事件中画框
Graphics g = 图片控件.CreateGraphics();
Pen pen = new Pen(Color.Red, 1.0f);//颜色和粗细
pen.DashStyle = DashStyle.Dash;//线条样式
g.DrawRectangle(pen, new Rectangle(p0.X, p0.Y, 宽, 高));
g.Dispose();

如果拖动的过程中也需要体现变化的矩形框请参考mouseup

提醒一点,起始点和终止点的选择需要仔细处理一下,应该考虑用户从右下到左上,右上到左下等等情形。
------解决方案--------------------
我想用一个矩形框把想要放大的部分框出来
这个矩形的长宽比例,不一定等于视框(viewport)的长宽比例。可以考虑直接用鼠标滚轮来放大缩小。

下面有个例子,可以直接编译和运行。有几个概念这里先讲一下。

1、对函数 y = f(x) 来说,将坐标原点移到(shiftX, shiftY),图形转变为y-shiftY = f(x-shiftX)
2、对函数 y = f(x) 来说,将坐标扩大scale倍,则图形转变为 y/scale = f( x/scale )

例子用了简单的Sinc函数,作为‘分型’的例子:
y = sin(x) / x;

C# code

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        Point center, oldCenter, dragStart;
        GraphicsPath path;
        int scale = 100;

        public Form1()
        {
            this.Size = new Size(600, 400);
            center = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);
            path = GeneratePath();
        }

        // 根据计算好的控制点绘制分形
        protected override void OnPaint(PaintEventArgs e)
        {
            using (Graphics g = e.Graphics)
            {
                g.DrawPath(Pens.Red, path);
            }
            this.Text = string.Format("center: {0,3},{1,3}  scale: {2}", center.X, center.Y, scale);
        }

        // 用滚轮控制放大缩小
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            scale = scale + e.Delta / 4;
            scale = Math.Min(10000, scale);
            scale = Math.Max(10, scale);

            path = GeneratePath();
            Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            dragStart = e.Location;
            oldCenter = center;
            this.Capture = true;
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.Capture = false;
        }

        // 用鼠标移动图形
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (this.Capture)
            {
                center = Point.Add(oldCenter, new Size(e.X - dragStart.X, e.Y - dragStart.Y));
                path = GeneratePath();
                Invalidate();
            }
        }

        // 创建图形的控制点
        GraphicsPath GeneratePath()
        {
            byte[] types = new byte[this.ClientSize.Width];
            for (int i = 0; i < types.Length; i++) types[i] = (byte)PathPointType.Line;

            PointF[] samples = new PointF[types.Length];
            for (int x = 0; x < samples.Length; x++)
            {
                // 只画屏幕中看得到的x点。30.0f用来减缓频率以美观图形
                samples[x].X = x;
                samples[x].Y = Sinc((x - center.X) / (scale / 30.0f)) * scale + center.Y;
            }

            return new GraphicsPath(samples, types);
        }

        static float Sinc(float theta)
        {