Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Now I have a picturebox in a window form,and I placed many buttons on the picturebox.I have accomplished of changing the picture size by sliding the Mouse middle button,but I don't know how to make all buttons change together with picturebox,including button's location and size. Who can give me a helping hand.

What I have tried:

I tried to solve the problem. The presend codes in vs.net2013 are as follows:

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

namespace ImageScale
{
    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.pictureBox1.BorderStyle = BorderStyle.FixedSingle;
            this.pictureBox1.BackColor = Color.DarkGray;
            this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
          
        }

        Bitmap m_bmp;             
        Point m_ptCanvas;          
        Point m_ptCanvasBuf;        
        Point m_ptBmp;              
        float m_nScale = 1.0F;     

        Point m_ptMouseDown;        

        string m_strMousePt;        

        private void Form1_Load(object sender, EventArgs e) {
         
            m_bmp = Properties.Resources.QQ20170722225306;
           
            m_ptCanvas = new Point(pictureBox1.Width / 2, pictureBox1.Height / 2);
            m_ptBmp = new Point(-(m_bmp.Width / 2), -(m_bmp.Height / 2));
           
        }
       
       
        }
        
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e) {
            Graphics g = e.Graphics;
            g.TranslateTransform(m_ptCanvas.X, m_ptCanvas.Y);      
            g.ScaleTransform(m_nScale, m_nScale);                   
            g.DrawImage(m_bmp, m_ptBmp);                          
           
            g.ResetTransform();                                     

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Middle) {     
                m_ptMouseDown = e.Location;
                m_ptCanvasBuf = m_ptCanvas;
            }
            pictureBox1.Focus();
        }
     
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Middle) {     
              
                m_ptCanvas = (Point)((Size)m_ptCanvasBuf + ((Size)e.Location - (Size)m_ptMouseDown));
                pictureBox1.Invalidate();
            }
           
        }
       
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) {
            if (m_nScale <= 0.3 && e.Delta <= 0) return;       
            if (m_nScale >= 4.9 && e.Delta >= 0) return;       
          
            SizeF szSub = (Size)m_ptCanvas - (Size)e.Location;
            
            float tempX = szSub.Width / m_nScale;          
            float tempY = szSub.Height / m_nScale;         
          
            m_ptCanvas.X -= (int)(szSub.Width - tempX);     
            m_ptCanvas.Y -= (int)(szSub.Height - tempY);    
                               
            szSub.Width = tempX;
            szSub.Height = tempY;
            m_nScale += e.Delta > 0 ? 0.2F : -0.2F;
            m_ptCanvas.X += (int)(szSub.Width * m_nScale - szSub.Width);
            m_ptCanvas.Y += (int)(szSub.Height * m_nScale - szSub.Height);
            pictureBox1.Invalidate();
          
        }
Posted
Updated 12-Aug-17 19:40pm
Comments
Graeme_Grant 12-Aug-17 23:06pm    
Your buttons are not mentioned in the code above other than they are on the PictureBox. What is the parent control for the Buttons and how have you "Anchored" them?
BillWoodruff 13-Aug-17 3:02am    
Independent of the issue with the Buttons (please answer Graeme's question): have you considered just setting the PictureBox's image once, and setting the 'SizeMode to 'StretchImage ?

Why do you want to scale the Size of the Buttons: in WinForms, Fonts are not going to scale well to arbitrary sizes.

Are the Buttons "on" the PictureBox to indicate a relationship between a particular point or area in the Image and something else ? If so, then doesn't arbitrary scaling, or panning, of the image mean that Buttons may not be shown ?
fwloveme 13-Aug-17 11:37am    
I put a picturebox on a panel container, and some buttons on the picturebox.Now the buttons position are random, I want to accomplish some functions: button's position and size can change with change of image in the picturebox, and the pixturebox 'SizeMode' is 'AutoSize' and 'Dock' is 'None',the panel container 'AutoScroll' is true.
BillWoodruff 13-Aug-17 12:17pm    
So, then you wish the Buttons to be anchored to specific points in the Image ? Like an HTML client-side image-map ?

How do you decide where the Buttons go ?

When you scroll the Image in the Panel do Buttons go off-screen ?

See if these give you some ideas:

https://github.com/EWSoftware/ImageMaps

https://www.codeproject.com/Articles/2820/C-Windows-Forms-ImageMap-Control

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900