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 < 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();
}
}
}
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!!
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!!
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!!