.Net中的PaintBrush [英] PaintBrush in .Net

查看:100
本文介绍了.Net中的PaintBrush的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从位图图像中选择像素(鼠标移动的任何特定区域).
在运行时,将有问题的位图图像加载到图片框上,并通过Graphics对象完成对该位图的操作.

How can I Select Pixels(any particular area on Mouse Movement) From a Bitmap Image.
The Bitmap Image in Question Is Loaded on a Picture Box at run time, and Operation on this Bitmap is done through Graphics object.

Do You have this answer?????

推荐答案

类似的内容可能对您有用:

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 of pixel under cursor grabbed here
                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());
        }
    }
}


我同意Fredrik,但是我将使用不同的措辞:

PictureBox在这里完全没有帮助;您也可以在简单的面板上绘制图像.

主要问题是您所称的图像可能会缩放,也可能不会缩放,重新缩放.因此,一切归结为找到与您的鼠标位置相对应的位图坐标.对于非旋转图像,该关系为线性和一维关系,即x和y都满足z(shown) = coefficient * z(intended) + offset
形式的转换
将极端值(左上和右下)塞入其中,将为您提供正确的系数和偏移量.

如果要避免所有缩放效果,请不要使用背景图像,只需在面板的绘制处理程序中添加Graphics.DrawImage()即可绘制图像而无需缩放,则鼠标在面板中的位置直接对应于x,y索引到位图中(只要不离开绘制的图像).

:)
I agree with Fredrik, however I will use different wording:

a PictureBox does not help at all here; you can as well paint the image on a simple Panel.

The main problem is your image may or may not be zoomed, stretched, rescaled, whatever you call it. So it all boils down to finding the bitmap coordinates that correspond to your mouse position. For non-rotated images, the relation is linear and one-dimensional, i.e. both x and y satisfy a transformation of the form z(shown) = coefficient * z(intended) + offset

Stuffing the extremes (top left and bottom right) into this, will give you the correct coefficients and offsets.

If you want to avoid all scaling effects, don''t use a background image, simply add a Graphics.DrawImage() to the Panel''s paint handler which paints the image without scaling, then the mouse position within the panel corresponds directly to the x,y indices into the bitmap (as long as you don''t leave the drawn image).

:)


这篇关于.Net中的PaintBrush的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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