c# GDI+ 绘图实时缩放

2021/11/16 17:13:00

本文主要是介绍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缩放的时候, 不发生重绘事件! 所以还是直接在滚轮事件中加线程比较合适。



这篇关于c# GDI+ 绘图实时缩放的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程