Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. mousedown event

mousedown event

Scheduled Pinned Locked Moved C#
tutorial
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tim_gunning
    wrote on last edited by
    #1

    hi all i did this a while ago but lost everything on my comp and now cant remember how I did it. I remember it was fairly simple but I cant for the life of me remember what or how I did it. Basically I want to get the colour of the pixel under my mouse pointer. I've loaded the image into the picturebox (picLayout) and have the mouseevent being handled I just can't remember how to get the colour of the pixel. any pointers well appreciated TIA tim

    M 1 Reply Last reply
    0
    • T tim_gunning

      hi all i did this a while ago but lost everything on my comp and now cant remember how I did it. I remember it was fairly simple but I cant for the life of me remember what or how I did it. Basically I want to get the colour of the pixel under my mouse pointer. I've loaded the image into the picturebox (picLayout) and have the mouseevent being handled I just can't remember how to get the colour of the pixel. any pointers well appreciated TIA tim

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, I think it is a good way to use the Bitmap.GetPixel function.

      using(Bitmap bm = new Bitmap(pictureBox2.BackgroundImage))
      {
      actColor = bm.GetPixel(e.X,e.Y);
      }

      Hope it helps!

      All the best, Martin

      T 1 Reply Last reply
      0
      • M Martin 0

        Hello, I think it is a good way to use the Bitmap.GetPixel function.

        using(Bitmap bm = new Bitmap(pictureBox2.BackgroundImage))
        {
        actColor = bm.GetPixel(e.X,e.Y);
        }

        Hope it helps!

        All the best, Martin

        T Offline
        T Offline
        tim_gunning
        wrote on last edited by
        #3

        hi martin thanks for that but I'm getting an error (im probably being a bit thick here but.....) i have this when the image loads: this.picLayout.MouseDown += new MouseEventHandler(this.HandleMouseDown); and then: public void HandleMouseDown(object sender, MouseEventArgs e) { using(Bitmap bm = new Bitmap(picLayout.BackgroundImage)) { label2.Text = bm.GetPixel(e.X,e.Y).ToString(); } } and it just crashes it, any thoughts (apologies ofr my dimwittedness - brain just isn't functioning atm) also i was thinking about having this as a mouseover event, if its creating bitmaps on every mousemove event wont it just explode ? I wish I could remember what I did before, it was fairly simple and something along the line imgName.R.ToString(); ta

        M L 2 Replies Last reply
        0
        • T tim_gunning

          hi martin thanks for that but I'm getting an error (im probably being a bit thick here but.....) i have this when the image loads: this.picLayout.MouseDown += new MouseEventHandler(this.HandleMouseDown); and then: public void HandleMouseDown(object sender, MouseEventArgs e) { using(Bitmap bm = new Bitmap(picLayout.BackgroundImage)) { label2.Text = bm.GetPixel(e.X,e.Y).ToString(); } } and it just crashes it, any thoughts (apologies ofr my dimwittedness - brain just isn't functioning atm) also i was thinking about having this as a mouseover event, if its creating bitmaps on every mousemove event wont it just explode ? I wish I could remember what I did before, it was fairly simple and something along the line imgName.R.ToString(); ta

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          going_mental wrote:

          but I'm getting an error

          Standard Question No.?: What does the error say?

          going_mental wrote:

          and it just crashes it

          runtime or designtime?

          All the best, Martin

          T 1 Reply Last reply
          0
          • M Martin 0

            going_mental wrote:

            but I'm getting an error

            Standard Question No.?: What does the error say?

            going_mental wrote:

            and it just crashes it

            runtime or designtime?

            All the best, Martin

            T Offline
            T Offline
            tim_gunning
            wrote on last edited by
            #5

            hmmm, told you my brain wasn't working.... i should have used picLayout.Image rather than picLayout.BackgroundImage. it was throwing an unhandled exception (which confused me as tried to catch it but it was happening before it got to my try/catch) over the lack of background image in the picturebox. thanks, all working now, but it would be nice to be able to do this as a mouseover event rather than just mousedown. I just tried it as mouseover event and the cpu load kinda went a bit mad. Any thoughts ? when i did it before it was fairly simple and something along the line imgName.R.ToString(); tim

            M 1 Reply Last reply
            0
            • T tim_gunning

              hmmm, told you my brain wasn't working.... i should have used picLayout.Image rather than picLayout.BackgroundImage. it was throwing an unhandled exception (which confused me as tried to catch it but it was happening before it got to my try/catch) over the lack of background image in the picturebox. thanks, all working now, but it would be nice to be able to do this as a mouseover event rather than just mousedown. I just tried it as mouseover event and the cpu load kinda went a bit mad. Any thoughts ? when i did it before it was fairly simple and something along the line imgName.R.ToString(); tim

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Glad I could help!

              All the best, Martin

              T 1 Reply Last reply
              0
              • T tim_gunning

                hi martin thanks for that but I'm getting an error (im probably being a bit thick here but.....) i have this when the image loads: this.picLayout.MouseDown += new MouseEventHandler(this.HandleMouseDown); and then: public void HandleMouseDown(object sender, MouseEventArgs e) { using(Bitmap bm = new Bitmap(picLayout.BackgroundImage)) { label2.Text = bm.GetPixel(e.X,e.Y).ToString(); } } and it just crashes it, any thoughts (apologies ofr my dimwittedness - brain just isn't functioning atm) also i was thinking about having this as a mouseover event, if its creating bitmaps on every mousemove event wont it just explode ? I wish I could remember what I did before, it was fairly simple and something along the line imgName.R.ToString(); ta

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, why would you need a new Bitmap in your MouseDown handler ? This is what I would try:

                public void HandleMouseDown(object sender, MouseEventArgs e) {
                Bitmap bm = picLayout.Image as Bitmap;
                string s= bm==null ? "Not a bitmap" : bm.GetPixel(e.X,e.Y).ToString();
                label2.Text = s;
                }

                And since this is a cheap operation now, you could try it in MouseHover handler also. :)

                Luc Pattyn


                try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                M T 2 Replies Last reply
                0
                • M Martin 0

                  Glad I could help!

                  All the best, Martin

                  T Offline
                  T Offline
                  tim_gunning
                  wrote on last edited by
                  #8

                  hi martin just to pick your brains a little further.....it would be nice to be able to do this as a mouseover event rather than just mousedown. I just tried it as mouseover event and the cpu load kinda went a bit mad. when i did it before it was fairly simple and something along the lines of imgName.R.ToString(); or something like that. is that something you've heard of ? I can access the RGB values for the background but obviously thats not much use (as its just grey) but it wont let do the same for the Image. Any thoughts ?

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, why would you need a new Bitmap in your MouseDown handler ? This is what I would try:

                    public void HandleMouseDown(object sender, MouseEventArgs e) {
                    Bitmap bm = picLayout.Image as Bitmap;
                    string s= bm==null ? "Not a bitmap" : bm.GetPixel(e.X,e.Y).ToString();
                    label2.Text = s;
                    }

                    And since this is a cheap operation now, you could try it in MouseHover handler also. :)

                    Luc Pattyn


                    try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                    M Offline
                    M Offline
                    Martin 0
                    wrote on last edited by
                    #9

                    :doh:

                    All the best, Martin

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Hi, why would you need a new Bitmap in your MouseDown handler ? This is what I would try:

                      public void HandleMouseDown(object sender, MouseEventArgs e) {
                      Bitmap bm = picLayout.Image as Bitmap;
                      string s= bm==null ? "Not a bitmap" : bm.GetPixel(e.X,e.Y).ToString();
                      label2.Text = s;
                      }

                      And since this is a cheap operation now, you could try it in MouseHover handler also. :)

                      Luc Pattyn


                      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                      T Offline
                      T Offline
                      tim_gunning
                      wrote on last edited by
                      #10

                      excellent, that does the trick nicely. thanks to you both

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups