Transparent Image
-
unfortunatly this doesn't work once you try to save the image. doesn't effect the background either...
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
-
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"
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 -
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"
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...
-
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. Hubbinsthis must be my problem... i will try this and get back :)
-
this must be my problem... i will try this and get back :)
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); -
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);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!
-
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"
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
-
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!
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 -
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
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 -
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. Hubbinsnot 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?)