日期:2014-05-18  浏览次数:20957 次

C#图片放大.
C# code

private bool isMoving = false;
        private int x, y;


        private void mouseDown(object sender, MouseEventArgs e)
        {
            this.pictureBox1.Focus();
            isMoving = true;
            x = e.X;
            y = e.Y;
        }

        private void mouseMove(object sender, MouseEventArgs e)
        {
            if (isMoving)
            {
                this.pictureBox1.Left += (e.X - x);
                this.pictureBox1.Top += (e.Y - y);

            }
        }

        private void mouseUp(object sender, MouseEventArgs e)
        {
            isMoving = false;
        }

        private void PictureBox1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {

            if (e.Delta > 0)
            {
                pictureBox1.Width = Convert.ToInt32(pictureBox1.Width * 0.8);
                pictureBox1.Height = Convert.ToInt32(pictureBox1.Height * 0.8);

            }

            else
            {
                pictureBox1.Width = Convert.ToInt32(pictureBox1.Width / 0.8);
                pictureBox1.Height = Convert.ToInt32(pictureBox1.Height / 0.8);

            }

        }





小弟现在这个放大代码,充其量只是能放大图片所在的PICTRUEBOX来达到放大图片的结果,但是放大的效果不好,图片是在PICTRUEBOX的左上角开始放大,那么就会造成图片会"走动"可否有办法放大的效果是"居中放大".请高手帮帮忙

------解决方案--------------------
写在代码里最直接,根据原始图片的 Width 和 pictureBox1.Width 进行判断

C# code

int imgWidth = this.pictureBox1.Image.Width;
int width = this.pictureBox1.Width;

if (width / imgWidth >= 8) return; // 放大
if((float)width / (float)imgWidth <= 0.1) return; // 缩小