imageComponent.NET - image processing .NET assemblies
Feather Image in .NET

This demo will show you how to do image feathering with PhotoController. Due to the constraint of my new web host provider, I am no longer able to provide a live demo. But feel free to download all the demo and their source code.

The following sample code shows how to apply feather filter to a given image:

   1:  Response.Clear();
   2:  Response.Expires = 0;
   3:  Response.ContentType = "image/jpeg";
   4:   
   5:  Bitmap canvas = new Bitmap(Server.MapPath("..\\images\\turtle.jpg"));
   6:   
   7:  string mask;
   8:  string backcolor;
   9:  int band = 10;
  10:   
  11:  if (Request.QueryString.Count > 0)
  12:  {
  13:      mask = Request.QueryString["mask"];
  14:      if (!int.TryParse(Request.QueryString["band"], out band))
  15:          band = 10;
  16:      backcolor = Request.QueryString["backcolor"];
  17:  }
  18:  else
  19:  {
  20:      mask = "none";
  21:      backcolor = "Black";
  22:  }
  23:   
  24:   
  25:  Controller pc = new Controller(canvas);
  26:  Bitmap result;
  27:   
  28:  FeatherBuilder fb = new FeatherBuilder(canvas.Width, canvas.Height);
  29:  int width =canvas.Width;
  30:  int height = canvas.Height ;
  31:  fb.BackgroundColor = Color.FromName(backcolor);
  32:   
  33:  // band defines the feather level. 
  34:  fb.Band = band;
  35:   
  36:  // this rectangle defines an area you want to feather the image. It doesn't have to be related to the band value.
  37:  // I just use it in here so I don't need to add one more control to speicify the area. (my laziness)
  38:  Rectangle rect = new Rectangle(band + 20, band + 20, width - (band + 20) * 2, height - (band + 20) * 2);
  39:   
  40:  switch (mask)
  41:  {
  42:      case "Rectangle":
  43:          fb.BuildRectangleMask(rect);
  44:          result = new Bitmap(pc.Feather(fb)); 
  45:          break;
  46:      case "Ellipse":
  47:          fb.BuildEllipseMask(rect);
  48:          result = new Bitmap(pc.Feather(fb)); 
  49:          break;
  50:      case "Polygon":
  51:          int band2 = band * 2;
  52:          // this polygon is a circle, it starts from the top-left point (band2, band2), then go to
  53:          // the top-right point (width-band2, band2), next point will be bottom-right, the last 
  54:          // point is bottom-left. it has 4 points, you can see it is a clockwise circle
  55:          fb.BuildPolygonMask(new Point[]{ new Point(band2, band2), new Point(width-band2, band2), 
  56:              new Point(width-band2, height-band2), new Point(band2, height-band2)});
  57:          result = new Bitmap(pc.Feather(fb)); 
  58:          break;
  59:      default:
  60:          result = new Bitmap(canvas);
  61:          break;
  62:  }
  63:  canvas.Dispose();
  64:  canvas = null;
  65:  pc.Dispose();
  66:  pc = null;
  67:   
  68:  MemoryStream oStream = new MemoryStream();
  69:  result.Save(oStream, ImageFormat.Jpeg);
  70:  Response.BinaryWrite(oStream.ToArray());
  71:   
  72:  result.Dispose();
  73:  result = null;
  74:  Response.End();

[Back to Top]
Comments

If you feel this page can be improved or has any mistake, please enter your valuable opinion here. Or if you cannot find the information you are after, feel free to suggest a subject too. I appreciate any voice.

Your Name:
  
Your Email Address:
  
Comments: