Raster Transparency Level

Topics: General Topics, SharpMap Project, SharpMap v0.9 / v1.x
May 20, 2009 at 1:16 PM

Hi all,

objects of the type GdalRasterLayer currently offer only 'TransparentColor' to get a transparent raster image. If assigning 'Color.Transparent' the image will be set to 100% transparency (except the image's background - that remains black). What I'm missing is the possibility to assign a reduced rate of transparency.

Is that an outstanding issue or did I oversee something?

Regards, Martin

May 28, 2009 at 10:27 PM

Martin,

Transparency can be easily added.

In GdalRasterLayer.cs do the following:

Add this to the global variables:

protected float transparency = 1.0f;

 

Add this to the accessors:

public override float Transparency
        {
            get { return (int)(1 - transparency) * 100; }
            set { transparency = (float)(1.0 - ((double)value / 100.0)); }
        }

 

And replace the following in GetPreview() and GetNonRotatedPreview():

 g.DrawImage(bitmap, new Point(bitmapTL.X, bitmapTL.Y));

with

// apply transparency
float[][] pnt = new float[][]
{new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, transparency, 0},
new float[] {0, 0, 0, 0, 1}};

ColorMatrix clm = new ColorMatrix(pnt);
ImageAttributes att = new ImageAttributes();
att.SetColorMatrix(clm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

///////// for GetNonRotatedPreview()
g.DrawImage(bitmap, new Rectangle((int)dblLocX, (int)dblLocY, bitmap.Size.Width, bitmap.Size.Height),
0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, att);

///////// for GetPreview()
g.DrawImage(bitmap, new Rectangle(bitmapTL.X, bitmapTL.Y, bitmap.Size.Width, bitmap.Size.Height),
0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, att);

Then you can set GdalRasterLayer.Transparency to a value between 0 and 1 which defines the transparency amount.

Hopefully you can make sense of this.

Dan

 

Jun 2, 2009 at 8:49 AM

Hi Dan,

it's running well - thank you very much. The value has to be set as a per cent value (1 - 100).

Regards,

Martin