private System.Drawing.Image GetScaledPhoto(int activismId, int ySize, int pWidth, int pNoOverlay) { ExternalActivity activity = ExternalActivity.FromIdentity(activismId); Documents docs = Documents.ForObject(activity); if (docs.Count == 0) { return(null); } using (System.Drawing.Image unscaledImage = System.Drawing.Image.FromFile(@"C:\Data\Uploads\PirateWeb\" + docs[0].ServerFileName)) { // Generate image float factor = 1; int xSize = 600; if (pWidth > 0 && pWidth < 1000) { factor = ((float)pWidth) / ((float)xSize); xSize = pWidth; ySize = (int)(xSize * ((float)unscaledImage.Height) / ((float)unscaledImage.Width)); } string label = activity.DateTime.ToString("yyyy-MM-dd") + ": " + activity.Description; System.Drawing.Image newImage = GetResizedImage(xSize, label, unscaledImage, pNoOverlay); return(newImage); } }
protected void Page_Load(object sender, EventArgs e) { int activismId = 0; string idString = Request.QueryString["Id"]; if (idString == "latest") { ExternalActivities activities = ExternalActivities.ForOrganization(Organization.PPSE, ExternalActivities.SortOrder.CreationDateDescending, 1); activismId = activities[0].Identity; } else { activismId = Int32.Parse(idString); } int ySize = 450; int pWidth = Int32.Parse("0" + Request.QueryString["Width"]); int pNoOverlay = Int32.Parse("0" + Request.QueryString["NoOverlay"]); int cacheLength = (activismId % 10) * 100 + 500; Response.Cache.SetExpires(DateTime.Now.AddSeconds(cacheLength)); string photoPath = @"C:\Data\Uploads\PirateWeb\ActivismImgCache\"; string fileName = "a" + activismId + "w" + pWidth + "o" + pNoOverlay + ".jpg"; Response.ContentType = "image/jpeg"; try { FileInfo cachedFile = new FileInfo(photoPath + fileName); ExternalActivity act = ExternalActivity.FromIdentity(activismId); if (cachedFile.LastWriteTime < act.CreatedDateTime) { cachedFile.Delete(); throw new FileNotFoundException("Cached File Invalid"); } Response.WriteFile(photoPath + fileName, false); } catch (FileNotFoundException) { // Call garbage collector to prevent Out Of Memory GC.Collect(); GC.WaitForPendingFinalizers(); using (System.Drawing.Image photo = GetScaledPhoto(activismId, ySize, pWidth, pNoOverlay)) { if (photo != null) { try { photo.Save(photoPath + fileName, ImageFormat.Jpeg); } catch (Exception) { } using (Stream outputstream = Response.OutputStream) { photo.Save(outputstream, ImageFormat.Jpeg); } } } } }