Image Manipulation with ASP.NET 2.0
by Eric Bergman-Terrell


Listing One
.public static class BitmapUtils
{
.  // Draw semi-transparent text in the middle of the Bitmap.
  public static void AddWatermark(Bitmap Bitmap, 
          string WatermarkText, Color TextColor, int OpacityPercent,
          string FontFamily, FontStyle FontStyle, int FontSize)
  {
    int opacity = (int)((255.0f * OpacityPercent) / 100.0f);
    using (Graphics gr = Graphics.FromImage(Bitmap))
    using (Font font = new Font(FontFamily, FontSize, FontStyle, 
                      GraphicsUnit.Pixel))
    using (Brush semiTransparentBrush = new SolidBrush(
                     Color.FromArgb(opacity, TextColor)))
    {
      // Determine the size of the bitmap that will contain the text.
      SizeF size = gr.MeasureString(WatermarkText, font);

      int xMargin = (int)(Bitmap.Width - size.Width) / 2;
      int yMargin = (int)(Bitmap.Height - size.Height) / 2;
      gr.DrawString(WatermarkText, font, semiTransparentBrush, 
                    new Point(xMargin, yMargin));
    }
  }
 ...
  // Add the specified JPEG metadata tag to the Bitmap.
  public static void WriteEXIFTag(Bitmap Bitmap, int TagNumber, 
                                  string TagText)
  {
    Encoding asciiEncoding = new ASCIIEncoding();
    System.Text.Encoder encoder = asciiEncoding.GetEncoder();
    char[] tagTextChars = TagText.ToCharArray();
    int byteCount = encoder.GetByteCount(tagTextChars, 0, 
                                         tagTextChars.Length, true);
    byte[] tagTextBytes = new byte[byteCount];
    encoder.GetBytes(tagTextChars, 0, tagTextChars.Length, 
                     tagTextBytes, 0, true);
    // Cannot just instantiate a PropertyItem because the
    // PropertyItem class does not have a public constructor.
    // Grab the first property item and change its values.
    if (Bitmap.PropertyItems != null && 
        Bitmap.PropertyItems.Length > 0)
    {
      PropertyItem propertyItem = Bitmap.PropertyItems[0];
      propertyItem.Id    = TagNumber;
      propertyItem.Type  = 2;  // ASCII
      propertyItem.Len   = tagTextBytes.Length;
      propertyItem.Value = tagTextBytes;
      Bitmap.SetPropertyItem(propertyItem);
    }
  }
 ...
  // Return an encoder of the specified Mime type
  // (e.g. "image/jpeg").
  private static ImageCodecInfo GetEncoderInfo(String MimeType)
  {
      ImageCodecInfo Result = null;
      ImageCodecInfo[] Encoders = ImageCodecInfo.GetImageEncoders();
      for (int i = 0; Result == null && i < Encoders.Length; i++)
      {
          if (Encoders[i].MimeType == MimeType)
          {
              Result = Encoders[i];
          }
      }
      return Result;
  }
  // Save the Bitmap to the Stream. If it's in JPEG format, save
  // with the specified Quality level.
  private static void Save(Bitmap Bitmap, Stream Stream, 
                           ImageFormat Format, int Quality)
  {
    if (Format != ImageFormat.Jpeg)
    {
      // Save non-JPEG images without changing the Quality level.
      Bitmap.Save(Stream, Format);
    }
    else
    {
      // Adjust quality level of JPEG images.
      // Create an EncoderParameters object
      // containing the Quality level as a parameter.
      EncoderParameters encoderParams = new EncoderParameters(1);
      encoderParams.Param[0] = new EncoderParameter(
              System.Drawing.Imaging.Encoder.Quality, Quality);
      // Save the image using the JPEG encoder
      // with the specified Quality level.
      Bitmap.Save(Stream, GetEncoderInfo("image/jpeg"), 
                  encoderParams);
    }
  }
 ...
}



2


