我對編程有點新,但我渴望瞭解更多信息,並且想知道是否有人可以幫我解決問題。C#應用程序放大/縮小PictureBox中的圖像
(主要目標)
我想打一個簡單的程序,它由一個C#的Windows窗體顯示預設的圖像(的6000x6000像素尺寸應用,SizeMode設置爲放大,使整個圖像可以在窗體中一次顯示)放在一個PictureBox中,它將實際佔用整個窗體,除了在窗體底部留出空間來顯示一個TrackBar,這將允許您放大和縮小圖像;以及PictureBox底部的水平滾動條和PictureBox右側的垂直滾動條,以便在縮放地圖時圍繞地圖滾動,並且我希望能夠通過點擊來控制這些滾動條並拖動相應的方向上的PictureBox(首選但不知道如果可能)或通過使用鼠標滾輪(可能更容易,但再次不確定)。
(參考)
[這裏如I中所述,在使用SizeMode縮放一個圖片一個6000x6000放置支架演示紋理,作爲一個例子我的形式精確地完成 - 這具有被處理,問題的下一部分是低於更新:]
(補遺)
我遇到的唯一問題是代碼,因爲我在該部門非常新手。我一直在努力學習Visual Studio的工作流程,但我真的可以使用一些幫助。
非常感謝您爲我提供的任何幫助。
UPDATE:
關於這個問題進行研究,並花時間做一些思考以後,我想出了下面列出的代碼;但是我的問題是,當我將圖像平移得太遠時,圖像被拉得太過分,從而在圖像被平移/拉到一個角落時暴露在其後面的面板。另外,當我變焦太遠時,圖像被允許變成WAY小於Picturebox。
Panning issue, the grey parts of the panel are the problem
Zoom issue, the grey parts of the panel are the problem
所以,我的最後一個問題:我怎麼會去修改下面的代碼爲「鎖定」,我平移和縮放圖像被允許平移或縮放外它的框架和暴露後面的面板?
public partial class ImageZoomMainForm : Form
{
Image img;
Point mouseDown;
int startx = 0;
int starty = 0;
int imgx = 0;
int imgy = 0;
bool mousepressed = false;
float zoom = 1;
public ImageZoomMainForm()
{
InitializeComponent();
string imagefilename = @"..\..\ViewPort_MAIN.tif";
img = Image.FromFile(imagefilename);
Graphics g = this.CreateGraphics();
zoom = ((float)pictureBox.Width/(float)img.Width) * (img.HorizontalResolution/g.DpiX);
pictureBox.Paint += new PaintEventHandler(imageBox_Paint);
}
private void pictureBox_MouseMove(object sender, EventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;
if (mouse.Button == MouseButtons.Left)
{
Point mousePosNow = mouse.Location;
int deltaX = mousePosNow.X - mouseDown.X;
int deltaY = mousePosNow.Y - mouseDown.Y;
imgx = (int)(startx + (deltaX/zoom));
imgy = (int)(starty + (deltaY/zoom));
pictureBox.Refresh();
}
}
private void imageBox_MouseDown(object sender, EventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;
if (mouse.Button == MouseButtons.Left)
{
if (!mousepressed)
{
mousepressed = true;
mouseDown = mouse.Location;
startx = imgx;
starty = imgy;
}
}
}
private void imageBox_MouseUp(object sender, EventArgs e)
{
mousepressed = false;
}
protected override void OnMouseWheel(MouseEventArgs e)
{
float oldzoom = zoom;
if (e.Delta > 0)
{
zoom += 0.1F;
}
else if (e.Delta < 0)
{
zoom = Math.Max(zoom - 0.1F, 0.01F);
}
MouseEventArgs mouse = e as MouseEventArgs;
Point mousePosNow = mouse.Location;
int x = mousePosNow.X - pictureBox.Location.X;
int y = mousePosNow.Y - pictureBox.Location.Y;
int oldimagex = (int)(x/oldzoom);
int oldimagey = (int)(y/oldzoom);
int newimagex = (int)(x/zoom);
int newimagey = (int)(y/zoom);
imgx = newimagex - oldimagex + imgx;
imgy = newimagey - oldimagey + imgy;
pictureBox.Refresh();
}
private void imageBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.ScaleTransform(zoom, zoom);
e.Graphics.DrawImage(img, imgx, imgy);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch (keyData)
{
case Keys.Right:
imgx -= (int)(pictureBox.Width * 0.1F/zoom);
pictureBox.Refresh();
break;
case Keys.Left:
imgx += (int)(pictureBox.Width * 0.1F/zoom);
pictureBox.Refresh();
break;
case Keys.Down:
imgy -= (int)(pictureBox.Height * 0.1F/zoom);
pictureBox.Refresh();
break;
case Keys.Up:
imgy += (int)(pictureBox.Height * 0.1F/zoom);
pictureBox.Refresh();
break;
case Keys.PageDown:
imgy -= (int)(pictureBox.Height * 0.90F/zoom);
pictureBox.Refresh();
break;
case Keys.PageUp:
imgy += (int)(pictureBox.Height * 0.90F/zoom);
pictureBox.Refresh();
break;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void ImageZoomMainForm_Load(object sender, EventArgs e)
{
}
}
}
總是把你製作的圖形對象放在一個'using(...){}'塊中 – Nyerguds