如何放大和缩小位图

时间:2020-10-13 07:31:02

标签: c# winforms graphics zoom

我是编程新手,请提供您的建议。 我的任务是编写一个程序,该程序将根据轨道的长度计算所需的项目数。我们需要自己绘制此轨道,但我不想使该应用程序全屏显示,因此我需要缩放功能,而对于我来说,我一生都无法实现。 请帮忙!

这是代码。

namespace RFIDGovno

{

    public partial class Form1 : Form
    {
        List<Line> lines = new List<Line>();
        List<Dot> dots = new List<Dot>();
        bool isVert;
        Point sPlace;
        Point ePlace;
        bool downed=false;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            Point temp = e.Location;
            if (downed)
            {
                if (!(Math.Abs(e.X - sPlace.X) > Math.Abs(e.Y - sPlace.Y)))
                {
                    temp.X = sPlace.X;
                }
                else
                {
                    temp.Y = sPlace.Y;
                }

                downed = false;
                ePlace = temp;
                dots.Add(new Dot(ePlace));
                lines.Add(new Line(sPlace,ePlace,isVert, isVert ? Math.Abs(sPlace.Y-ePlace.Y) : Math.Abs(sPlace.X - ePlace.X)));

            }else
            {
                if (!dots.Any<Dot>())
                {
                    downed = true;
                    sPlace = e.Location;
                    dots.Add(new Dot(sPlace));
                }
                else if(dots.Where(t => t.fuse).Any<Dot>())
                {
                    downed = true;
                    sPlace = dots.Where(t => t.fuse).First().place;
                }
            }
            drawing(null);
        }

        private void Zooming()
        {
            
        }

        private void drawing(Point? mouse)
        {
            Graphics g = pictureBox1.CreateGraphics();
            Bitmap im = new Bitmap(pictureBox1.Width-1, pictureBox1.Height-1);
            Graphics q = Graphics.FromImage(im);
            q.Clear(Color.White);

            lines.ForEach(line =>
            {
                q.DrawLine(new Pen(Brushes.Black, 2), line.pStart, line.pEnd);
                q.DrawString(line.length.ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, line.isVert ? line.pStart.X : line.pStart.X + ((line.pEnd.X - line.pStart.X) / 2) - 5, line.isVert ? line.pStart.Y + ((line.pEnd.Y - line.pStart.Y) / 2) - 5 : line.pStart.Y);

            });

            if (downed)
            {
                q.DrawLine(new Pen(Brushes.Black, 2), sPlace, mouse ?? sPlace);
                if ((isVert)&&(mouse.HasValue))
                {
                    q.DrawString(Math.Abs(sPlace.Y-mouse.Value.Y).ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, sPlace);
                }
                else if ((isVert==false) && (mouse.HasValue))
                {
                    q.DrawString(Math.Abs(sPlace.X - mouse.Value.X).ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, sPlace);
                }
            }

            dots.ForEach(dot =>
            {   
                q.DrawEllipse(new Pen(Brushes.Black, 2),dot.place.X-1, dot.place.Y - 1, 2, 2);
                if (dot.fuse)
                {
                    q.DrawEllipse(new Pen(Brushes.Red, 2), dot.place.X - 2, dot.place.Y - 2, 4, 4);
                }
            });
            g.DrawImage(im, 1, 1);
            q.Dispose();
            im.Dispose();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            Point temp = e.Location;
            if (downed)
            {
                if (!(Math.Abs(e.X - sPlace.X) > Math.Abs(e.Y - sPlace.Y)))
                {
                    temp.X = sPlace.X;
                    isVert = true;
                }
                else
                {
                    temp.Y = sPlace.Y;
                    isVert = false;
                }
            }
            else
            {
                dots.ForEach(t => t.fuse = false);
                List<Dot> tDots = dots.Where(dt => Math.Abs(e.Location.X - dt.place.X) < 20 && Math.Abs(e.Location.Y - dt.place.Y) < 20).ToList();
                if (tDots.Any<Dot>())
                {
                    double min = 0;
                    min = tDots.Min(t => distance(t.place, e.Location));
                    tDots.Where(t => distance(t.place, e.Location) == min).First().fuse = true;
                }
            }
            drawing(temp);
        }
        private double distance(Point p1, Point p2)
        {
            double dist=0;
            dist = (Math.Sqrt(Math.Abs(p1.X-p2.X)+Math.Abs(p1.Y-p2.Y)));
            return dist;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
        
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int between = 0;
            string curr = comboBox1.SelectedItem.ToString();
            if (curr == "Short")
            {
                between = 1250;
            }
            else if (curr == "Medium")
            {
                between = 1875;
            }
            else
            {
                between = 3750;
            }
            double wut = (double.Parse(textBox1.Text) / between * 300 + double.Parse(textBox3.Text) + double.Parse(textBox2.Text) * 2);
            string answer = Convert.ToString(Math.Ceiling(wut));
            label4.Text = answer;
        }
    }

    public class Line
    {
        public Point pEnd;
        public Point pStart;
        public bool isVert;
        public int length;
        public Line(Point pStart, Point pEnd, bool isVert,int length)
        {
            this.pStart = pStart;
            this.pEnd = pEnd;
            this.isVert = isVert;
            this.length = length;
        }
    }

    public class Dot
    {
        public bool fuse;
        public Point place;
        public Dot(Point place)
        {
            this.place = place;
        }
    }
}

0 个答案:

没有答案