Results 1 to 8 of 8

Thread: [RESOLVED] Pen DashPattern - alternating colours?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Resolved [RESOLVED] Pen DashPattern - alternating colours?

    Can I create a Pen dashpattern that consists of alternating colours rather than a solid colour followed by a space? For example:

    LinePen.DashPattern = New Single() {6, 4}

    ... creates a pattern with a dash of 6 pixels followed by a space of 4 pixels. Rather than a space, I would like a different solid colour. That way I could create a rectangular cursor with something like a black/white alternating pattern, which would be clearly visible on top of any picture.

    Thanks.

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Pen DashPattern - alternating colours?

    The simplest way to do that is by drawing the cursor twice, with two pens, each with a different Color, DashPattern and DashOffset.

    An alternative way would be to make an image containing two dashes of the alternating colors. Then convert that to a TextureBrush, and convert that to a Pen like this:
    Code:
    Dim pn As New Pen(textureBrush)
    It takes a few more steps, but it is obviously far more flexible because you could use any image.

    BB

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Pen DashPattern - alternating colours?

    Quote Originally Posted by boops boops View Post
    The simplest way to do that is by drawing the cursor twice, with two pens, each with a different Color, DashPattern and DashOffset.

    An alternative way would be to make an image containing two dashes of the alternating colors. Then convert that to a TextureBrush, and convert that to a Pen like this:
    Code:
    Dim pn As New Pen(textureBrush)
    It takes a few more steps, but it is obviously far more flexible because you could use any image.

    BB
    The TextureBrush sounds like a good idea. I already have quite a lot drawing in my cursor routine so I would rather not have to double everything.

    BTW, I sent you a PM a couple of weeks ago. Perhaps you didn't get it? I had a query about your FastPix routine. I can't remember exactly what the query was now! I think it was something to do with using the byte array data - it didn't seem to be working properly. I think I just changed to GetPixel and SetPixel (which I use a lot) and it was fine.

    Thanks, as always, for your help.
    Last edited by paulg4ije; Feb 20th, 2012 at 10:02 AM.

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Pen DashPattern - alternating colours?

    Quote Originally Posted by paulg4ije View Post
    BTW, I sent you a PM a couple of week ago. Perhaps you didn't get it? I had a query about your FastPix routine. I can't remember exactly what the query was now! I think it was something to do with using the byte array data - it didn't seem to be working properly. I think I just changed to GetPixel and SetPixel (which I use a lot) and it was fine.

    Thanks, as always, for your help.
    Sorry, it's been a frantic period lately. I would have been more inclined to help you if you had posted your question to the FastPix thread in the code bank. Sometimes posting stuff there is like talking to yourself.

    BB

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Pen DashPattern - alternating colours?

    Quote Originally Posted by boops boops View Post
    Sorry, it's been a frantic period lately. I would have been more inclined to help you if you had posted your question to the FastPix thread in the code bank. Sometimes posting stuff there is like talking to yourself.

    BB
    No problem. I use FastPix a lot and it has been very helpful.

    Many thanks

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Pen DashPattern - alternating colours?

    I just had a thought: the dashes won't work very well using the TextureBrush method, because they won't follow the direction of the line. The TextureBrush just "shows through" the line of the pen so vertical lines would have a solid color, either black or white. You could make some kind of checkerboard pattern to use as the image instead of the two dashes, but I expect using two pens will look better. It won't be any less efficient. If your cursor is more elaborate than just a rectangle, you could build it as a GraphicsPath and draw that instead of drawing parts individually. BB

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Pen DashPattern - alternating colours?

    Quote Originally Posted by boops boops View Post
    I just had a thought: the dashes won't work very well using the TextureBrush method, because they won't follow the direction of the line. The TextureBrush just "shows through" the line of the pen so vertical lines would have a solid color, either black or white. You could make some kind of checkerboard pattern to use as the image instead of the two dashes, but I expect using two pens will look better. It won't be any less efficient. If your cursor is more elaborate than just a rectangle, you could build it as a GraphicsPath and draw that instead of drawing parts individually. BB
    Yeah, I've just tried a checkerboard pattern and it's not bad, but not quite right. The effect depends on exactly where the cursor is placed. I will look at using two pens and drawing the rectangle twice.

    Thanks again.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Pen DashPattern - alternating colours?

    The two pen idea works well. I used this pattern:

    LinePen(1).DashPattern = New Single() {6, 6}
    LinePen(2).DashPattern = New Single() {1, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}

    This gives a near-perfect dashed line. There may be a better way but this works for me

    Thanks Boops ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width