图片缩放后如果我希望图片上方的控件也能跟随着一起缩放,我应该怎么做呢?C#图片控件

解决方案 »

  1.   

    准确说是,图片本身上方我放了一个lable,lable的坐标是100,100.当我图片缩小的时候,我希望lable的坐标能跟随我的图片一起变更位置,这样该怎么做到?
      

  2.   

    如果是winform的picturebox控件,设置成控件跟随图片大小的模式就行。
    不知道你具体是什么问题,描述得不够清楚
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Xml;namespace tc
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                pictureBox1.Paint+=new PaintEventHandler(pictureBox1_Paint);
                this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
            }
            float zoom;
            Point oldPoint,newPoint;
            bool IsDown;
            bool IsLoadBmp;
            Bitmap bmp;
            private void Form1_Load(object sender, EventArgs e)
            {
                IsLoadBmp = IsDown = false;
                zoom = 1.0f;
                oldPoint = new Point(0,0);
                newPoint = new Point(0,0);
                btnHight = button1.Height;
                btnWith = button1.Width;
            }
            int With = 0;
            int Height = 0;
            int btnHight = 0;
            int btnWith=0;
            private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {                       Graphics g = e.Graphics;
                if (IsLoadBmp)
                {
                    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                    rect.X = newPoint.X;
                    rect.Y = newPoint.Y;
                    Rectangle rt = new Rectangle(0, 0, btnWith, btnHight);
                    rt.X = button1.Location.X;
                    rt.Y = button1.Location.Y;
                    //button1.Location = new Point(newPoint.X, newPoint.Y);
                    //button1.Location.Y = newPoint.Y;
                    
                    if (With==0&&Height==0)
                    {
                        rect.Width = (int)(rect.Width * zoom);
                        rect.Height = (int)(rect.Height * zoom);
                        //记录下当前的长宽
                        With = rect.Width;
                        Height = rect.Height;
                    }
                    else
                    {
                        if ((int)(rect.Width * zoom)>=1000||(int)(rect.Height * zoom)>=1000)
                        {
                            if ((int)(rect.Width * zoom)<With||(int)(rect.Height * zoom)<Height)
                            {
                                rect.Width = (int)(rect.Width * zoom);
                                 rect.Height = (int)(rect.Height * zoom);
                                 button1.Width = (int)(rt.Width * zoom);
                                 button1.Height = (int)(rt.Height * zoom);
                              //记录下当前的长宽
                                 With = rect.Width;
                                 Height = rect.Height;
                            }
                            else
                            {
                                rect.Width = With;
                                rect.Height = Height;
                            }
                           
                        }
                        else
                        {
                            rect.Width = (int)(rect.Width * zoom);
                            rect.Height = (int)(rect.Height * zoom);
                            button1.Width = (int)(rt.Width * zoom);
                            button1.Height = (int)(rt.Height * zoom);
                            //记录下当前的长宽
                            With = rect.Width;
                            Height = rect.Height;
                        }
                    }
                    
                    g.FillRectangle(Brushes.White, pictureBox1.ClientRectangle);
                    g.DrawImage(bmp, rect, new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                }
                
                
            }
            private void OpenImg_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofg = new OpenFileDialog();
                ofg.Filter = "图片文件(*.bmp)|*.bmp;*.jpg;*.jpeg;*.png;*.gif";
                ofg.Multiselect = false;
                if (ofg.ShowDialog() == DialogResult.OK)
                {
                    Form1_Load(null, null);
                    Image img = Image.FromFile(ofg.FileName);
                    bmp = new Bitmap(img);
                    if (pictureBox1.Width < bmp.Width || pictureBox1.Height < bmp.Height)
                        zoom = Math.Min((float)pictureBox1.Width / (float)bmp.Width, (float)pictureBox1.Height / (float)bmp.Height);
                    else
                        zoom = 1.0f;
                    img.Dispose();
                    IsLoadBmp = true;
                    pictureBox1.Refresh();
                }
            }
            private void Form1_MouseWheel(object sender, MouseEventArgs e)
            {
                zoom += (float)e.Delta/2000;
                if (zoom < 0)//原来是0
                    zoom = 0.0f;
                if (zoom > 30)//原来是100
                    zoom =10.0f;
                pictureBox1.Refresh();
            }        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                IsDown = true;
                oldPoint.X =e.Location.X-newPoint.X;
                oldPoint.Y = e.Location.Y - newPoint.Y;
                this.Cursor = Cursors.Hand;
            }        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                if (IsDown)
                {
                    Point temp = e.Location;
                    newPoint.X=temp.X - oldPoint.X;
                    newPoint.Y=temp.Y - oldPoint.Y;
                    pictureBox1.Refresh();
                }
            }        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                IsDown = false;
                this.Cursor = Cursors.Default;
            }
        }
    }源代码是这样的,我加了一些东西进去,我希望能得到就是我加载一张图片后,图片上如果有某个控件,希望控件能随着图片变小放大而变小放大。