Save image to .PNG with transparent background?

  • Thread starter Thread starter Martin
  • Start date Start date
M

Martin

I'm using the code show below (in VB.Net 2003) to create an image. I
want to save this image to a .PNG file such that when I subsequently
display it as part of a web page, the background of the image will be
transparent.

This test/learning code simply draws a diagonal green line across a
square box. When I display the resulting file on a web page, I want
only the line itself to be visible but what is happening is that the
entire "box" is shown (in a light grey color) with the line drawn
across it.

Any suggestions as to how I can do this?

Thanks.

-----------------------------------------------------------
Dim myPen As Pen = New Pen(Color.Green, 9))
Dim bmp As Bitmap = New Bitmap(100, 100)
Dim g As Graphics = Graphics.FromImage(bmp)

g.Clear(Color.Plum)
g.DrawLine(myPen, 0, 0, 100, 100)
bmp.MakeTransparent(Color.Plum)
bmp.Save("TestFile.png", System.Drawing.Imaging.ImageFormat.Png)
 
Hi,

Try this instead.

Dim myPen As Pen = New Pen(Color.Green, 9)

Dim bmp As Bitmap = New Bitmap(100, 100)

Dim g As Graphics = Graphics.FromImage(bmp)

g.Clear(Color.Transparent)

g.DrawLine(myPen, 0, 0, 100, 100)

bmp.Save("TestFile.png", System.Drawing.Imaging.ImageFormat.Png)



Ken

------------------------

I'm using the code show below (in VB.Net 2003) to create an image. I
want to save this image to a .PNG file such that when I subsequently
display it as part of a web page, the background of the image will be
transparent.

This test/learning code simply draws a diagonal green line across a
square box. When I display the resulting file on a web page, I want
only the line itself to be visible but what is happening is that the
entire "box" is shown (in a light grey color) with the line drawn
across it.

Any suggestions as to how I can do this?

Thanks.

-----------------------------------------------------------
Dim myPen As Pen = New Pen(Color.Green, 9))
Dim bmp As Bitmap = New Bitmap(100, 100)
Dim g As Graphics = Graphics.FromImage(bmp)

g.Clear(Color.Plum)
g.DrawLine(myPen, 0, 0, 100, 100)
bmp.MakeTransparent(Color.Plum)
bmp.Save("TestFile.png", System.Drawing.Imaging.ImageFormat.Png)
 
Thanks, Ken, but that doesn't seem to do anything different. The
resulting .PNG image still appears on the web page as a light-grey
box.
 
Martin said:
Thanks, Ken, but that doesn't seem to do anything different. The
resulting .PNG image still appears on the web page as a light-grey
box.

Notice that MSIE doesn't fully support transparent PNGs! Did you try to
open the file in explorer?
 
Martin said:
Thanks, Ken, but that doesn't seem to do anything different. The
resulting .PNG image still appears on the web page as a light-grey
box.

Internet Explorer doesn't support transparent PNGs, as far as I
can tell from the Web. So if your testing it using IE, you won't be
seeing what you should be.
 
Yes, I am using Internet Explorer. And, I wasn't aware that it had a
problem with transparent PNGs.

This is a big problem for me. I need to display these graphics (with
transparent backgrounds) in Internet Explorer. Do you have any
suggestions as to how I might accomplish that?

How about .GIF files - do you know if they would display correctly?

Any suggestions?

A bit of background: I currently have a series of web pages (that are
used only on an Intranet) that display some simple, created-on-the-fly
images. I'm doing this now by using VML (Vector Markup Language). I
have server-side scripts that create the VML statements as part of the
web page itself. The big problem is that VML is supported ONLY in
Internet Explorer (and it's a dead technology).

I'm experimenting with using some individual images and sending them
out as part of the web pages. My thinking is that such would be
viewable in ANY browser.
 
Well, I just tried it as a .gif file - that doesn't work either. The
image has a black background. :(
 
Martin said:
How about .GIF files - do you know if they would display correctly?


GIF (and maybe transparent PNG8) support of MSIE is IMO better.
 
Back
Top