Skip to main content
 首页 » 编程设计

C# 如何从拉伸(stretch)位图/图片框获取像素

2024年02月05日12zhoujg

好的,我有一个带有图像的 pictureBox,其 sizeMode 设置为:StretchImage,
现在,我想获得我点击的像素。 (位图.GetPixel(x,y))。
但是当图像从正常尺寸拉伸(stretch)时,我得到了原始像素。就像在 strech 之前的像素中一样(如果这有意义?)

我的代码:

Private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { 
  Bitmap img = (Bitmap)pictureBox1.Image;  
  var color = img.GetPixel(e.X, e.Y) 
} 

提前致谢

请您参考如下方法:

应该有一种方法来补偿由图片框引起的拉伸(stretch)因素。我正在考虑从图片框中获取拉伸(stretch)后的宽度和高度,以及从原始图像中获取拉伸(stretch)后的宽度和高度,计算拉伸(stretch)因子,然后将它们与 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));