共享个小工具,图片取点工具

时间:2022-11-18 12:27:36
今天做个东西需要获得一张图片上的某个点的坐标,网上没有找到工具,所以就自己动手写了个.
功能很简单,就是载入一张图片后获得图片上任意点的坐标.

下载: http://download.csdn.net/source/1531704
效果:
共享个小工具,图片取点工具

全部代码如下:

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

namespace PicPoint
{
    public partial class frmPic : Form
    {
        int tWidth = 0;
        int tHeigth = 0;
        int mouseX = 0;
        int mouseY = 0;
        public frmPic()
        {
            InitializeComponent();
        }

        private void frmPic_Load(object sender, EventArgs e)
        {
            tWidth = this.Size.Width;
            tHeigth = this.Size.Height;
            lblMouseTip.Visible = false;
            this.pictureBox1.Controls.Add(lblMouseTip);
            this.panel1.Controls.Add(pictureBox1);
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files(*.*)|*.*";
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            SetFile(openFileDialog1.FileName);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox1.Invalidate();
            this.lblMouseTip.Location = new Point(e.X + 10, e.Y);
            this.lblMouseTip.Text = "X=" + e.X + ",Y=" + e.Y;
            mouseX = e.X;
            mouseY = e.Y;
            Graphics g = pictureBox1.CreateGraphics();
            g.DrawLine(new Pen(Color.Red, 1), e.X, 0, e.X, pictureBox1.Size.Height);
            g.DrawLine(new Pen(Color.Red, 1), 0, e.Y, pictureBox1.Size.Width, e.Y);
        }

        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            Graphics g = pictureBox1.CreateGraphics();
            g.DrawLine(new Pen(Color.Red, 1), mouseX, 0, mouseX, pictureBox1.Size.Height);
            g.DrawLine(new Pen(Color.Red, 1), 0, mouseY, pictureBox1.Size.Width, mouseY);
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            lblX.Text = "X=" + e.X.ToString();
            lblY.Text = "Y=" + e.Y.ToString();
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            lblMouseTip.Visible = true;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            lblMouseTip.Visible = false;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmAbout frm = new frmAbout();
            frm.ShowDialog();
        }

        private void frmPic_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Link;
            else e.Effect = DragDropEffects.None; 
        }

        private void frmPic_DragDrop(object sender, DragEventArgs e)
        {
            SetFile(((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString());
        }
        void SetFile(string fullPathname)
        {
            this.Size = new Size(tWidth, tHeigth);//还原为默认大小
            txtFile.Text = fullPathname;
            //获得图片大小
            Image pic = Image.FromFile(fullPathname);
            pictureBox1.Size = new Size(pic.Width, pic.Height);
            pictureBox1.Image = new System.Drawing.Bitmap(fullPathname);
            //获得窗体大小
            int iTmpWidth = this.Size.Width > pic.Width ? this.Size.Width : pic.Width;
            int iTmpHeight = this.Size.Height > pic.Height + tHeigth ? this.Size.Height : pic.Height + tHeigth;
            iTmpWidth = iTmpWidth > 800 ? 800 : iTmpWidth;
            iTmpHeight = iTmpHeight > 600 ? 600 : iTmpHeight;
            this.Size = new Size(iTmpWidth, iTmpHeight);
            //设置图片居中
            if (this.panel1.Width > pictureBox1.Width || this.panel1.Height > pictureBox1.Height)
            {
                if (this.panel1.Width > pictureBox1.Width)
                {
                    pictureBox1.Left = (this.panel1.Width - pictureBox1.Width) / 2;
                }
                if (this.panel1.Height > pictureBox1.Height)
                {
                    pictureBox1.Top = (this.panel1.Height - pictureBox1.Height) / 2;
                }
            }
            else
            {
                pictureBox1.Location = new Point(0, 2);
            }
        }
    }
}

7 个解决方案

#1


吼吼



SIGNATURE:----------------------------------------------------------------

[img=http://id.91cool.net/sign/?name=%CE%DE%D3%C7&say=%B2%C5%B8%DF%C6%DF%B0%CB%B6%B7,%D1%A7%B8%BB%CE%E5%C1%F9%B3%B5,%B8%C4%D0%D0%C8%FD%CB%C4%B4%CE,%D4%C2%C8%EB%D2%BB%B6%FE%C7%A7!][/img]

#2


学习!

#3


UP

#4


谢谢LZ分享

#5


up

#6


````````````````````````会是会做,不过懒得做··········谢谢lz了

#7


看看..

#1


吼吼



SIGNATURE:----------------------------------------------------------------

[img=http://id.91cool.net/sign/?name=%CE%DE%D3%C7&say=%B2%C5%B8%DF%C6%DF%B0%CB%B6%B7,%D1%A7%B8%BB%CE%E5%C1%F9%B3%B5,%B8%C4%D0%D0%C8%FD%CB%C4%B4%CE,%D4%C2%C8%EB%D2%BB%B6%FE%C7%A7!][/img]

#2


学习!

#3


UP

#4


谢谢LZ分享

#5


up

#6


````````````````````````会是会做,不过懒得做··········谢谢lz了

#7


看看..