Make background of graphics object transparent.

K

Kalvin

I keep getting my string inside a black box with this code.
I would like to make the background transparent. Any help on this will
be appreciated very much.

'**** BEGIN CODE ****
Try

Dim NewFont As New Font(fg.Font.FontFamily, 10, FontStyle.Regular)

Dim g As Graphics

Dim pic As Bitmap = New Bitmap(20, 20,
imaging.PixelFormat.Format24bppRgb)

g = Graphics.FromImage(pic)

g.Clear(Color.Transparent)

g.DrawString("H", NewFont, Brushes.Blue, 0, 0)


Dim st As New System.IO.MemoryStream

pic.Save(st, System.Drawing.Imaging.ImageFormat.Bmp)
Dim img As Image = Image.FromStream(st)
pct.Image = img

Catch exc As Exception
Stop

End Try
'**** END CODE ****

Kalvin
 
K

Ken Tucker [MVP]

Hi,

Try something like this.

Try

Dim g As Graphics

Dim pic As Bitmap = New Bitmap(20, 20, Imaging.PixelFormat.Format24bppRgb)

g = Graphics.FromImage(pic)

g.Clear(Color.Yellow)

g.DrawString("H", Me.Font, Brushes.Blue, 0, 0)

pic.MakeTransparent(Color.Yellow)

'Dim st As New System.IO.MemoryStream

'pic.Save(st, System.Drawing.Imaging.ImageFormat.Bmp)

'Dim img As Image = Image.FromStream(st)

PictureBox1.Image = pic

Catch exc As Exception

Stop

End Try



Ken

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

I keep getting my string inside a black box with this code.
I would like to make the background transparent. Any help on this will
be appreciated very much.

'**** BEGIN CODE ****
Try

Dim NewFont As New Font(fg.Font.FontFamily, 10, FontStyle.Regular)

Dim g As Graphics

Dim pic As Bitmap = New Bitmap(20, 20,
imaging.PixelFormat.Format24bppRgb)

g = Graphics.FromImage(pic)

g.Clear(Color.Transparent)

g.DrawString("H", NewFont, Brushes.Blue, 0, 0)


Dim st As New System.IO.MemoryStream

pic.Save(st, System.Drawing.Imaging.ImageFormat.Bmp)
Dim img As Image = Image.FromStream(st)
pct.Image = img

Catch exc As Exception
Stop

End Try
'**** END CODE ****

Kalvin
 
K

Kalvin

Thank you very much for your reply. I did try that, but what I wind up
with is still a black background. When I use the
pic.clear(color.yellow) line without the
pic.MakeTransparent(Color.Yellow) line, I get a yellow background, as
soon as I add the maketransparent call execute, I get the black
background again. I did find that if I don't save it to the
memoryStream, then the background is transparent. Is there something
with the memory stream that is going to color the background?

Kalvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top