美文网首页
点击图片任意位置,此位置移动至图框中心

点击图片任意位置,此位置移动至图框中心

作者: forsek | 来源:发表于2017-11-20 16:47 被阅读0次

参考CSND论坛
一,点击图片上任意位置,此位置移动至图框(picturebox)中心.
需要三个坐标:1,图片中心坐标(根据图片尺寸计算);2,图框中心坐标(常量,已确定);3点击坐标(图框坐标系)
方法:根据1与2的位置差,先将图片中心与图框中心重合,然后使用全局变量叠加存储每次的3与2的偏差值(叠加存储作用是让图片在上一次偏移的基础上,进行补偿),最后将最新偏差值赋予图片做位置补偿,然后存储最新偏差值。

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

namespace WFA3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Bitmap bit1;//上提示图片(图片1)
        public Bitmap bitBig;//放大后图片
        bool mark=false;
        //点击位置坐标
        Point MD;
        //存储点击位置坐标
        Point md;
        //picbox中心坐标
        Point PbCenter;
        //图片中心坐标
        Point PicCenter;
        Point PicCenterB;
        //图片偏移量缓存(存储上一次图片偏移量)
        int Sx;
        int Sy;

        //pb中心与pic中心位置差
        Size CenDist;
       
        //大图片时pb中心与pic中心位置差
        Size CenDistBig;
        
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string path = this.openFileDialog1.FileName;//获得图片路径
                bit1 = new Bitmap(path);
                this.pictureBox1.Image = bit1;
                //图片中心坐标,据pb中心距离114,200
                PicCenter = new Point((bit1.Width / 2), (bit1.Height / 2));
     
                //pb中心与pic中心位置差
                Size CenDis = (Size)PicCenter - (Size)PbCenter;
                label1.Text = Convert.ToString(CenDis.Width);
                label2.Text = Convert.ToString(CenDis.Height);
                CenDist = CenDis;


            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if(mark==false)
            {
 
                if (true)
                {

                }
                mark = true;
            }
            else
            {
            }
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            label9.Text = "PbCenter";
            label10.Text = "PicCenter";
            label11.Text = "CenDis";
            label12.Text = "Mouse Click Postion";
            //图框中心坐标
            PbCenter = new Point(pictureBox1.Width / 2, pictureBox1.Height / 2);
            label1.Text = Convert.ToString(PbCenter.X);
            label2.Text = Convert.ToString(PbCenter.Y);
        }

        //小图绘图
        private void button2_Click(object sender, EventArgs e)
        {
            int sx = MD.X - PbCenter.X;
            int sy = MD.Y - PbCenter.Y;
            label5.Text = Convert.ToString(sx);
            label6.Text = Convert.ToString(sy);
            Bitmap bit = new Bitmap(500, 500);
            Graphics g = Graphics.FromImage(bit);
            //g.DrawImage(bit1,114-Sx-sx, 200-Sy-sy, bit1.Width,bit1.Height);
            g.DrawImage(bit1, -CenDist.Width- Sx - sx,-CenDist.Height - Sy - sy, bit1.Width, bit1.Height);//图片偏移量计算,图中心与图框中心偏移量+之前点击偏移累加量+最新一次点击偏移量,三者之和做补偿(所以全部为负)
            DrawNumber(pictureBox1, g);
            this.pictureBox1.Image = bit;
            Sx += sx;
            Sy += sy;
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {

            label7.Text = Convert.ToString(e.X);
            label8.Text = Convert.ToString(e.Y);
            MD.X = e.X;
            MD.Y = e.Y;

        }
        //绘制图框中心标记,调用绘图方法
        public static void DrawNumber(PictureBox pictureBox, Graphics g)
        {
            //DrawString(g, "点击左边的字", 115, 300);
            //DrawString(g, "请点击图中最大的正方体", 10, 410);
            DrawPoint(g,249,249,3,3);
        }
        //绘图方法函数
        private static void DrawPoint(Graphics g, int x,int y,int width,int height)
        {
            //g.DrawString(s, new System.Drawing.Font("????", 10, FontStyle.Bold), new System.Drawing.SolidBrush(System.Drawing.Color.Red), x, y);
            g.DrawRectangle(new Pen(Color.Red),x,y,width,height);
        }
        //图片缩放函数,未完成
        private static Bitmap ZoomP(Bitmap a, float s, float v,int x,int y)
        {
            Bitmap bmp = new Bitmap((int)(a.Width * s), (int)(a.Height * v), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            g.ScaleTransform(s, v);
            g.DrawImage(a, x, y, a.Width, a.Height);
            a = bmp;
            return a;
        }


        //图片放大,未完成
        private void button3_Click(object sender, EventArgs e)
        {
            Bitmap bitB = new Bitmap(500,500);
            bitB = ZoomP(bit1, 2, 2,Sx,Sy);
            this.pictureBox1.Image = bitB;
            bitBig = bitB;

            //大图中心点位置
            //PicCenter = new Point((bitB.Width / 2), (bitB.Height / 2));
            //label3.Text = Convert.ToString(PicCenter.X);
            //label4.Text = Convert.ToString(PicCenter.Y);
            ////图片中心变成了272,100
            ////距离pb中心为-22,150
            ////大图片时,pb与picbig的中心差值
            //PicCenterB = new Point((bitBig.Width / 2), (bitBig.Height / 2));
            //Size CenDisB = (Size)PicCenterB - (Size)PbCenter;
            //CenDistBig = CenDisB;
            //label5.Text = Convert.ToString(CenDistBig.Width);
            //label6.Text = Convert.ToString(CenDistBig.Height);     
        }
        //大图绘图,未完成
        private void button4_Click(object sender, EventArgs e)
        {
            int sx = MD.X - PbCenter.X;
            int sy = MD.Y - PbCenter.Y;
            label5.Text = Convert.ToString(sx);
            label6.Text = Convert.ToString(sy);
            Bitmap bit = new Bitmap(500, 500);
            Graphics g = Graphics.FromImage(bit);
            //g.DrawImage(bit1,114-Sx-sx, 200-Sy-sy, bit1.Width,bit1.Height);
            g.DrawImage(bitBig, -CenDistBig.Width - Sx - sx, -CenDistBig.Height - Sy - sy, bitBig.Width, bitBig.Height);
            DrawNumber(pictureBox1, g);
            this.pictureBox1.Image = bit;
            Sx += sx;
            Sy += sy;
        }

    }
}

相关文章

网友评论

      本文标题:点击图片任意位置,此位置移动至图框中心

      本文链接:https://www.haomeiwen.com/subject/jpwvvxtx.html