none
Drawline doesn't respect Form.TransparencyKey on XP RRS feed

  • Question

  • I'm writing an application that has a transparent window with a line drawn on it that points to a portion of the screen. I do this in a very simple way, by setting the form's background and transparencykey to Fuchsia. The form is fullscreen, so instead of my old approach of invalidating it every time I need to redraw the line on it, I am trying to draw another line, first, in the transparent color, in order to blank out the original.

    So basically:
    Point p1, p2;
    
    void updatelines(Graphics g)
    {
          g.DrawLine(Pen.Fuchsia, p1, p2);
          UpdatePoints();
          g.DrawLine(Pen.Black, p1, p2);
    }
    This works just fine on my Vista machine, but when I went to try it on the XP machine, I am seeing all the Fuchsia lines not transparent at all.

    After some experimenting, it seems that SOME methods in the Graphics class respect the transparency key on XP, but some don't.

    As a rule of thumb, most of the ones that involve Pens don't work, but most Brush ones (like fillXXX) work. FillRectangle works, FillPath doesn't work... fillRegion(new Region(myGraphicsPath) works...


    I could go back to invalidating, or drawing rectangles, but that's noticably slower, and causes flickering, because these updates have to happen quite often. (as a window is being dragged)

    Both computers have .net 2.0sp1 installed, plus up to .net 3.5 (but the app is in 2.0)
    Thursday, June 4, 2009 3:10 PM

All replies

  • TransparencyKey works through a hardware feature of the video adapter, there is no way that the Graphics class could bypass it.  More likely is that the lines you draw don't have the exact same color value as TransparencyKey.  This is common on images that get stretched, I haven't heard this to be an issue on line drawing before.  Tinker with Graphics.InterpolationMode and Graphics.SmoothingMode.

    And beware of video drivers that take short-cuts for slight speed improvements.

    Hans Passant.
    Thursday, June 4, 2009 4:24 PM
    Moderator
  • Thanks for your quick reply.

    The smoothing mode is set to none. I'll try interpolation mode. As for colors... do you have any idea what else might be modifying the color? Because I am using named colors (and have checked that their values are the same).

    It's interesting you mention it is a hardware feature. I didn't know that. Perhaps I will try to update the graphics drivers. Also, I just got a colleague of mine to try it on his XP machine and he wasn't experiencing that problem. Maybe it's something buggy with the .net installation?


    I don't think it has to do with stretching, because the lines are very crisp / 100% aliased, with no blurred edges.

    edit: I also just ran my application and took a screenshot of the line color, to see if it was actually off by a few shades, but it's reporting the correct color.
    Thursday, June 4, 2009 5:07 PM
  • Hi Kir,

     

    I did test your code and it works fine both on my Windows 7 and Windows XP.

    I think you can check below items on two machines, on one of which it works fine and on the other it doesn’t. This could help you find out the cause of your problem.

    1.       The version of the Windows XP. One may be SP2, but the other is SP3.

    2.       The version of the .net framework. One may be 2.0, but the other is 3.5.

    3.       The setting of the machine. Such as screen resolution, color quality and so on.

     

    Let me know if this helps.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Monday, June 8, 2009 6:35 AM
    Moderator
  • Hi Aland,

    I've looked and both machines are SP3. The versions of .net are slightly different, but from what I can tell it's due to the operating system differences. Maybe you can clarify this for me.

    The two XP machines report .net 2.0 version 2.2.30729. Which seems to be a version of 2.0 SP2, but on my vista machine the version is 2.0.50727.3053. If I try to install the other version, which I can't actually even find listed on MSDN downloads, it says it's not compatible with Vista. This lead me to understand that Vista comes with updates preinstalled which make this version irrelevant (I do not even see .net framework 2.0 in my Program Features list on Vista).

    Both the Vista and XP machine (the XP one that does not work) also have .net 3.5 installed (though the application is not using any 3.5 libraries)
    Monday, June 8, 2009 12:33 PM
  • Hi Kir, 

    Based on my understanding, the problem is mostly relative to the graphics driver. Before we go any further, would you mind doing extra tests in your machine as follows.

    1.       Install a virtual machine software in your Windows Vista system, such as virtual PC.

    2.       Add a Windows XP system to the virtual machine, the version of which is SP3.

    3.       Install dot net framework in the Windows XP system, the version of which is 2.2.30729.

    4.       Install Virtual Machine Additions.

    5.       Test the program in this system.

     

    If the program works fine, please reinstall or update the graphics driver of your Windows XP system.

     

    Let me know if this helps.
    Aland Li

     

     


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Tuesday, June 9, 2009 8:21 AM
    Moderator
  • Thanks for the tip, Aland.

    I am trying it right now on the Virtual Machine on my computer. I'm not sure what you mean by Virtual Machine Additions (I'm using Virtual PC)

    With .net 2.0 sp1 2.1.21022 - I witness the problem.
    With .net 2.0 sp2 2.2.30729 - the problem persists
    With .net 3.5 sp1 - still have it

    And this is on the computer where it works on vista.

    I replicated this with a very simple program which I'm using to check:

    Form1 - Background Color: Fuchsia, Transparent Key: Fuchsia
    Paint:
    Graphics g = e.Graphics;
    g.DrawLine(Pens.Fuchsia, new Point(100, 0), new Point(100, 300));
    g.FillRectangle(Brushes.Fuchsia, new Point(200, 0), new Size(100, 100));

    Very bizarre... And I tried updating the drivers on the original xp computer - they are the latest ati catalyst drivers as of June 6
    Tuesday, June 9, 2009 12:31 PM
  • Hi Kir, 

    I also used Virtual PC to run the virtual machine. Virtual Machine Additions means some software you need to install on the virtual machine system, you can click menu Action
    -> Install or Update Virtual Machine Additions
    to start the installation. I think you can update the graphics driver at first, then run your program. If it still cannot work, please compare the machine on which it works find with your machine, then provide me with the differences between them. Thanks.

    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Thursday, June 11, 2009 6:03 AM
    Moderator