點擊圖片任意位置,此位置移動至圖框中心

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

    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1、窗體 1、常用屬性 (1)Name屬性:用來獲取或設置窗體的名稱,在應用程序中可通過Name屬性來引用窗體。 ...
    Moment__格調閱讀 4,593評論 0 11
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,229評論 4 61
  • 說一說你平時寫代碼遵守的編碼規范 所有命名都使用英文小寫 命名用引號包裹 名字拼接用中橫線 命名體現功能,不涉及表...
    普萊那閱讀 128評論 0 0
  • 每次兩人休息撞在一起,十有八九會去宜家。原本約定說給小烏龜買個缸,結果逛了一圈,什么零零碎碎都買了,唯獨給小烏龜的...
    一個姓good閱讀 167評論 2 0
  • 終于等到這一部。 8月25日《極盜車神》;9月1日《敦刻爾克》;9月8日《蜘蛛俠:英雄歸來》…… 或許你對前面幾部...
    Sir電影閱讀 575評論 2 7