Help with MaskImage() in Windows 7

BlitzMax Forums/BlitzMax Beginners Area/Help with MaskImage() in Windows 7

PoliteProgrammer(Posted 2010) [#1]
Hi everyone. Does anyone know how to get images to mask properly in Windows 7?

I have done nothing more than created a PNG image with a magenta background in the new Microsoft Paint and saved it then tried to load it in BlitzMax with setmaskcolor(255,0,255) and loadimage(...) and it's still showing the mask colour when I draw it.


GfK(Posted 2010) [#2]
Tried SetBlend(MASKBLEND) before drawing the image?

[edit] Actually, this works fine:
Strict

Graphics 800,600
SetMaskColor(255,0,255)
Local img:TImage = LoadImage("a.png")

While Not KeyDown(key_escape)
	Cls
	DrawImage img,MouseX(),MouseY()
	Flip
Wend

Note that if you use SetMaskColor *after* LoadImage, it doesn't work.


PoliteProgrammer(Posted 2010) [#3]
That still doesn't work for me...

But strangely I've just tried it with an image that I saved some time ago under windows xp and it works fine with that one.


PoliteProgrammer(Posted 2010) [#4]
No worries, I've just downloaded the xp version of microsoft paint and used that to make my image. It works fine now. There was definitely something strange going on with the new one - perhaps something to do with the alpha channel.


GfK(Posted 2010) [#5]
That's odd, I'm on Windows 7 here.


_Skully(Posted 2010) [#6]
Function SetImageMask(p_image:TImage, p_red:Int,p_green:Int,p_blue:Int)
	Local l_maskrgb:Int
	Local l_maskargb:Int
	Local l_pixelrgb:Int
	Local l_pixelraw:Int
	Local l_x:Int,l_y:Int
	Local l_pix1:TPixmap
	
	l_maskrgb = p_red Shl 16 + p_green Shl 8 + p_blue
	l_pix1 = LockImage(p_image)
	For l_x = 0 To ImageWidth(p_image)-1
		For l_y = 0 To ImageHeight(p_image)-1
			l_pixelraw = ReadPixel(l_pix1,l_x,l_y)
			l_pixelrgb = l_pixelraw & 16777215
			If l_pixelrgb = l_maskrgb Then
				' make transparent
				WritePixel(l_pix1,l_x,l_y,l_maskrgb)
			Else
				' remove transparency
				Local l_PixelNoAlpha:Int=l_pixelrgb | (255 Shl 24)
				WritePixel(l_pix1,l_x,l_y,l_PixelNoAlpha)
			End If
		Next 
	Next
	UnlockImage(p_image)
End Function



gameproducer(Posted 2010) [#7]
I totally recommend setting transparent background in gimp instead of pinky colors. No need to hassle with masks.


DinoEntrails(Posted 2010) [#8]
I am using Win7 as well and had a similar problem a while ago, but I don't remember what I did to fix it.


William Drescher(Posted 2010) [#9]
The easiest way to do this, in my opinion, is to use a PNG image with a transparent background (or transparency where you want it to be transparent). Load the image with LoadImage then use SetBlend(ALPHABLEND) and you should be golden.


Arabia(Posted 2010) [#10]
I've been wondering about this for the last week - I was sure I worked out how to display a .png with a transparent background, but tried again the other day and it just wouldn't work.

I'm using Win 7 now (with the version of MS Paint that comes with it)

I assumed (wrongly) that a .PNG was a .PNG file. Wrong.

I downloaded the first little "MS Paint" Similar program I could find - Paint.Net. Loaded the file I created in MS Paint and resaved it.

The files:

MS Paint Version - 691 Bytes
Paint.Net Version - 1225 Bytes

MS Paint version doesn't display correctly (with SetMaskColor) but the Paint.Net version does.

I should point out - in neither program (not that I looked that hard) was their an option to specify a transparent color, I simply made red (255,0,0) the color I wanted to be the transparent area of my image.

I've had a very quick look at the format for .png files but it really is beyond my brain at this hour of the day. Someone who is a hell of a lot brighter than me can probably explain the exact reason why MS Paint doesn't save in a format which allows for transparent .png's - I would think this is a pretty common thing, but then again, MS Paint has never claimed to be a serious paint package. It's quick and easy for mucking around with, it would be nice if there was an easy way to save in a format that supported transparent .pngs :S Considering the XP version of paint didn't have this problem. Very very weird.

I'd really like to know (from a programmers point of view) why you would change the way in which you save a file (PNG in this case) between versions of the same program (MS Paint XP V MS Paint Win7)


xlsior(Posted 2010) [#11]
I'd really like to know (from a programmers point of view) why you would change the way in which you save a file (PNG in this case) between versions of the same program (MS Paint XP V MS Paint Win7)


The windows 7 version of paint is pretty much a complete re-write of the program, more than likely made by entire different people.


Jesse(Posted 2010) [#12]
maybe this will help solve the problem:
http://www.blitzmax.com/Community/posts.php?topic=92075


TomToad(Posted 2010) [#13]
MS Paint in Windows 7 saves images with alpha channel. i believe earlier versions did not. MaskImage doesn't work on images if it already has an alpha channel. I don't have access to my Win7 computer right now, so I can't check to see if there is an option to save the image without alpha.


xlsior(Posted 2010) [#14]
I don't see any way in mspaint to adjust whether or not to save the alpha.
Heck, you can't even specify the JPG compression level, which is supported by pretty much any other graphical editor on the planet.


Dabhand(Posted 2010) [#15]

Heck, you can't even specify the JPG compression level, which is supported by pretty much any other graphical editor on the planet.



Well, if they made it beyond a simple image editor, they'd probably have their arse in court in a flash and the EU would make them release a version of Windows without MSPaint included, so why bother!

Dabz

Last edited 2010


Zacho(Posted 2011) [#16]
The trick is to save it as a 16bit or anything like that (*.bmp).

it works if you do it like this


YellBellzDotCom(Posted 2011) [#17]
Wow, 16bit bmp with an alpha channel, never knew. :)

Gimp has the function to save bmp 16bit w/ alpha, 24bit without alpha,32bit w/ alpha, gotta love gimp!


I've always used .png, once I found out about the alpha channel.


Czar Flavius(Posted 2011) [#18]
I've never been able to get any ms paint to save with alpha channel. To draw the pngs don't forget setblend alphablend or it won't work.


Matt Vinyl(Posted 2011) [#19]
"I've always used .png, once I found out about the alpha channel."

This. I know nothing about the technical aspects of the .png file format, apart from it:

-Supports Alpha
-Has small filesizes
-Is pretty widespread in it's usage now.

Anyone still bother with .bmp? Out of interest...


Czar Flavius(Posted 2011) [#20]
To their defence, bitmaps are faster to load.


GfK(Posted 2011) [#21]
They're faster to load because they aren't compressed.


John G(Posted 2011) [#22]
@Matt Vinyl
Anyone still bother with .bmp? Out of interest...

I've been learning Inkscape for vector graphics. Inkscape has a "File/Export Bitmap..." option. They results are xxx.png (not .bmp). The files are really tiny but handle Alpha just fine.


xlsior(Posted 2011) [#23]
I've been learning Inkscape for vector graphics. Inkscape has a "File/Export Bitmap..." option. They results are xxx.png (not .bmp). The files are really tiny but handle Alpha just fine.


There's bitmap, and bitmap -- there is the OS/2 / Microsoft 'bitmap' fileformat with the .bmp extension, but it's also a general term to refer to rasterized images that are stored in a pixel format instead of vectors.

Therefore, a .bmp is not a png or GIF or JPEG, but png/gif/jpeg images ARE bitmaps.


Nexinarus(Posted 2015) [#24]
@ Polite Programmer
"Hi everyone. Does anyone know how to get images to mask properly in Windows 7?

I have done nothing more than created a PNG image with a magenta background in the new Microsoft Paint and saved it then tried to load it in BlitzMax with setmaskcolor(255,0,255) and loadimage(...) and it's still showing the mask colour when I draw it."

i dont code in blitz max. I code in blitz3d. But this is what i do. I just leave the maskimage color at 0,0,0. If i want black, I go to the next best thing. 8,8,8. That is what I do. It may not be perfect black but at least it works. It works for me in blitz3d that is.


xlsior(Posted 2015) [#25]
...you resurrected a 5-year old thread - he probably figured it out by now. :-?