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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Transparency in windows forms - doesn't work on some pcs

Transparency in windows forms - doesn't work on some pcs

Scheduled Pinned Locked Moved C#
graphicshelpwinformstutorialquestion
23 Posts 4 Posters 27 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.
  • D Offline
    D Offline
    Delta Z
    wrote on last edited by
    #1

    Hi I have created an application with transparent main window that displays some text. To initially display and change the text, I use the following code: if (behavior.IsVisible) { Show(); mImage = new Bitmap(Width, Height); Graphics dc = Graphics.FromHwnd(Handle); dc.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; SizeF size = dc.MeasureString(behavior.DisplayText, behavior.Font); dc.Dispose(); Height = (int)size.Height + 1; Width = (int)size.Width + 1; mImage = new Bitmap(Width, Height); dc = Graphics.FromImage(mImage); dc.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; Color background = (behavior.Color.ToArgb() == Color.White.ToArgb()) ? Color.Black : Color.White; dc.Clear(background); dc.DrawString(behavior.DisplayText, behavior.Font, new SolidBrush(behavior.Color), 0, 0); mTextColor = behavior.Color.ToArgb(); dc.Dispose(); BackgroundImage = mImage; TransparencyKey = background; } else { Hide(); BackgroundImage = null; } For some reason the window shows transparent on my PC and at least one other, while on most I still get a white background (i.e. no transparency). The only difference I can think of is that on the PCs where the transparency displays correctly, VS2005 is installed, while it's VS2003 or none on the others (I'm using latest video drivers etc). Does anyone know of this issue and how to fix it?

    Thanks, Dmitry

    B M S 3 Replies Last reply
    0
    • D Delta Z

      Hi I have created an application with transparent main window that displays some text. To initially display and change the text, I use the following code: if (behavior.IsVisible) { Show(); mImage = new Bitmap(Width, Height); Graphics dc = Graphics.FromHwnd(Handle); dc.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; SizeF size = dc.MeasureString(behavior.DisplayText, behavior.Font); dc.Dispose(); Height = (int)size.Height + 1; Width = (int)size.Width + 1; mImage = new Bitmap(Width, Height); dc = Graphics.FromImage(mImage); dc.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; Color background = (behavior.Color.ToArgb() == Color.White.ToArgb()) ? Color.Black : Color.White; dc.Clear(background); dc.DrawString(behavior.DisplayText, behavior.Font, new SolidBrush(behavior.Color), 0, 0); mTextColor = behavior.Color.ToArgb(); dc.Dispose(); BackgroundImage = mImage; TransparencyKey = background; } else { Hide(); BackgroundImage = null; } For some reason the window shows transparent on my PC and at least one other, while on most I still get a white background (i.e. no transparency). The only difference I can think of is that on the PCs where the transparency displays correctly, VS2005 is installed, while it's VS2003 or none on the others (I'm using latest video drivers etc). Does anyone know of this issue and how to fix it?

      Thanks, Dmitry

      B Offline
      B Offline
      blackjack2150
      wrote on last edited by
      #2

      I'm not 100% sure, but I think that transparent windows can only be rendered in Windows XP.

      D 1 Reply Last reply
      0
      • B blackjack2150

        I'm not 100% sure, but I think that transparent windows can only be rendered in Windows XP.

        D Offline
        D Offline
        Delta Z
        wrote on last edited by
        #3

        You are correct, but I am talking all XP SP2 PCs here, its 2007 ;) I think it is probably different DLL versions, like different common controls DLL, but I don't know where to start and what to look for.

        1 Reply Last reply
        0
        • D Delta Z

          Hi I have created an application with transparent main window that displays some text. To initially display and change the text, I use the following code: if (behavior.IsVisible) { Show(); mImage = new Bitmap(Width, Height); Graphics dc = Graphics.FromHwnd(Handle); dc.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; SizeF size = dc.MeasureString(behavior.DisplayText, behavior.Font); dc.Dispose(); Height = (int)size.Height + 1; Width = (int)size.Width + 1; mImage = new Bitmap(Width, Height); dc = Graphics.FromImage(mImage); dc.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; Color background = (behavior.Color.ToArgb() == Color.White.ToArgb()) ? Color.Black : Color.White; dc.Clear(background); dc.DrawString(behavior.DisplayText, behavior.Font, new SolidBrush(behavior.Color), 0, 0); mTextColor = behavior.Color.ToArgb(); dc.Dispose(); BackgroundImage = mImage; TransparencyKey = background; } else { Hide(); BackgroundImage = null; } For some reason the window shows transparent on my PC and at least one other, while on most I still get a white background (i.e. no transparency). The only difference I can think of is that on the PCs where the transparency displays correctly, VS2005 is installed, while it's VS2003 or none on the others (I'm using latest video drivers etc). Does anyone know of this issue and how to fix it?

          Thanks, Dmitry

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

          Hello, Had the same problem and if you search in google or msdn you will find a lot of other people sharing it. What I found out is, that it has something todo with the graphic card and the Color quality (16bit or 32 bit). I use the MakeTransparent method of the Bitmap class instead. This works fine for me!

          Color transparentcolor = bkgrnd.GetPixel(0,0); //If you know the pixel where the TransparentColor should be
          BackgroundImage.MakeTransparent(transparentcolor);

          Hope it helps! All the best, Martin

          D 1 Reply Last reply
          0
          • D Delta Z

            Hi I have created an application with transparent main window that displays some text. To initially display and change the text, I use the following code: if (behavior.IsVisible) { Show(); mImage = new Bitmap(Width, Height); Graphics dc = Graphics.FromHwnd(Handle); dc.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; SizeF size = dc.MeasureString(behavior.DisplayText, behavior.Font); dc.Dispose(); Height = (int)size.Height + 1; Width = (int)size.Width + 1; mImage = new Bitmap(Width, Height); dc = Graphics.FromImage(mImage); dc.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; Color background = (behavior.Color.ToArgb() == Color.White.ToArgb()) ? Color.Black : Color.White; dc.Clear(background); dc.DrawString(behavior.DisplayText, behavior.Font, new SolidBrush(behavior.Color), 0, 0); mTextColor = behavior.Color.ToArgb(); dc.Dispose(); BackgroundImage = mImage; TransparencyKey = background; } else { Hide(); BackgroundImage = null; } For some reason the window shows transparent on my PC and at least one other, while on most I still get a white background (i.e. no transparency). The only difference I can think of is that on the PCs where the transparency displays correctly, VS2005 is installed, while it's VS2003 or none on the others (I'm using latest video drivers etc). Does anyone know of this issue and how to fix it?

            Thanks, Dmitry

            S Offline
            S Offline
            Stevo Z
            wrote on last edited by
            #5

            I'm facing exactly same problem. I just doesn't work on some PC's (with XP SP2) . If u find any sollution I would be very gratefull :) . I was thinking that it is caused by some settings of desktop... anyway, it causes me big headache

            D 1 Reply Last reply
            0
            • M Martin 0

              Hello, Had the same problem and if you search in google or msdn you will find a lot of other people sharing it. What I found out is, that it has something todo with the graphic card and the Color quality (16bit or 32 bit). I use the MakeTransparent method of the Bitmap class instead. This works fine for me!

              Color transparentcolor = bkgrnd.GetPixel(0,0); //If you know the pixel where the TransparentColor should be
              BackgroundImage.MakeTransparent(transparentcolor);

              Hope it helps! All the best, Martin

              D Offline
              D Offline
              Delta Z
              wrote on last edited by
              #6

              I have seen the postings of other people, however those that do have a solution are for C or Java. One of them talks about disabling DirectDraw acceleration for the application. It would be fine with me, but I could not find a way to do it in C# (no search results whatsoever). I have tried MakeTransparent, but it doesn't work at all - now I see a background painted grey ("transparent" color) on all computers. :( Can you explain more how you are using it so it works? Thanks, Dmitry

              M 1 Reply Last reply
              0
              • S Stevo Z

                I'm facing exactly same problem. I just doesn't work on some PC's (with XP SP2) . If u find any sollution I would be very gratefull :) . I was thinking that it is caused by some settings of desktop... anyway, it causes me big headache

                D Offline
                D Offline
                Delta Z
                wrote on last edited by
                #7

                I have tried both 32 and 16 bit color, doesn't change anything on my PC. I will go see if it changes something on the ones that do not work. Its strange that there is no posting from MS on this, maybe we should post it on their newsboard. If I find a solution I will post it here. Good luck, Dmitry

                S 1 Reply Last reply
                0
                • D Delta Z

                  I have seen the postings of other people, however those that do have a solution are for C or Java. One of them talks about disabling DirectDraw acceleration for the application. It would be fine with me, but I could not find a way to do it in C# (no search results whatsoever). I have tried MakeTransparent, but it doesn't work at all - now I see a background painted grey ("transparent" color) on all computers. :( Can you explain more how you are using it so it works? Thanks, Dmitry

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

                  Hello, I'm using it for Forms with a BackgroundImage. This Image has rounded edges with a special color, which is not used in other regions of the Image. I don't use the TransparencyKey property at all!

                  //I'm getting my Image from a file during runtime
                  Bitmap bkgrnd = new Bitmap(myimagepath);
                  Color transparentcolor = bkgrnd.GetPixel(0,0);
                  bkgrnd.MakeTransparent(transparentcolor );
                  this.BackgroundImage = bkgrnd;

                  Hope it helps! All the best, Martin

                  S D 2 Replies Last reply
                  0
                  • D Delta Z

                    I have tried both 32 and 16 bit color, doesn't change anything on my PC. I will go see if it changes something on the ones that do not work. Its strange that there is no posting from MS on this, maybe we should post it on their newsboard. If I find a solution I will post it here. Good luck, Dmitry

                    S Offline
                    S Offline
                    Stevo Z
                    wrote on last edited by
                    #9

                    I've tried to go from 32 to 16 bit and it helped!!! But it looks that it's diffrent from one PC to another... And using MakeTransparent didn't help at all...

                    1 Reply Last reply
                    0
                    • M Martin 0

                      Hello, I'm using it for Forms with a BackgroundImage. This Image has rounded edges with a special color, which is not used in other regions of the Image. I don't use the TransparencyKey property at all!

                      //I'm getting my Image from a file during runtime
                      Bitmap bkgrnd = new Bitmap(myimagepath);
                      Color transparentcolor = bkgrnd.GetPixel(0,0);
                      bkgrnd.MakeTransparent(transparentcolor );
                      this.BackgroundImage = bkgrnd;

                      Hope it helps! All the best, Martin

                      S Offline
                      S Offline
                      Stevo Z
                      wrote on last edited by
                      #10

                      This didn't help me.... :(

                      M 1 Reply Last reply
                      0
                      • S Stevo Z

                        This didn't help me.... :(

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

                        Zilo(svk) wrote:

                        This didn't help me....

                        Why? Show me the code you are using! All the best, Martin

                        S 1 Reply Last reply
                        0
                        • M Martin 0

                          Zilo(svk) wrote:

                          This didn't help me....

                          Why? Show me the code you are using! All the best, Martin

                          S Offline
                          S Offline
                          Stevo Z
                          wrote on last edited by
                          #12

                          I don't know why, it just doesn't work. Myabe I'm doing something wrong... Resources.StartUpPicture.MakeTransparent(Color.FromArgb(80, 80, 80)); //or //Color transparentColor = Resources.StartUpPicture.GetPixel(0, 0); //Resources.StartUpPicture.MakeTransparent(transparentColor); //doesn't matter, I know the color values this.BackgroundImage = Resources.StartUpPicture; this.BackgroundImageLayout = ImageLayout.Stretch;

                          M 1 Reply Last reply
                          0
                          • S Stevo Z

                            I don't know why, it just doesn't work. Myabe I'm doing something wrong... Resources.StartUpPicture.MakeTransparent(Color.FromArgb(80, 80, 80)); //or //Color transparentColor = Resources.StartUpPicture.GetPixel(0, 0); //Resources.StartUpPicture.MakeTransparent(transparentColor); //doesn't matter, I know the color values this.BackgroundImage = Resources.StartUpPicture; this.BackgroundImageLayout = ImageLayout.Stretch;

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

                            Zilo(svk) wrote:

                            I don't know why, it just doesn't work. Myabe I'm doing something wrong...

                            Sorry, I meant what's the effect you see right now? I don't know if working with the Resources is ok. Martin

                            S 1 Reply Last reply
                            0
                            • M Martin 0

                              Zilo(svk) wrote:

                              I don't know why, it just doesn't work. Myabe I'm doing something wrong...

                              Sorry, I meant what's the effect you see right now? I don't know if working with the Resources is ok. Martin

                              S Offline
                              S Offline
                              Stevo Z
                              wrote on last edited by
                              #14

                              Allright, it works this way Bitmap bmp = (Bitmap)Resources.StartUpPicture.Clone(); bmp.MakeTransparent(Color.FromArgb(80,80,80)); this.BackgroundImage = bmp;// Resources.StartUpPicture; this.BackgroundImageLayout = ImageLayout.Stretch; But behind the transparent image, there is still gray form background... And I need to make it transparent too, thats why I used the TransparencyKey. Anyway thanx for help

                              M 1 Reply Last reply
                              0
                              • S Stevo Z

                                Allright, it works this way Bitmap bmp = (Bitmap)Resources.StartUpPicture.Clone(); bmp.MakeTransparent(Color.FromArgb(80,80,80)); this.BackgroundImage = bmp;// Resources.StartUpPicture; this.BackgroundImageLayout = ImageLayout.Stretch; But behind the transparent image, there is still gray form background... And I need to make it transparent too, thats why I used the TransparencyKey. Anyway thanx for help

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

                                Zilo(svk) wrote:

                                But behind the transparent image, there is still gray form background... And I need to make it transparent too, thats why I used the TransparencyKey.

                                I have the same thing at my form! Don't use the TransparencyKey!

                                Zilo(svk) wrote:

                                this.BackgroundImageLayout = ImageLayout.Stretch;

                                I don't know what this does, maybe just for test reason, leave it out. All the best, Martin

                                S 1 Reply Last reply
                                0
                                • M Martin 0

                                  Zilo(svk) wrote:

                                  But behind the transparent image, there is still gray form background... And I need to make it transparent too, thats why I used the TransparencyKey.

                                  I have the same thing at my form! Don't use the TransparencyKey!

                                  Zilo(svk) wrote:

                                  this.BackgroundImageLayout = ImageLayout.Stretch;

                                  I don't know what this does, maybe just for test reason, leave it out. All the best, Martin

                                  S Offline
                                  S Offline
                                  Stevo Z
                                  wrote on last edited by
                                  #16

                                  That's for stretching the image to form's size. I can't leave it out (tried and didn't help) Anyway, why do you think Bitmap.MakeTransparent() will changed form's background to transparent? Imagine that you have black image with red triangle within, and you want to use it as triangle startup Form(with no borders, buttons etc..) . Just setting the black color on image as transparent will cause to display square Form with default background and red triangle within (instead black form with red triangle). In my point of view you have two options. You can changed the Form.Region value to display only triangle (which is not so difficult in this case), or use TransparencyKey. Image I use is almost impossible to describe in Region (many diffrent shapes) , so I have only one option left -> TransparencyKey. Or do you know any other ways to do that?

                                  M 1 Reply Last reply
                                  0
                                  • S Stevo Z

                                    That's for stretching the image to form's size. I can't leave it out (tried and didn't help) Anyway, why do you think Bitmap.MakeTransparent() will changed form's background to transparent? Imagine that you have black image with red triangle within, and you want to use it as triangle startup Form(with no borders, buttons etc..) . Just setting the black color on image as transparent will cause to display square Form with default background and red triangle within (instead black form with red triangle). In my point of view you have two options. You can changed the Form.Region value to display only triangle (which is not so difficult in this case), or use TransparencyKey. Image I use is almost impossible to describe in Region (many diffrent shapes) , so I have only one option left -> TransparencyKey. Or do you know any other ways to do that?

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

                                    What I'm having is a image which fills completely the Form. The Form also has a gray Bockground. Then I "MakeTransparent" with a special color, and at the reagon (at the rounded edge) on the bitmap, where the special color was, my Form also shows the things which are under the Form (not my gray Background). Maybe there is a propblem because you are doing the stretching after the MakeTransparent. Try the stretching before. All the best, Martin

                                    S 1 Reply Last reply
                                    0
                                    • M Martin 0

                                      What I'm having is a image which fills completely the Form. The Form also has a gray Bockground. Then I "MakeTransparent" with a special color, and at the reagon (at the rounded edge) on the bitmap, where the special color was, my Form also shows the things which are under the Form (not my gray Background). Maybe there is a propblem because you are doing the stretching after the MakeTransparent. Try the stretching before. All the best, Martin

                                      S Offline
                                      S Offline
                                      Stevo Z
                                      wrote on last edited by
                                      #18

                                      I tried to put it before or remove it. No change. I'll probobly stick with TransparencyKey, which by the way I use on one more place-> Trasparent Flash player. So I need to make it work :)

                                      1 Reply Last reply
                                      0
                                      • M Martin 0

                                        Hello, I'm using it for Forms with a BackgroundImage. This Image has rounded edges with a special color, which is not used in other regions of the Image. I don't use the TransparencyKey property at all!

                                        //I'm getting my Image from a file during runtime
                                        Bitmap bkgrnd = new Bitmap(myimagepath);
                                        Color transparentcolor = bkgrnd.GetPixel(0,0);
                                        bkgrnd.MakeTransparent(transparentcolor );
                                        this.BackgroundImage = bkgrnd;

                                        Hope it helps! All the best, Martin

                                        D Offline
                                        D Offline
                                        Delta Z
                                        wrote on last edited by
                                        #19

                                        Strange, here it doesn't work at all... I think maybe what I am seeing is actually form's BackColor. What did you set it to be (Color.Transparent throws exception)? UPDATE: Regarding the bit depth/resulution, on some PCs the transparency only works in 16 bit color (on the old Intel onboard graphics). Blasted graphics drivers :(

                                        M 1 Reply Last reply
                                        0
                                        • D Delta Z

                                          Strange, here it doesn't work at all... I think maybe what I am seeing is actually form's BackColor. What did you set it to be (Color.Transparent throws exception)? UPDATE: Regarding the bit depth/resulution, on some PCs the transparency only works in 16 bit color (on the old Intel onboard graphics). Blasted graphics drivers :(

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

                                          Hello, I'm really confused! Just made a test project. )MainForm has a Backgroundimage during DesignTime. )BackgroundImage is a bmp File designed in Paint (All Black only a Red Line in the middle) )BackgoundColor is Default "Control". )No TransparencyKey set )Following code in the Forms constructor (After InitializeComponents)

                                          		Bitmap b = this.BackgroundImage as Bitmap;
                                          		b.MakeTransparent(Color.Black);
                                          		this.BackgroundImage = b;
                                          

                                          )When I start now, I see just the Red line and the Desktop. )If I move the Form then off corse the Desktop is not refreshing in the Forms region. All the best, Martin

                                          S 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