Results 1 to 10 of 10

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

Threaded View

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

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