好的,我有一个带有图像的pictureBox,sizeMode设置为: StretchImage,
现在,我想得到我点击的像素。(bitmap.GetPixel(x,y))
但当图像是从正常大小,我得到原来的像素。就像在链球菌之前的像素一样(如果这有意义的话?)
我的守则:
Private void pictureBox1_MouseUp(object sender, MouseEventArgs e) {
Bitmap img = (Bitmap)pictureBox1.Image;
var color = img.GetPixel(e.X, e.Y)
}提前谢谢
发布于 2015-09-04 17:17:11
应该有一种方法来补偿由图片框引起的拉伸因子。我正在考虑从图片框中获取拉伸的宽度和高度,以及从原始图像中获取的宽度和高度,计算拉伸因子,并将其乘以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)); 发布于 2015-09-04 17:28:38
用拉伸因子除以e.X和e.Y。这是拉伸的图像填充整个图片框。
Bitmap img = (Bitmap)pictureBox1.Image;
float factor_x = (float)pictureBox1.Width / img.Width;
float factor_y = (float)pictureBox1.Height / img.Height;
var color = img.GetPixel(e.X / factor_x, e.Y / factor_y)通过这样做,我们确保e.X和e.Y不会超过原始图像的限制。
发布于 2015-09-04 17:21:25
你可以存储原始图像并保持原状不变。这将比调整拉伸图像的大小和获取指定的像素后缀更容易。确保e.X和e.Y不会超出原始位图的范围。
private Bitmap _img;
public void LoadImage(string file) {
// Get the image from the file.
pictureBox1.Image = Bitmap.FromFile(file);
// Convert it to a bitmap and store it for later use.
_img = (Bitmap)pictureBox1.Image;
// Code for stretching the picturebox here.
// ...
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) {
var color = _img.GetPixel(e.X, e.Y);
}编辑:漠视。马西米兰的答案更好。
https://stackoverflow.com/questions/32403295
复制相似问题