Results 1 to 4 of 4

Thread: applying texture to a bitmap

  1. #1

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

    applying texture to a bitmap


    I oroginally wrote this in VB.NET ( in this post ).... this is the C# version, but is much faster since I access the bitmap with pointers...

    Use this code to apply a texture to a bitmap. (if you dunno what I mean, scroll down and see the image )

    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:


    PHP Code:
    //written by Kourosh Derakshan (MrPolite @ vbforums.com)
    using System;
    using System.Drawing.Imaging;
    using System.Drawing;

    namespace 
    MrPolite
    {
        
    // Your project needs to be able to execute unsafe code:
        // Right click on your project, go to Properties, click on Configuration Properties
        // in the Build section, make "Allow unsafe code blocks" to true
        // written by Kourosh Derakshan (MrPolite @ vbforums.com)
        
    public unsafe abstract class BitmapTxtr
        
    {
            
    /// <summary>
            /// Applies the given texture to the bitmap
            /// </summary>
            /// <param name="img">Image to be texturized</param>
            /// <param name="texture">Texture image. This will be turned to grayscale in the process.
            /// Make sure the texture isnt of an indexed image format.</param>
            /// <param name="textureTransparency">Tarnsparency value of the texture, between 0 and 1</param>
            
    public static void ApplyTexture (
                
    Image img,
                
    Image texture
                
    float textureTransparency)
            {
                if ( (
    img ==null) || (texture==null) ) 
                    throw new 
    ArgumentNullException();
                if (
    textureTransparency || textureTransparency 1
                    throw new 
    ArgumentOutOfRangeException(
                        
    "Value must be between 0 and 1.");

                
    // Convert the texture to grayscale before using it
                
    MakeImageGrayscale (texture);


                
    Bitmap txtr = new Bitmap(texture);
                
    BitmapData bmData txtr.LockBits(new Rectangle(00
                    
    txtr.Widthtxtr.Height), 
                    
    ImageLockMode.ReadWritePixelFormat.Format32bppArgb); 
                
    int stride bmData.Stride
                
    System.IntPtr Scan0 bmData.Scan0
                
    // 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.
                
    byte = (byte *)(void *)Scan0;
                
    int nOffset stride txtr.Width*4;


                for (
    int y=0txtr.Height; ++y)
                {
                    for (
    int x=0txtr.Width; ++x)
                    {
                        
    // p[3] is alpha  -  array is BGRA (blue, green , 
                        // red, alpha)
                        
    p[3] = (byte)(*textureTransparency);
                        
    += 4// Move to the next pixel
                    
    }
                    
    += nOffset;
                }
                
    txtr.UnlockBits(bmData);

                
    Graphics gr Graphics.FromImage(img);
                
    TextureBrush myBrush = new TextureBrush(txtr);
                
    gr.FillRectangle (myBrush,0,0,img.Widthimg.Height);
                
    myBrush.Dispose();
                
    gr.Dispose();
                
    txtr.Dispose();
            }



            
    /// <summary>
            ///  Converts the image to grayscale
            /// </summary>
            
    private static void MakeImageGrayscale (Image img)
            {
                
    ColorMatrix cMatrix = new ColorMatrix 
                    new 
    float[][]  { 
                                       new 
    float[] {0.299F0.299F0.299F00},
                                       new 
    float[] {0.587F0.587F0.587F00},
                                       new 
    float[] {0.114F0.114F0.114F00},
                                       new 
    float[] {00010},
                                       new 
    float[] {0,  0,  0,  10}});

                
    ImageAttributes imageAttrib = new ImageAttributes();
                
    imageAttrib.SetColorMatrix(cMatrix);

                
    Graphics gr Graphics.FromImage (img);
                
    // Apply the grayscale image attribute
                
    gr.DrawImage (img, new Rectangle(00img.Widthimg.Height), 
                    
    00img.Widthimg.HeightGraphicsUnit.Pixel
                    
    imageAttrib);
                
    gr.Dispose();
            }
        }

    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by MrPolite; Sep 2nd, 2005 at 04:15 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
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmmmmmm as a side note you can get even better speed if u instead of indexing points you do pointer arithmetics so:
    instead of
    ptr[1] you do *(++ptr)
    \m/\m/

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    aha thanks alot
    I dont really know antying about C#, I havent read a book or anyting, I just messed around with it
    thanks again
    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

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

    Re: applying texture to a bitmap

    I just added a sample project
    note that the texture parameter is modified in the process (turned into grayscale), and that also it cannot be of an indexed image format (because GDI+ doesnt support this)
    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