请问如何获取pictureBox中图片的像素位置点的坐标啊

俺老刘 2017-05-16 06:38:07



//我想自动获取p1点和size信息
Point p1=new Point(100,50);//起始点p1
Size s=new Size (300,200);//选取的图像宽和高
也就是用鼠标在左侧图片上框一个矩形。然后自动计算出开始截取点和截取的图片宽度和高度



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 test
{
public partial class 去除指定颜色 : Form
{
public 去除指定颜色()
{
InitializeComponent();
}

private void btnSelect_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog .ShowDialog() == DialogResult.OK)
{
this.txbPic.Text = dialog.FileName;
Bitmap bm = new Bitmap(Image.FromFile(dialog.FileName));
//pictureBox1.BackgroundImage =bm;
this.pictureBox1.ImageLocation=dialog.FileName;
}
}
private void btnCut_Click(object sender, EventArgs e)
{
Bitmap source = new Bitmap(txbPic.Text);

//我想自动获取p1点和size信息
Point p1=new Point(100,50);//起始点p1
Size s=new Size (300,200);//选取的图像宽和高


Rectangle section = new Rectangle(p1,s);

Bitmap cutPic = CropImage(source, section);
pictureBox2.Image = cutPic;
}
private void btnRemove_Click(object sender, EventArgs e)
{

}


//剪切图片
public Bitmap CropImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
Bitmap bmp = new Bitmap(section.Width, section.Height);

Graphics g = Graphics.FromImage(bmp);

// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

return bmp;
}


}
}

...全文
2460 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39152950 2019-04-20
  • 打赏
  • 举报
回复
Point pxy = pictureBox1.PointToClient(Control .MousePosition); Point Ixy=new Point(0,0);//图片的起始位置 Ixy.X = 0; int yy = (int)((double)pictureBox1.Width / pictureBox1.Image.Width*100); Ixy.Y = (pictureBox1.Height - pictureBox1.Image.Height * yy / 100) / 2; Bitmap btp = new Bitmap(pictureBox1 .Image ); MessageBox.Show(""+pxy+"--"+Ixy .Y );
qq_39152950 2019-04-20
  • 打赏
  • 举报
回复
Point pxy = pictureBox1.PointToClient(Control .MousePosition); Point Ixy=new Point(0,0);//图片的起始位置 Ixy.X = 0; int yy = (int)((double)pictureBox1.Width / pictureBox1.Image.Width*100); Ixy.Y = (pictureBox1.Height - pictureBox1.Image.Height * yy / 100) / 2; Bitmap btp = new Bitmap(pictureBox1 .Image ); MessageBox.Show(""+pxy+"--"+Ixy .Y );
xuzuning 2017-05-30
  • 打赏
  • 举报
回复
在 MouseMove 事件中,记录的所有鼠标键按下时的 e.X、e.Y 鼠标键抬起后,取得最小的 x、y 作为左上角坐标,取得最大的 x、y 作为右下角坐标
俺老刘 2017-05-28
  • 打赏
  • 举报
回复
这个我后来想了想,还是用鼠标框一个范围,然后给出算出左上角和右下角的坐标 自然就可以知道了,不知道怎么实现
冷场 2017-05-17
  • 打赏
  • 举报
回复
e里面貌似有x和y吧
xuzuning 2017-05-16
  • 打赏
  • 举报
回复
何为自动

111,123

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧