滚动拉伸的图像

时间:2017-05-29 13:39:35

标签: c# winforms picturebox

我在c#windows窗体应用程序中有一个图片框,我已经制作了一些代码,以便我可以改变图片框的高度和宽度,实际上是放大。但是,这只关注右上角区域我希望用户能够使用滚动条将地图滚动到他们需要的区域。

每当我放大,然后使用滚动条,我的图像变为默认大小,然后只滚动该大小。

如何移动整个图像以便用户可以平移图片框图?

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mapping_Application
{
    public partial class Form1 : Form
    {
        //Declare zoom variable counter
        int zoom;
        //Main Void
        public Form1()
        {
            InitializeComponent();
        }

        //Zoom In
        private void button1_Click(object sender, EventArgs e)
        {
            if(zoom != 3)
            {
                pictureBox1.Height = pictureBox1.Height * 2;
                pictureBox1.Width = pictureBox1.Width * 2;
                zoom = zoom + 1;
            }            
        }

        //Zoom out
        private void button2_Click(object sender, EventArgs e)
        {
            if (zoom != 0)
            {
                pictureBox1.Height = pictureBox1.Height / 2;
                pictureBox1.Width = pictureBox1.Width / 2;
                zoom = zoom -= 1;
            }
        }

        //The horizontal scrollbar
        private void horizontal_Scroll(object sender, ScrollEventArgs e)
        {
            //Create a graphics object and draw a portion of the image in the PictureBox.
            Graphics g = pictureBox1.CreateGraphics();

            int x;
            int y;

            if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
            {
                x = e.NewValue * 19;
                y = vertical.Value * 19;
            }
            else //e.ScrollOrientation == ScrollOrientation.VerticalScroll
            {
                y = e.NewValue * 19;
                x = horizontal.Value * 19;
            }

            g.Clear(Color.White);

            g.DrawImage(pictureBox1.Image,
              new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Width),  //where to draw the image
              new Rectangle(x, y, pictureBox1.Width, pictureBox1.Width),  //the portion of the image to draw
              GraphicsUnit.Pixel);

            pictureBox1.Update();
        }

        //The vertical scrollbar
        private void vertical_Scroll(object sender, ScrollEventArgs e)
        {
            //Create a graphics object and draw a portion of the image in the PictureBox.
            Graphics g = pictureBox1.CreateGraphics();

            int x;
            int y;

            if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
            {
                x = e.NewValue * 19;
                y = vertical.Value * 19;
            }
            else //e.ScrollOrientation == ScrollOrientation.VerticalScroll
            {
                y = e.NewValue * 19;
                x = horizontal.Value * 19;
            }

            g.Clear(Color.White);

            g.DrawImage(pictureBox1.Image,
              new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Width),  //where to draw the image
              new Rectangle(x, y, pictureBox1.Width, pictureBox1.Width),  //the 
portion of the image to draw
              GraphicsUnit.Pixel);

            pictureBox1.Update();
        }
    }
}

目前滚动似乎有效,但仅限放大。没有缩放负面功能,我使用var命名缩放来存储程序所处的级别缩放。无法缩放到-2x,因此if(缩放!= 0)

0 个答案:

没有答案