C# 滚动拉伸的图像
我的c#windows窗体应用程序中有一个picturebox,我还编写了一些代码,可以改变picturebox的高度和宽度,基本上可以放大。但是,这只关注右上角区域,我希望用户能够使用滚动条将地图滚动到他们需要的区域 每当我放大,然后使用滚动条时,我的图像将变为默认大小,然后仅以该大小滚动 如何移动整个图像,以便用户可以平移picturebox地图 这是我的密码:C# 滚动拉伸的图像,c#,winforms,picturebox,C#,Winforms,Picturebox,我的c#windows窗体应用程序中有一个picturebox,我还编写了一些代码,可以改变picturebox的高度和宽度,基本上可以放大。但是,这只关注右上角区域,我希望用户能够使用滚动条将地图滚动到他们需要的区域 每当我放大,然后使用滚动条时,我的图像将变为默认大小,然后仅以该大小滚动 如何移动整个图像,以便用户可以平移picturebox地图 这是我的密码: using System; using System.Collections.Generic; using System.Comp
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();
}
}
}
滚动条目前似乎可以工作,但仅用于放大。没有缩放负片功能,我使用一个名为zoom的变量来存储程序所处的缩放级别。无法缩放到-2x,因此如果(缩放!=0)将pbox设置为缩放;将其嵌套在面板中。将面板设置为自动滚动。要放大或缩小pbox,使其变大或变小,我将尝试一下。谢谢。@TaW这个有用。谢谢