Fli*_*nta 1 c# image bitmap picturebox
好的,我有一个带有图像的图片框,其 sizeMode 设置为:StretchImage,
现在,我想获取我单击的像素。(位图。GetPixel(x,y))。
但是当图像从正常尺寸拉伸时,我得到原始像素。就像在拉伸之前存在的像素一样(如果这有意义?)
我的代码:
Private void pictureBox1_MouseUp(object sender, MouseEventArgs e) {
Bitmap img = (Bitmap)pictureBox1.Image;
var color = img.GetPixel(e.X, e.Y)
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
应该有一种方法可以补偿由图片框引起的拉伸因子。我正在考虑采取拉伸宽度和高度从图片框,宽度和高度从原来的图像,计算拉伸系数,再乘以这些与e.X和e.Y坐标。也许是这样的:
Bitmap img = (Bitmap)pictureBox1.Image;
float stretch_X = img.Width / (float)pictureBox1.Width;
float stretch_Y = img.Height / (float)pictureBox1.Height;
var color = img.GetPixel((int)(e.X * stretch_X), (int)(e.Y * stretch_Y));
Run Code Online (Sandbox Code Playgroud)