ThinkGeo.com    |     Documentation    |     Premium Support

Transparent Background

Hello,


My application is loading Tiles from a Wms Server, and they are displayed as Background of my map. The Wms Server sends PNG files.

Is it possible to render client side those tiles with some kind of transparency, so that the features I display over the map will be more visible ?


Thanks,

Guillaume.



Hi Guillaume, 
  
 We cannot handle much on the 3rd part WMS server. That all depends on whether the server supports transparency background. You can check its capability or you can send us your WMS server address and we can check for you. 
  
 Thanks, 
 Howard

Hi, 
  
 I finally found a solution by overriding the WmsOverlay and the UriTile classes, and by setting image opacity client side. 
 Here is the code if someone is interested in : 
  
 
public class CustomWmsUriTile : UriTile
    {
        private const float OPACITY = 1.0f;

        protected override Bitmap GetBitmapCore(UriTileArguments args)
        {
            HttpWebResponse response = null;
            Bitmap bitmap = null;

            try
            {
                var webRequest = (HttpWebRequest)WebRequest.Create(args.Uri.ToString());
                request.Proxy = args.WebProxy;

                response = (HttpWebResponse)request.GetResponse();
                bitmap = new Bitmap(response.GetResponseStream());

                return SetImgOpacity(bitmap, OPACITY);
            }
            finally
            {
                if (response != null) { response.Close(); }
            }
        }


        public static Bitmap SetImgOpacity(Bitmap bitmap, double opacity)
        {
            // If opacity is 1, we have nothing to do
            if (opacity == 1)
                return bitmap;

            var newBitmap = new Bitmap(bitmap.Width, bitmap.Height);

            using (Graphics graphics = Graphics.FromImage(newBitmap))
            {
                graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height);
                var matrix = new ColorMatrix();
                matrix.Matrix33 = (float) opacity;

                var attributes = new ImageAttributes();
                attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                graphics.DrawImage(bitmap, new Rectangle(0, 0, newBitmap.Width, newBitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, attributes);
            }

            return newBitmap;
        }
    }
 


Hi Guillaume, 
  
 Very glad to know your problem is solved. 
 And thanks to your contribution to this community. 
  
 Regards, 
 Tsui

We have a problem with Transparency in WMS layer. Using ThinkGeo v12+ here. WmsRasterLayer has a transparency Property and it works as expected, but the WmsOverlay does not have the property, I also could not get an overridable method to apply transparency in Bitmap.
Need urgent help.
Regards,
Syed

Hey @IT1,

Overlays are a bit different to work with compared to Layers since they are much closer to the underlying UI controls. Instead, you need to set the Opacity property on the OverlayCanvas object:

wmsOverlay.OverlayCanvas.Opacity = 0.5;  // 50% transparent

Thanks,
Kyle