Results 1 to 10 of 10

Thread: VB.NET - UPDATED - applying texture to a bitmap

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    VB.NET - UPDATED - applying texture to a bitmap

    Use this code to apply a texture to a bitmap.


    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:

    The C# version is here

    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:
    1. 'written by Kourosh Derakshan (MrPolite @ vbforums.com)
    2. Imports System.Drawing.Imaging
    3.  
    4. Namespace MrPolite
    5.     Public NotInheritable Class BitmapTexture
    6.  
    7.         ' Please do not remove
    8.         ' Written by Kourosh Derakshan
    9.         '
    10.  
    11.         ' Modifies the ORIGINAL bitmap
    12.         ' textureTransparency  has to be between 0 and 1,
    13.         ' with 0 being the least transparent, and 1 the most transparent
    14.         Public Shared Sub ApplyTexture(ByVal bmp As Bitmap, _
    15.             ByVal texture As Bitmap, _
    16.             ByVal textureTransparency As Single)
    17.  
    18.             If (bmp Is Nothing) OrElse (texture Is Nothing) Then Throw New ArgumentNullException
    19.             If textureTransparency < 0 OrElse textureTransparency > 1 Then
    20.                 Throw New ArgumentOutOfRangeException("textureTransparency must be between 0 and 1.")
    21.             End If
    22.  
    23.             ' Convert the texture to grayscale before using it
    24.             MakeImageGrayscale(texture)
    25.  
    26.  
    27.             Dim x, y, alpha As Integer
    28.  
    29.             ' Adjust the alpha value of each pixel in the texture bitmap.
    30.             ' The white-most pixels will have the the most transparency (highest alpha),
    31.             ' and the dark-most pixels will have the least transparency.
    32.             For x = 0 To texture.Width - 1
    33.                 For y = 0 To texture.Height - 1
    34.                     Dim c As Color = texture.GetPixel(x, y)
    35.                     ' c.R -> all of the RGB values are the same since the image is in grayscale
    36.                     alpha = CInt(c.R * textureTransparency)
    37.                     c = Color.FromArgb(alpha, c)
    38.  
    39.                     texture.SetPixel(x, y, c)
    40.                 Next
    41.             Next
    42.  
    43.             Dim gr As Graphics = Graphics.FromImage(bmp)
    44.             Dim myBrush As New TextureBrush(texture)
    45.  
    46.             ' Draw the texture over the original bitmap
    47.             gr.FillRectangle(myBrush, bmp.GetBounds(GraphicsUnit.Pixel))
    48.             myBrush.Dispose()
    49.             gr.Dispose()
    50.         End Sub
    51.  
    52.         ' Converts the image to grayscale
    53.         Private Shared Sub MakeImageGrayscale(ByVal bmp As Bitmap)
    54.             Dim cMatrix As New ColorMatrix(New Single()() _
    55.              {New Single() {0.299, 0.299, 0.299, 0, 0}, _
    56.              New Single() {0.587, 0.587, 0.587, 0, 0}, _
    57.               New Single() {0.114, 0.114, 0.114, 0, 0}, _
    58.               New Single() {0, 0, 0, 1, 0}, _
    59.               New Single() {0, 0, 0, 0, 1}})
    60.  
    61.             Dim imageAttrib As New ImageAttributes
    62.             imageAttrib.SetColorMatrix(cMatrix)
    63.  
    64.  
    65.             Dim gr As Graphics = Graphics.FromImage(bmp)
    66.             ' Apply the grayscale image attribute
    67.             gr.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height), _
    68.               0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttrib)
    69.             gr.Dispose()
    70.         End Sub
    71.  
    72.     End Class
    73. End Namespace
    Attached Files Attached Files
    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!!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Hey MrPolite What the hell is that thing in the picture? Its like a cat-monkey or something.

    Good job though.

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: VB.NET - UPDATED - applying texture to a bitmap

    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?

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: VB.NET - UPDATED - applying texture to a bitmap

    Quote 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!!

  6. #6
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    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

  7. #7

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: VB.NET - UPDATED - applying texture to a bitmap

    I doubt this is what you're asking, but if it is:
    VB Code:
    1. Dim img As Image = Image.FromFile("C:\test.jpg")
    2.         MakeImageGrayscale(img)
    3.         img.Save("C:\test_gray.jpg")
    4.         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!!

  8. #8

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    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!!

  9. #9
    Member
    Join Date
    Oct 2005
    Posts
    43

    Thumbs up Re: VB.NET - UPDATED - applying texture to a bitmap

    Dear Mr Polite,

    Can Your texture to Bitmap be used for JPG?
    It would be nice, because a JPG is more common and smaller.
    I could use some could code.

    P.

  10. #10

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    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!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width