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. Transparent Image

Transparent Image

Scheduled Pinned Locked Moved C#
graphicsquestionlounge
13 Posts 5 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.
  • V Verdant123

    is there anyway i can generate a transparent image with system.drawing, using only safe code? all i have seen are unsafe hacks for gifs... if i try to save pngs to a stream i always get "general errors"

    I Offline
    I Offline
    ian mariano
    wrote on last edited by
    #2

    See Using a Color Matrix to Set Alpha Values in Images in the MSDN library.

    Ian Mariano - http://www.ian-space.com/
    "We are all wave equations in the information matrix of the universe" - me

    V 1 Reply Last reply
    0
    • I ian mariano

      See Using a Color Matrix to Set Alpha Values in Images in the MSDN library.

      Ian Mariano - http://www.ian-space.com/
      "We are all wave equations in the information matrix of the universe" - me

      V Offline
      V Offline
      Verdant123
      wrote on last edited by
      #3

      unfortunatly this doesn't work once you try to save the image. doesn't effect the background either...

      D 1 Reply Last reply
      0
      • V Verdant123

        unfortunatly this doesn't work once you try to save the image. doesn't effect the background either...

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #4

        How about posting some code that your trying to use so we can see what your doing. I take it your trying to draw a picture using GDI+ and then save the resulting image with a color-keyed transparent background? RageInTheMachine9532

        1 Reply Last reply
        0
        • V Verdant123

          is there anyway i can generate a transparent image with system.drawing, using only safe code? all i have seen are unsafe hacks for gifs... if i try to save pngs to a stream i always get "general errors"

          K Offline
          K Offline
          Kentamanos
          wrote on last edited by
          #5

          Not sure if you were getting the same "general error" I was (I got something equally informative), but I ran into a problem writing an HTTPHandler to convert TIF's to PNG's on the fly. I was trying to save the image to the ResponseStream directly, but I kept getting that error. I then created a MemoryStream, and I was then able to write the contents of the MemoryStream to the HTTP Response. I'm GUESSING here, but I think the stream it wants needs to be seekable, so in my case the HTTP ResponseStream didn't work. You might make sure that the stream you're trying to write the PNG is a MemoryStream and see if it fixes some of your problems.


          I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
          -David St. Hubbins

          V 1 Reply Last reply
          0
          • V Verdant123

            is there anyway i can generate a transparent image with system.drawing, using only safe code? all i have seen are unsafe hacks for gifs... if i try to save pngs to a stream i always get "general errors"

            M Offline
            M Offline
            Matthew Hazlett
            wrote on last edited by
            #6

            Bitmap img = new Bitmap(100, 100); img.MakeTransparent = color.white; img.Save("myimage.png", System.Drawing.Imaging.ImageFormat.Png); img.Dispose(); Matthew Hazlett Windows 2000/2003 MCSE Never got an MCSD, go figure...

            1 Reply Last reply
            0
            • K Kentamanos

              Not sure if you were getting the same "general error" I was (I got something equally informative), but I ran into a problem writing an HTTPHandler to convert TIF's to PNG's on the fly. I was trying to save the image to the ResponseStream directly, but I kept getting that error. I then created a MemoryStream, and I was then able to write the contents of the MemoryStream to the HTTP Response. I'm GUESSING here, but I think the stream it wants needs to be seekable, so in my case the HTTP ResponseStream didn't work. You might make sure that the stream you're trying to write the PNG is a MemoryStream and see if it fixes some of your problems.


              I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
              -David St. Hubbins

              V Offline
              V Offline
              Verdant123
              wrote on last edited by
              #7

              this must be my problem... i will try this and get back :)

              V 1 Reply Last reply
              0
              • V Verdant123

                this must be my problem... i will try this and get back :)

                V Offline
                V Offline
                Verdant123
                wrote on last edited by
                #8

                hmm, i am doing something wrong... i have not used the memorystream before... i am not getting any errors during compile, but the png is not comming out at all (redx in internet explorer and "cannot be displayed" because it contains errors in firebird) can you see what i am doing wrong?

                System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                fImage.Save(memStream,ImageFormat.Png);
                byte[] j = new Byte[memStream.Length];
                memStream.Read(j,0,(int)memStream.Length);
                ctx.Response.ContentType = "image/png";
                ctx.Response.BinaryWrite(j);

                V 1 Reply Last reply
                0
                • V Verdant123

                  hmm, i am doing something wrong... i have not used the memorystream before... i am not getting any errors during compile, but the png is not comming out at all (redx in internet explorer and "cannot be displayed" because it contains errors in firebird) can you see what i am doing wrong?

                  System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                  fImage.Save(memStream,ImageFormat.Png);
                  byte[] j = new Byte[memStream.Length];
                  memStream.Read(j,0,(int)memStream.Length);
                  ctx.Response.ContentType = "image/png";
                  ctx.Response.BinaryWrite(j);

                  V Offline
                  V Offline
                  Verdant123
                  wrote on last edited by
                  #9

                  seems to be working with:

                  System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                  fImage.Save(memStream,ImageFormat.Png);
                  ctx.Response.ContentType = "image/png";
                  ctx.Response.BinaryWrite(memStream.ToArray());

                  thanks for the help!

                  K 1 Reply Last reply
                  0
                  • V Verdant123

                    is there anyway i can generate a transparent image with system.drawing, using only safe code? all i have seen are unsafe hacks for gifs... if i try to save pngs to a stream i always get "general errors"

                    V Offline
                    V Offline
                    Verdant123
                    wrote on last edited by
                    #10

                    any way i can get my images to look a little nicer, perhaps some anti-aliasing? currently it looks like: (note transparent png doesn't seem to work in IE, firefox only) This

                    K 1 Reply Last reply
                    0
                    • V Verdant123

                      seems to be working with:

                      System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                      fImage.Save(memStream,ImageFormat.Png);
                      ctx.Response.ContentType = "image/png";
                      ctx.Response.BinaryWrite(memStream.ToArray());

                      thanks for the help!

                      K Offline
                      K Offline
                      Kentamanos
                      wrote on last edited by
                      #11

                      Good deal. Glad to see someone else stumbled across that REALLY useful error message as well :).


                      I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
                      -David St. Hubbins

                      1 Reply Last reply
                      0
                      • V Verdant123

                        any way i can get my images to look a little nicer, perhaps some anti-aliasing? currently it looks like: (note transparent png doesn't seem to work in IE, firefox only) This

                        K Offline
                        K Offline
                        Kentamanos
                        wrote on last edited by
                        #12

                        I'm not sure what type of transparency you're currently building into this PNG. If you're doing a color key where basically one color indicates 100% transparency, you can't really do something like anti-aliasing. Your image would have to know color of what it's being drawn on top of an anti-alias to that color (thus defeating the purpose of a transparent image :)). People have this problem with transparent GIF's because they only allow one color in the pallete to be transparent (100% transparent). If you can get an alpha channel going instead of a color-key (256 levels of transparency on 8 bits of alpha for instance), then you can have the edges do some anti-aliasing. I'm not exactly positive of the best way to actually anti-alias, but I can tell you it's pretty much impossible for things with only 100% or 0% transparency (without knowing what you're being drawn on top of). One easy (cheesy) way to make it look better is draw the image at 2x or 4x and then resize (with a good resizing algorithm) it to the size you want (all with an alpha channel, in other words a 32-bit image). Let us know how this turns out...


                        I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
                        -David St. Hubbins

                        V 1 Reply Last reply
                        0
                        • K Kentamanos

                          I'm not sure what type of transparency you're currently building into this PNG. If you're doing a color key where basically one color indicates 100% transparency, you can't really do something like anti-aliasing. Your image would have to know color of what it's being drawn on top of an anti-alias to that color (thus defeating the purpose of a transparent image :)). People have this problem with transparent GIF's because they only allow one color in the pallete to be transparent (100% transparent). If you can get an alpha channel going instead of a color-key (256 levels of transparency on 8 bits of alpha for instance), then you can have the edges do some anti-aliasing. I'm not exactly positive of the best way to actually anti-alias, but I can tell you it's pretty much impossible for things with only 100% or 0% transparency (without knowing what you're being drawn on top of). One easy (cheesy) way to make it look better is draw the image at 2x or 4x and then resize (with a good resizing algorithm) it to the size you want (all with an alpha channel, in other words a 32-bit image). Let us know how this turns out...


                          I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
                          -David St. Hubbins

                          V Offline
                          V Offline
                          Verdant123
                          wrote on last edited by
                          #13

                          not too sure, i am a runtime drawing newbie :) first i "Clear" the background with Graphics.Clear(Color.Transparent) then i draw a line and two strings on the graphic using Graphics.DrawString and Graphics.DrawLine i just noticed the quality of the image descreased significantly when saving as a png with transparency, instead of a jpg... i find it particularily odd that the transparent pngs that are generated are not compatible with internet explorer... i thought that the low quality pngs (8bpp) with transparency worked... yet there is not PixelFormat.8bpp that isn't indexed... and you can't draw on indexed bitmaps using Graphics the purpose of this project was just to test out the drawing features and try to make something useful... you can test it out... it takes the string of a request and parses it into values of a "fraction" which it then draws the first part is a hex colour, preceded by the pound indicator... or "p" in this instance. then the font size in pixel format then the numerator and the denominator, with a final terminating comma. Example for best results use a browser that supports transparent pngs if you want to take a look at the code Here (anyone know of a utility for automatically formating in VS.NET style, for the web/html?)

                          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