2014-05-21 2 views
2

Google지도처럼 커서로 확대/축소 할 수있는 pictureBox를 만들려고합니다.PictureBox 확대/축소 및 마우스 휠 C#

일부 코드 :

int viewRectWidth; 
int viewRectHeight; 
public float zoomshift = 0.05f; 
int xForScroll; 
int yForScroll; 
float zoom = 1.0f; 
public float Zoom 
{ 
    get { return zoom; } 
    set 
    { 
    if (value < 0.001f) value = 0.001f; 
    zoom = value; 
    displayScrollbar(); 
    setScrollbarValues(); 
    Invalidate(); 
    } 
} 
Size canvasSize = new Size(60, 40); 
public Size CanvasSize 
{ 
    get { return canvasSize; } 
    set 
    { 
    canvasSize = value; 
    displayScrollbar(); 
    setScrollbarValues(); 
    Invalidate(); 
    } 
} 
Bitmap image; 
public Bitmap Image 
{ 
    get { return image; } 
    set 
    { 
    image = value; 
    displayScrollbar(); 
    setScrollbarValues(); 
    Invalidate(); 
    } 
} 
InterpolationMode interMode = InterpolationMode.HighQualityBilinear; 
public InterpolationMode InterpolationMode 
{ 
    get { return interMode; } 
    set { interMode = value; } 
} 
public ZoomablePictureBox() 
{ 
    InitializeComponent(); 
    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | 
       ControlStyles.ResizeRedraw | ControlStyles.UserPaint | 
       ControlStyles.DoubleBuffer, true); 
} 
protected override void OnPaint(PaintEventArgs e) 
{ 
    base.OnPaint(e); 
    if(image != null) 
    { 
    Rectangle srcRect, distRect; 
    Point pt = new Point((int)(hScrollBar1.Value/zoom), (int)(vScrollBar1.Value/zoom)); 
    if (canvasSize.Width * zoom < viewRectWidth) 
     srcRect = new Rectangle(0, 0, canvasSize.Width, canvasSize.Height); 
    else srcRect = new Rectangle(pt, new Size((int)(viewRectWidth/zoom), (int)(viewRectHeight/zoom))); 
    distRect = new Rectangle((int)(-srcRect.Width/2), -srcRect.Height/2, srcRect.Width, srcRect.Height); 

    Matrix mx = new Matrix(); 
    mx.Scale(zoom, zoom); 
    mx.Translate(viewRectWidth/2.0f, viewRectHeight/2.0f, MatrixOrder.Append); 

    Graphics g = e.Graphics; 
    g.InterpolationMode = interMode; 
    g.Transform = mx; 
    g.DrawImage(image, distRect, srcRect, GraphicsUnit.Pixel); 
    } 
} 

는 지금은 확대 및 마우스 포인트로 스크롤 마우스 휠 이벤트를해야하고, 난 그냥 스크롤바를 설정해야합니다 값 무슨 공식을 알아낼 수 없습니다.

private void onMouseWheel(object sender, MouseEventArgs e) 
{ 
    if (ModifierKeys == Keys.Control) 
    { 
    this.Zoom += e.Delta/120 * this.zoomshift; 
    vScrollBar1.Value = ?; 
    hScrollBar1.Value = ?; 
    } 
} 

어떤 도움을 주시면 감사하겠습니다.

감사합니다, 토마스

+0

이 방법은 큰 그림을 그릴하려는 경우, 당신은 성능 문제로 실행, [이] (http://stackoverflow.com/q/23733405/1997232) 아주 신선한 질문을 참조하십시오. – Sinatr

+0

http://bobpowell.net/zoompicbox.aspx –

답변

0

같은 상대 위치에있는 스크롤바를 유지하려면, 나는 다음과 같은 일을해야 생각 :

float oldvMax = vScrollBar1.Maximum; 
int oldvValue = vScrollBar1.Value; 
float oldhMax = hScrollBar1.Maximum; 
int oldhValue = hScrollBar1.Value; 
this.Zoom += e.Delta/120 * this.zoomshift; 
vScrollBar1.Value = (int)((oldvValue/oldvMax) * vScrollBar1.Maximum); 
hScrollBar1.Value = (int)((oldhValue/oldhMax) * hScrollBar1.Maximum);