Crest Christopher wrote:
> I've always wanted to add a watermark to images automatically when > uploaded to the server for display on the page. Can you customize the > watermark ? Of course : see the code fragment below. > My only problem is I have to check if I can support IIS, > which is faster IIS or PHP ? Absolutely no idea, Christopher, but my code works only on IIS. Philip Taylor -------- <script language="C#" runat="server"> public bool ThumbnailCallback () {return false;} public void Page_Load (object sender, EventArgs e) { int max = 30; int min = 1; string imageURL = Request.QueryString ["img"]; int imageMax = Convert.ToInt32 (Request.QueryString ["max"]); int xOffset = 0, imageWidth = 0, imageHeight = 0; imageURL = "D:\\Web\\Sites\\Org\\For-Charity\\Photos\\Resources\\Images\\Private\\" + imageURL; System.Drawing.Image fullSizeImg = null; fullSizeImg = System.Drawing.Image.FromFile (imageURL); int CurrentimgHeight = fullSizeImg.Height; int CurrentimgWidth = fullSizeImg.Width; if (CurrentimgHeight > CurrentimgWidth) { imageHeight = imageMax; imageWidth = Convert.ToInt32 ( (1.0 * CurrentimgWidth) / (1.0 * CurrentimgHeight) * imageHeight); } else { imageWidth = imageMax; imageHeight = Convert.ToInt32 ( (1.0 * CurrentimgHeight) / (1.0 * CurrentimgWidth) * imageWidth); }; Response.ContentType = "image/jpeg"; System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = null; dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort (ThumbnailCallback); System.Drawing.Image thumbNailImg = null; thumbNailImg = fullSizeImg.GetThumbnailImage (imageWidth, imageHeight, dummyCallBack, IntPtr.Zero); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (thumbNailImg); SizeF StringSizeF = default (SizeF); float DesiredWidth = 0; Font wmFont = default (Font); float RequiredFontSize = 0; float Ratio = 0; String strWatermark = "Copyright © 2010"; String strWatermarkowner = Request.QueryString ["wmo"]; float xdelta = 0; float ydelta = 0; wmFont = new Font ("Arial", 24, FontStyle.Bold); DesiredWidth = Convert.ToInt32 (imageWidth * 0.85); xOffset = Convert.ToInt32 ( (imageWidth - DesiredWidth) / 2); StringSizeF = g.MeasureString (strWatermark, wmFont); Ratio = StringSizeF.Width / wmFont.SizeInPoints; RequiredFontSize = DesiredWidth / Ratio; wmFont = new Font ("Arial", RequiredFontSize, FontStyle.Bold); xdelta = 0; // imageWidth / 25; ydelta = imageWidth / 20; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage (fullSizeImg, 0, 0, imageWidth, imageHeight); g.SmoothingMode = SmoothingMode.HighQuality; SolidBrush letterBrush = new SolidBrush (Color.FromArgb (50, 255, 255, 255)); SolidBrush shadowBrush = new SolidBrush (Color.FromArgb (50, 0, 0, 0)); g.DrawString (strWatermark, wmFont, shadowBrush, Convert.ToSingle (xOffset * 1.0) , Convert.ToSingle ( (imageHeight * 0.5) - 2.25 * ydelta)); g.DrawString (strWatermark, wmFont, letterBrush, Convert.ToSingle ( (xOffset + 3) * 1.0), Convert.ToSingle ( (imageHeight * 0.5) + 2 - 2.25 * ydelta)); wmFont = new Font ("Arial", 24, FontStyle.Bold); StringSizeF = g.MeasureString (strWatermarkowner, wmFont); Ratio = StringSizeF.Width / wmFont.SizeInPoints; RequiredFontSize = DesiredWidth / Ratio; wmFont = new Font ("Arial", RequiredFontSize, FontStyle.Bold); g.DrawString (strWatermarkowner, wmFont, shadowBrush, Convert.ToSingle (xOffset - xdelta), Convert.ToSingle ( (imageHeight * 0.5) + 0.25 * ydelta)); g.DrawString (strWatermarkowner, wmFont, letterBrush, Convert.ToSingle (xOffset + 3 - xdelta), Convert.ToSingle ( (imageHeight * 0.5) + 2 + 0.25 * ydelta)); Matrix myMatrix = new Matrix (); ______________________________________________________________________ css-discuss [css-d@lists.css-discuss.org] http://www.css-discuss.org/mailman/listinfo/css-d List wiki/FAQ -- http://css-discuss.incutio.com/ List policies -- http://css-discuss.org/policies.html Supported by evolt.org -- http://www.evolt.org/help_support_evolt/