C#获取像素颜色 [英] C# get pixel color

查看:319
本文介绍了C#获取像素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是在窗口中显示图像,并且当用户单击该图像上的某个位置时,基于该位置的像素颜色,会发生某些情况.

我尝试做的事情是...将面板扔到Windows窗体上,使用on paint方法从磁盘上的图像在该面板上绘制一个位图,然后使用鼠标单击事件使用位图的getPixel方法.问题是鼠标指针的坐标与图像中像素的坐标不匹配..就像我可以在面板内部的某个位置单击图像一样...即使我在蓝色区域内单击也可以,返回的颜色是蓝色区域上方或左侧某区域的颜色...

我该怎么办?



我发现问题出在坐标系上.这当然是因为位图具有固定的尺寸,例如400x400,并且光标坐标是全局的.因此,当单击表单中某处(应为20x200的像素)时,鼠标坐标类似于300x700,因此出现了问题.

关于如何克服这一障碍的任何想法?

What I want to do is display an image in a window, and when the user clicks somewhere on that image, based on the color of the pixel at that position, something happens.

What i tried to do is something like...throw a panel on a windows form, use the on paint method to draw a bitmap on that panel, from an image on my disk, and then use the mouse click event for the panel along with the getPixel method for the bitmap. The problem is the coordinates of the mouse pointer don''t match the coordinates for the pixels in the image..something like I can click somewhere inside the panel, on the image...and I even if i click inside a blue area, the color returned is that for an area somewhere above or to the left of the blue area...

What can I do?



I figured out that the problem comes from the coordinate system. This is of course because the bitmap has a fixed dimension..like 400x400, and the cursor coordinates are global. So when clicking somewhere in the form, where the pixel for 20x200 should be, the mouse coordinates are something like 300x700, hence the problem.

Any ideas on how to overcome this obstacle?

推荐答案

Control::PointToClient [
The Control::PointToClient [^] method looks promising...
:)


听起来像是缩放方面的问题.
如果要使用与源Bitmap不同的尺寸绘制图像,则需要相应地缩放坐标.

这样的事情可能对您有用:

Sounds like you might have an issue with scaling.
If you are drawing the image using a different size that the source Bitmap, the you need to scale the coordinates accordingly.

Something like this might work for you:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace GetPixelExample
{
    public class Form1 : Form
    {
        private class ImageControl : Panel
        {
            private Bitmap bitmap = new Bitmap("C:\\alien.jpg");

            public ImageControl()
            {
                MouseMove += new MouseEventHandler(HandleMouseMove);
            }

            private void HandleMouseMove(object sender, MouseEventArgs e)
            {
                // Scale the window coordinates to the bitmap coordinates
                double windowX = e.X;
                double windowY = e.Y;

                double controlWidth = Width;
                double controlHeight = Height;
                double imageWidth = bitmap.Width;
                double imageHeight = bitmap.Height;

                Color pixel = bitmap.GetPixel(
                   (int)(windowX * bitmap.Width / controlWidth), 
                   (int)(windowY * bitmap.Height / controlHeight));
                System.Diagnostics.Debug.WriteLine(pixel);
            }

            protected override void OnResize(EventArgs eventargs)
            {
                base.OnResize(eventargs);
                Invalidate();
            }

            protected override void  OnPaintBackground(PaintEventArgs pevent)
            {
                 pevent.Graphics.DrawImage(bitmap, Bounds);
            }
        }

        public Form1()
        {
            ImageControl control = new ImageControl();
            control.Dock = DockStyle.Fill;
            Controls.Add(control);
            this.PerformLayout();
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}


这篇关于C#获取像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆