Results 1 to 5 of 5

Thread: how can I apply a texture to a bitmap?

  1. #1

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

    how can I apply a texture to a bitmap?

    I'm asking too many of these questions anyways, I can't figure out how. I couldnt find anything on the internet either
    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
    Lively Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    95
    Sorry I don't fully understand your question...

    Do you mean that you want to use a bitmap texture as a brush?

    if so look here or here

    unfortunately the examples are not in VB but the GDI+ API's are largely the same across .NET languages.

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by shutty
    Sorry I don't fully understand your question...

    Do you mean that you want to use a bitmap texture as a brush?

    if so look here or here

    unfortunately the examples are not in VB but the GDI+ API's are largely the same across .NET languages.
    no, I want to apply a texture to a bitmap, as I said I dont know how else to say it here, I'll show you an example:

    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
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I guess you are writing 'Mr.Polite Photoshop' to replace 'Adobe Photoshop' . Great! We back you all. Good Luck
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by Lunatic3
    I guess you are writing 'Mr.Polite Photoshop' to replace 'Adobe Photoshop' . Great! We back you all. Good Luck
    ok I started working on this crappy program of mine again... I thought a little about this texture thing and I figured it out.
    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:

    VB Code:
    1. ' Modifies the ORIGINAL bitmap and also returns a reference to it
    2.     Public Shared Function ApplyTexture(ByRef bmp As Bitmap, ByVal textureTransparency As Single) As Bitmap
    3.         If bmp Is Nothing Then Throw New ArgumentNullException
    4.         If textureTransparency < 0 OrElse textureTransparency > 1 Then
    5.             Throw New ArgumentOutOfRangeException("textureTransparency must be between 0 and 1.")
    6.         End If
    7.  
    8.         bmp = MakeImageGrayscale(bmp)
    9.  
    10.         Dim x, y As Integer
    11.         Dim alpha As Integer
    12.  
    13.         For x = 0 To bmp.Width - 1
    14.             For y = 0 To bmp.Height - 1
    15.                 Dim c As Color = bmp.GetPixel(x, y)
    16.                 ' c.R -> all of the RGB values are the same since the image is grayscale.
    17.                 alpha = CInt(c.R * textureTransparency)
    18.                 c = Color.FromArgb(alpha, c)
    19.  
    20.                 bmp.SetPixel(x, y, c)
    21.             Next
    22.         Next
    23.  
    24.         Return bmp
    25.     End Function
    26.  
    27.      Public Shared Function MakeImageGrayscale(ByVal bmp As Bitmap) As Bitmap
    28.             Dim imageAttrib As ImageAttributes = GetGrayscaleAttr()
    29.             Return ApplyImageAttribute(bmp, imageAttrib)
    30.         End Function
    31.  
    32.  
    33.  Public Shared Function GetGrayscaleAttr() As ImageAttributes
    34.             Dim values()() As Single = {New Single() {0.5, 0.5, 0.5, 0, 0}, _
    35.                                               New Single() {0.5, 0.5, 0.5, 0, 0}, _
    36.                                               New Single() {0.5, 0.5, 0.5, 0, 0}, _
    37.                                               New Single() {0, 0, 0, 1, 0}, _
    38.                                               New Single() {0, 0, 0, 0, 1}}
    39.             ' Use the matrix to initialize a new colorMatrix object
    40.             Dim colMatrix As New ColorMatrix(values)
    41.             Dim imageAttr As New ImageAttributes
    42.             imageAttr.SetColorMatrix(colMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
    43.  
    44.             Return imageAttr
    45.         End Function
    46.  
    47.     Public Shared Function ApplyImageAttribute(ByVal img As Bitmap, ByVal imageAttrib As ImageAttributes) As Bitmap
    48.             Dim bmp As New Bitmap(img.Width, img.Height)
    49.             Dim gr As Graphics = Graphics.FromImage(bmp)
    50.             gr.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imageAttrib)
    51.             Return bmp
    52.         End Function
    I just copy pasted my code from my project... you can combine all those a3 functions for converting to grayscale if you want


    edit: ahem, would it be more efficient if I converted the image to grayscale myself in that first forloop? or would applying an image attribute be faster?
    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