当前位置:编程学习 > C#/ASP.NET >>

求助!C#关于图片缩放

图片缩放后如果我希望图片上方的控件也能跟随着一起缩放,我应该怎么做呢? C# 图片 控件 --------------------编程问答-------------------- 准确说是,图片本身上方我放了一个lable,lable的坐标是100,100.当我图片缩小的时候,我希望lable的坐标能跟随我的图片一起变更位置,这样该怎么做到? --------------------编程问答-------------------- 最好截个图来,你说的图片是窗体的背景图片? --------------------编程问答-------------------- 如果是winform的picturebox控件,设置成控件跟随图片大小的模式就行。
不知道你具体是什么问题,描述得不够清楚 --------------------编程问答--------------------
引用 3 楼 heycoder 的回复:
如果是winform的picturebox控件,设置成控件跟随图片大小的模式就行。
不知道你具体是什么问题,描述得不够清楚


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;
        }
    }
}

源代码是这样的,我加了一些东西进去,我希望能得到就是我加载一张图片后,图片上如果有某个控件,希望控件能随着图片变小放大而变小放大。 --------------------编程问答--------------------
引用 2 楼 hwenycocodq520 的回复:
最好截个图来,你说的图片是窗体的背景图片?

您稍微看下4楼好吗?我刚刚把代码贴上去了 --------------------编程问答--------------------
引用 5 楼 dearwangzong 的回复:
Quote: 引用 2 楼 hwenycocodq520 的回复:

最好截个图来,你说的图片是窗体的背景图片?

您稍微看下4楼好吗?我刚刚把代码贴上去了

好啊,在看 --------------------编程问答--------------------
引用 6 楼 hwenycocodq520 的回复:
Quote: 引用 5 楼 dearwangzong 的回复:

Quote: 引用 2 楼 hwenycocodq520 的回复:

最好截个图来,你说的图片是窗体的背景图片?

您稍微看下4楼好吗?我刚刚把代码贴上去了

好啊,在看

不知您是否能帮助我解决这个问题呢 --------------------编程问答-------------------- 图片上的控件位置相对不变? --------------------编程问答-------------------- 使用百分比不就行了 --------------------编程问答--------------------
引用 8 楼 hwenycocodq520 的回复:
图片上的控件位置相对不变?

对的,假如图片上有一个圆点,这个圆点上有个控件,那我希望下次这个控件能随着图片的缩放而跟着缩放,并且始终在那个圆点上 --------------------编程问答--------------------
引用 9 楼 tiwenid0 的回复:
使用百分比不就行了

百分比? --------------------编程问答--------------------
引用 8 楼 hwenycocodq520 的回复:
图片上的控件位置相对不变?

您要是解决了麻烦您能回复下吗?不好意思,这个东西可能我这边有点着急着用,谢谢您了 --------------------编程问答-------------------- 你的意思是图片缩放的时候以他的中心缩放而不是左上角吧。这样你计算和设置每次缩放完之后图片控件应该所在的位置就行了啊。 --------------------编程问答--------------------
引用 13 楼 yuwenge 的回复:
你的意思是图片缩放的时候以他的中心缩放而不是左上角吧。这样你计算和设置每次缩放完之后图片控件应该所在的位置就行了啊。

对,就是这个意思,只是我不太清楚该怎么写出来这个:计算和设置每次缩放完之后图片控件应该所在的位置。
--------------------编程问答--------------------
引用 14 楼 dearwangzong 的回复:
Quote: 引用 13 楼 yuwenge 的回复:

你的意思是图片缩放的时候以他的中心缩放而不是左上角吧。这样你计算和设置每次缩放完之后图片控件应该所在的位置就行了啊。

对,就是这个意思,只是我不太清楚该怎么写出来这个:计算和设置每次缩放完之后图片控件应该所在的位置。

可以去问任何一个初中生。 --------------------编程问答-------------------- dock = fill
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,