c# GDI+ 绘图实时缩放

c#GDI+绘图实时缩放

具体措施:

  1. 先获取控件的原大小,然后对需要绘制的控件加入滚轮事件,每次滚动改变控件的大小:

    this.pic.MouseWheel += new MouseEventHandler(pictureBox1_Paint1);
    
  2. 滚轮事件中加入线程,线程的存在是为了实时绘制不卡顿,

    private void pictureBox1_Paint1(object sender, MouseEventArgs e) {
                
                this.pic.Width += e.Delta/10;
                this.pic.Height += e.Delta/10;
                if (this.pic.Height < 50) {
                    this.pic.Height = 50;
                    this.pic.Width = 50;
                }
                run();
    }
    private void run() {
                Thread t = new Thread(() => {
                    Graphics g = pic.CreateGraphics();
                    g.Clear(Color.Gray);
                    g.ScaleTransform(this.pic.Width / width1, this.pic.Height / height1);
                    Console.WriteLine($"{this.pic.Width / width1} + {this.pic.Height / height1}");
                    g.DrawRectangle(new Pen(Color.Black), new Rectangle(10, 10, 10, 10));
                    g.Flush();
                });
                t.Start();
    }
    

策略一:

Matrix m = new Matrix();
m.Scale(this.Width / width1, this.Height / height1);
GraphicsPath a = new GraphicsPath();
a.AddRectangle(new RectangleF(100, 100, 50, 50));
a.Transform(m);
g.DrawPath(new Pen(Color.Red), a);

策略二:

Graphics g = pic.CreateGraphics();
g.Clear(Color.Gray);
g.ScaleTransform(this.pic.Width / width1, this.pic.Height / height1);
Console.WriteLine($"{this.pic.Width / width1} + {this.pic.Height / height1}");
g.DrawRectangle(new Pen(Color.Black), new Rectangle(10, 10, 10, 10));
g.Flush();

*注:*因为滚轮导致控件的改变,是可以在控件的重绘事件中捕获的,_Paint(object sender, PaintEventArgs e)。理论上下面是可行的。

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //Thread t = new Thread(() => {
            //    Graphics g = pic.CreateGraphics();
            //    g.Clear(Color.Gray);
            //    g.ScaleTransform(this.pic.Width / width1, this.pic.Height / height1);
            //    Console.WriteLine($"{this.pic.Width / width1} + {this.pic.Height / height1}");
            //    g.DrawRectangle(new Pen(Color.Black), new Rectangle(10, 10, 50, 50));
            //    g.Flush();
            //});
            //t.Start();
        }

但是md缩放的时候, 不发生重绘事件! 所以还是直接在滚轮事件中加线程比较合适。

上一篇:【图像识别】基于PCA+SVM 表情识别评分系统matlab代码


下一篇:【qq机器人】发送图片的插件