Transparent background for a bmp in a picture box

rodneyc8063

New member
Joined
Nov 19, 2006
Messages
4
Programming Experience
Beginner
I recently started to dabble in a bit of game programming for VB.net and am having a lot of problems with making the background of a picture transparent.

I have a picture of "mario" with a neon green background. Im drawing him to a "rectangle" and am attempting to make his green background transparent.

I have

Dim g as Graphics
Dim imageattributes as new system.drawing.imaging.imageattributes

In start up form

g=piconscreen.creategraphics

I want everything to be drawn to "g"

Then i actually draw mario to g

Dim mariorectangle as rectangle

mariorectangle.x=300
mariorectangle.y=300
mariorectangle.width=34
mariorectangle.height=38

g.drawimage(picmario.image, mariorectangle, 0, 0, mario.width, mario.height, system.drawing.graphicsunit.pixel, imageattributes)


Ok so at this point i have drawn the mario image to a rectangle on "g". Now i want to get rid of the green background he has so i tried to use the following code in the start up form:

Dim lowercolor as color=color.fromArgb(255, 0, 255)
Dim uppercolor as color=color.fromArgb(255, 0, 255)

imageattributes.SetColorKey(lowercolor, uppercolor, System.Drawing.Imaging.ColorAdjustType.Default)


Here, i thought i got rid of the any greens contained within the mario picture with the 0. But im still getting his background, in fact when i tried to fiddle with any of the RGB and set it all to 0, 0, 0 the picture doesnt change at all.

Any help as to how to make a specific color transparent would be greatly appreciated!!! :confused:
 
Set imageattributes before you use them. Also, CreateGraphics is not used for drawing, the Paint event will erase it. You can draw through the Paint Event and use Refresh/Invalidate elsewhere in code when you want repaint.
 
mario.jpg


Thats a picture of the behaviour im getting now also.

I thought that I had already set the imageattributes in "start up form load" with the following code:

Dim lowercolor as color=color.fromArgb(255, 0, 255)
Dim uppercolor as color=color.fromArgb(255, 0, 255)

imageattributes.SetColorKey(lowercolor, uppercolor, System.Drawing.Imaging.ColorAdjustType.Default)

Shouldnt that set the imageattributes when the form first loads, and also take out any greens with the "0"?
 
Perhaps, it's very confusing where the code stubs of yours are placed. Also, the green color you want transparent in that image is RGB 0,255,1
 
lol troubleshooting someone elses code is always tough but thanks for the help :eek:

Ok, so if the imageattributes are set right then it should make it transparent right?
 
That's what SetColorKey does.
 
I find this very very odd, i just tried to set the RGB to the numbers you told me to but it doesnt do anything. I have been trying to play around with the numbers a few times, setting them to 0,0,0 up to 255,255,255 and it doesnt seem to affect or do anything to the pictures colors. I just dont get how it cant affect the colors at all no matter what settings im putting into it.


Dim lowercolor as color=color.fromArgb(0, 255, 1)
Dim uppercolor as color=color.fromArgb(0, 255, 1)

imageattributes.SetColorKey(lowercolor, uppercolor, System.Drawing.Imaging.ColorAdjustType.Default)
 
Works just fine here with this code:
VB.NET:
Private Sub Panel3_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Panel3.Paint
    Dim bmp As New Bitmap("mario.bmp")
    Dim tcol As Color = bmp.GetPixel(0, 0)
    Dim ia As New System.Drawing.Imaging.ImageAttributes
    ia.SetColorKey(tcol, tcol)
    Dim rct As Rectangle = Rectangle.Round(bmp.GetBounds(Drawing.GraphicsUnit.Pixel))
    rct.Offset(50, 50)
    e.Graphics.Clear(Color.Azure)
    e.Graphics.DrawImage(bmp, rct, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia)
End Sub
Using the attached image cropped from your sample.
 

Attachments

  • mario.bmp
    2.8 KB · Views: 40
The setcolorkey method is designed to be used with the painteventargs graphics object you may get unpredictable results any other way. The bitmap class has a maketransparent method which makes any particular color transparent. There is also a setremaptable method designed to be used with the colorremaptable object (if i remember correctly) which can be used to make colors transparent, and you can also use a colormatrix to adjust the alpha component of pixles in an image. There are a number of ways to skin this cat.
 
Back
Top