# VBForums CodeBank > CodeBank - C# >  applying texture to a bitmap

## MrPolite

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  :Smilie: )

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:




```

//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 < 0 || 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(0, 0, 
                txtr.Width, txtr.Height), 
                ImageLockMode.ReadWrite, PixelFormat.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 * p = (byte *)(void *)Scan0;
            int nOffset = stride - txtr.Width*4;


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

            Graphics gr = Graphics.FromImage(img);
            TextureBrush myBrush = new TextureBrush(txtr);
            gr.FillRectangle (myBrush,0,0,img.Width, img.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.299F, 0.299F, 0.299F, 0, 0},
                                   new float[] {0.587F, 0.587F, 0.587F, 0, 0},
                                   new float[] {0.114F, 0.114F, 0.114F, 0, 0},
                                   new float[] {0, 0, 0, 1, 0},
                                   new float[] {0,  0,  0,  1, 0}});

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

            Graphics gr = Graphics.FromImage (img);
            // Apply the grayscale image attribute
            gr.DrawImage (img, new Rectangle(0, 0, img.Width, img.Height), 
                0, 0, img.Width, img.Height, GraphicsUnit.Pixel, 
                imageAttrib);
            gr.Dispose();
        }
    }
} 



```

----------


## PT Exorcist

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)

----------


## MrPolite

aha thanks alot
I dont really know antying about C#, I havent read a book or anyting, I just messed around with it  :Smilie: 
thanks again

----------


## MrPolite

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)

----------

