How Do I... Figures

Important: This documentation applies to 2.0 Beta only. The 1.0 documentation is here

Figure 1: httpHandlers configuration element
<configuration>
  <system.web>
    <httpHandlers>
      <add verb="GET" path="AIP.ashx"
           type="DaveSexton.Web.Controls.AutoInputProtectionRequestHandler, DaveSexton.AutoInputProtection" />
    </httpHandlers>
  </system.web>
</configuration>


Figure 2: Register TagPrefix
<%@ Register Namespace="DaveSexton.Web.Controls" Assembly="DaveSexton.AutoInputProtection"
             TagPrefix="dsweb" %>


Figure 3: Basic control declaration
<dsweb:AutoInputProtectionControl runat="server" ID="aip" />


Figure 4: Configuring a default text provider and image provider
<configuration>
  <configSections>
    <section name="autoInputProtection"
             type="DaveSexton.Web.Controls.Configuration.AutoInputProtectionSection, DaveSexton.AutoInputProtection" />
  </configSections>

  <autoInputProtection defaultTextProvider="random text" defaultImageProvider="resource">
    <textProviders>
      <add name="random text"
           type="DaveSexton.Web.Controls.RandomCharactersAutoInputProtectionTextProvider, DaveSexton.AutoInputProtection"
           colors="Red,Green,Blue,Brown" fonts="Times New Roman,Arial"
           minimumFontSize="20" maximumFontSize="35" />
    </textProviders>
    <imageProviders>
      <add name="resource" 
           type="DaveSexton.Web.Controls.ResourceAutoInputProtectionImageProvider, DaveSexton.AutoInputProtection"
           minimumCharacterRotation="-15" maximumCharacterRotation="15" />
    </imageProviders>
  </autoInputProtection>

  ...
</configuration>


Figure 5: Configuring filter providers
<autoInputProtection>
  <filters>
    <add name="horizontal bars" 
         type="DaveSexton.Web.Controls.TranslucentBarsOverlayAutoInputProtectionFilterProvider, DaveSexton.AutoInputProtection"
         colors="Orange,Yellow,Fuchsia" />
    <add name="cross hatch" 
         type="DaveSexton.Web.Controls.CrossHatchAutoInputProtectionFilterProvider, DaveSexton.AutoInputProtection"
         colors="Red,Black" />
  </filters>
</autoInputProtection>


Figure 6: Custom text provider example in C#
using System;
using DaveSexton.Web.Controls;

public sealed class RandomNumbersAutoInputProtectionTextProvider : AutoInputProtectionTextProvider
{
  public override string GenerateRandomAutoInputProtectionText()
  {
    return RandomString.Create(MinimumCharacters, MaximumCharacters, 
      CharacterSets.PositiveDigits, null);
  }
}


Figure 7: Custom image provider example in C#
using System;
using System.Drawing;
using DaveSexton.Web.Controls;

public sealed class WhiteBackgroundAutoInputProtectionImageProvider
  : PartitionedAutoInputProtectionImageProvider
{
  public override AutoInputProtectionImage GenerateAutoInputProtectionImage(
    string name, Size size, AutoInputProtectionTextProvider textProvider)
  {
    return GenerateImage(size, textProvider);
  }

  public override AutoInputProtectionImage GenerateRandomAutoInputProtectionImage(
    Size size, AutoInputProtectionTextProvider textProvider)
  {
    return GenerateImage(size, textProvider);
  }
}


Figure 8: Custom filter provider example in C#
using System;
using System.Drawing;
using System.Configuration.Provider;
using DaveSexton.Web.Controls;

public sealed class BlueOverlayAutoInputProtectionFilterProvider
  : AutoInputProtectionFilterProvider
{
  public float Opacity { get; set; }	// C# 3.0 syntax

  public override bool CanPreProcess
  {
    get { return false; }
  }

  public override bool CanPostProcess
  {
    get { return true; }
  }

  public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
  {
    if (config == null)
      throw new ArgumentNullException("config");

    if (string.IsNullOrEmpty(name))
      name = "BlueOverlayAutoInputProtectionFilterProvider";

    if (string.IsNullOrEmpty(config["description"]))
    {
      config.Remove("description");
      config.Add("description", "Blue Overlay Auto-Input Protection Filter Provider");
    }

    base.Initialize(name, config);

    ProviderHelper helper = new ProviderHelper(config);

    Opacity = helper.ParseSingle("opacity", false, .15F);

    // Throw an exception if unrecognized attributes remain
    if (config.Count > 0)
    {
      string attr = config.GetKey(0);

      if (!string.IsNullOrEmpty(attr))
        throw new ProviderException("Unrecognized attribute: " + attr);
    }
  }

  public override void PreProcess(Graphics graphics, Size imageSize)
  {
    throw new NotSupportedException();
  }

  public override void PostProcess(Graphics graphics, Size imageSize)
  {
    using (Brush brush = new SolidBrush(Color.FromArgb((int)(Opacity * 255), Color.Blue)))
    {
      graphics.FillRectangle(brush, 0, 0, imageSize.Width, imageSize.Height);
    }
  }
}

Last edited Apr 2, 2008 at 12:30 PM by davedev, version 2

Comments

No comments yet.