here's what I do: I convert the texture to grayscale. The whitemost pixels (highest r, g, or b value) will have the most transparency. The darkmost pixels will be the least transparent pixels when you are applying the texture:
edit: if speed is an issue, you can rewrite the code without using GetPixel and SetPixel. These two functions are very slow because with every call, the whole bitmap needs to be locked and unlocked in the memory. I'm not going to do this, but if you search the net you'll find out how to use these two: replace getpixel and setpixel with calls to Marshal.WriteInt32() and Marshal.ReadInt32(). You would need to lock and unlock the bitmap before doing so!
VB Code:
'written by Kourosh Derakshan (MrPolite @ vbforums.com)
Imports System.Drawing.Imaging
Namespace MrPolite
Public NotInheritable Class BitmapTexture
' Please do not remove
' Written by Kourosh Derakshan
'
' Modifies the ORIGINAL bitmap
' textureTransparency has to be between 0 and 1,
' with 0 being the least transparent, and 1 the most transparent
Public Shared Sub ApplyTexture(ByVal bmp As Bitmap, _
ByVal texture As Bitmap, _
ByVal textureTransparency As Single)
If (bmp Is Nothing) OrElse (texture Is Nothing) Then Throw New ArgumentNullException
If textureTransparency < 0 OrElse textureTransparency > 1 Then
Throw New ArgumentOutOfRangeException("textureTransparency must be between 0 and 1.")
End If
' Convert the texture to grayscale before using it
MakeImageGrayscale(texture)
Dim x, y, alpha As Integer
' Adjust the alpha value of each pixel in the texture bitmap.
' The white-most pixels will have the the most transparency (highest alpha),
' and the dark-most pixels will have the least transparency.
For x = 0 To texture.Width - 1
For y = 0 To texture.Height - 1
Dim c As Color = texture.GetPixel(x, y)
' c.R -> all of the RGB values are the same since the image is in grayscale
Last edited by MrPolite; Sep 2nd, 2005 at 04:24 PM.
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB ) VB.NET to C# conversion tips!!
Originally posted by Edneeis Hey MrPolite What the hell is that thing in the picture? Its like a cat-monkey or something.
Good job though.
oh yeah isnt that beautiful? I just walked in a petshop with that picture yesterday, asking if they had that pet or not. They said it's not sold in this galaxy, oh well
hehe I got that from chitchatforums, some guy posted it like a year ago
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB ) VB.NET to C# conversion tips!!
Re: VB.NET - UPDATED - applying texture to a bitmap
Originally Posted by wossname
I like the MakeImageGreyscale() sub. Very neat.
MSDN seems to have very little to say on the subject of ColorMatrixes, do you have any links about it?
you mean creature, if you really liked it then you'd give me a rep point all your rep points are belong to me!
umm to be honest I dont see why MSDN would provide anything on this. Color matrix relates more to mathematics, so ...
HOWEVER, I wrote a lil nonsentual comment for one of my libraries, and I just copy paste it here. Looking at it, it's a bit vague because I was probably half sleep back then when I was writing it but if you read it, I gave some examples so it should make sense what each thing in the color matrix means
PHP Code:
/*
* ColorMatrix Explanation:
* 1 0 0 0 0
* 0 1 0 0 0
* 0 0 1 0 0
* 0 0 0 1 0
* .37 .15 .07 0 1
*
* r = (r*1) + (g*0) + (b*0) + (a*0) + (1*.37) = r + .37
* g = (r*0) + (g*1) + (b*0) + (a*0) + (1*.15) = g + .15
* b = (r*0) + (g*0) + (b*1) + (a*0) + (1*.07) = b + .07
* a = (r*0) + (g*0) + (b*0) + (a*1) + (1*0) = a
*
* Max value for each r,g,b,a value is 1 which means 255. The 5th column is useless except for the last row (5,5).
* Last row is for translation
* Another explanation:
* RED rR gR bR aR 0
* GREEN rG gG bG aG 0
* BLUE rB gB bB aB 0
* ALPHA rA gA bA aA 0
* TRANSFORM fR fG fB fA F
*
* This will convert (R, G, B, A) to:
* R = (R*rR) + (G*gR) + (B*bR) + (A*aR) + (F*fR)
* G = (R*rG) + (G*gG) + (B*bG) + (A*aG) + (F*fG)
* B = (R*rB) + (G*gB) + (B*bB) + (A*aB) + (F*fB)
* A = (R*rA) + (G*gA) + (B*bA) + (A*aA) + (F*fB)
*/
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB ) VB.NET to C# conversion tips!!
Re: VB.NET - UPDATED - applying texture to a bitmap
How the heck do you save the gray-scaled image?
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz' save a blobFileStreamDataTable To Text Filemy blog
Re: VB.NET - UPDATED - applying texture to a bitmap
I doubt this is what you're asking, but if it is:
VB Code:
Dim img As Image = Image.FromFile("C:\test.jpg")
MakeImageGrayscale(img)
img.Save("C:\test_gray.jpg")
img.Dispose()
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB ) VB.NET to C# conversion tips!!
Re: VB.NET - UPDATED - applying texture to a bitmap
ok someone emailed me saying they had a problem
I looked back at my ugly code and sure enough there are some problems
the function won't work if the texture of the image is an indexed image format (GDI+ doesnt work with these). Also, the function needs to change the alpha values of the texture, so unless the imageformat of the texture supports this, it won't work
the C# version is a bit neater look at that one.
I've updated the code a bit, and provided a sample project
AND, no this isn't such a neat way of texturizing a bitmap. it's just my old experimental way
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB ) VB.NET to C# conversion tips!!
Re: VB.NET - UPDATED - applying texture to a bitmap
yea ofcourse. When i said bitmap i dont know what I was thinking You can load any valid .NET image an then apply the code.
Dim bmp as new Bitmap ("myJpgFile.jpg") then use that
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB ) VB.NET to C# conversion tips!!