/// <summary> /// Saves the three quest icons /// </summary> /// <param name="newFileNameAndPath">The file and path for saving the images</param> /// <param name="iconNameNoExt">The icon name without extension</param> /// <param name="userGeneratedQuest">Is this a user generated quest?</param> public static Boolean SaveQuestIcons(string newFileNameAndPath, string iconNameNoExt, bool userGeneratedQuest) { try { Image image = Image.FromFile(HttpContext.Current.Server.MapPath(JPPConstants.Images.IconPath + iconNameNoExt + ".png")); ImageSaveInfo info = new ImageSaveInfo(0, 0, 0, 0, userGeneratedQuest ? ImageSaveInfo.ImageType.CommunityQuest : ImageSaveInfo.ImageType.SystemQuest); String savePath = ""; if (HttpContext.Current.Server.MapPath(newFileNameAndPath).Contains(".jpg")) { savePath = HttpContext.Current.Server.MapPath(newFileNameAndPath).Replace(".jpg", ""); } savePath = HttpContext.Current.Server.MapPath(newFileNameAndPath).Replace(".png", ""); SaveImageAtSquareSize(savePath + "_s.png", image, JPPConstants.Images.SizeSmall, info); SaveImageAtSquareSize(savePath + "_m.png", image, JPPConstants.Images.SizeMedium, info); SaveImageAtSquareSize(savePath + ".png", image, JPPConstants.Images.SizeLarge, info); image.Dispose(); return(true); } catch { return(false); } }
/// <summary> /// Saves the three achievement icons /// </summary> /// <param name="newFileNameAndPath">The file and path for saving the images</param> /// <param name="iconNameNoExt">The icon name without extension</param> /// <param name="create">Create points for the achievement</param> /// <param name="explore">Explore points for the achievement</param> /// <param name="learn">Learn points for the achievement</param> /// <param name="socialize">Socialize points for the achievement</param> public static bool SaveAchievementIcons(string newFileNameAndPath, string iconNameNoExt, int create, int explore, int learn, int socialize) { String imagePath = ""; try { Image image = Image.FromFile(HttpContext.Current.Server.MapPath(JPPConstants.Images.IconPath + iconNameNoExt + ".png")); ImageSaveInfo info = new ImageSaveInfo(create, explore, learn, socialize, ImageSaveInfo.ImageType.Achievement); String savePath = ""; if (HttpContext.Current.Server.MapPath(newFileNameAndPath).Contains(".jpg")) { savePath = HttpContext.Current.Server.MapPath(newFileNameAndPath).Replace(".jpg", ""); } savePath = HttpContext.Current.Server.MapPath(newFileNameAndPath).Replace(".png", ""); imagePath = savePath; SaveImageAtSquareSize(savePath + "_s.png", image, JPPConstants.Images.SizeSmall, info); SaveImageAtSquareSize(savePath + "_m.png", image, JPPConstants.Images.SizeMedium, info); SaveImageAtSquareSize(savePath + ".png", image, JPPConstants.Images.SizeLarge, info); image.Dispose(); return(true); } catch { return(false); } }
/// <summary> /// Saves the three player images /// </summary> /// <param name="filePath">The file path (no file name)</param> /// <param name="fileNameNoExt">The file name without extension</param> /// <param name="stream">The image stream</param> public static Boolean SavePlayerImages(string filePath, string fileNameNoExt, Stream stream) { try { Image image = Image.FromStream(stream); ImageSaveInfo info = new ImageSaveInfo(0, 0, 0, 0, ImageSaveInfo.ImageType.Player); String savePath = ""; if (HttpContext.Current.Server.MapPath(filePath).Contains(".jpg")) { savePath = HttpContext.Current.Server.MapPath(filePath).Replace(".jpg", ""); } savePath = HttpContext.Current.Server.MapPath(filePath).Replace(".png", ""); SaveImageAtSquareSize(savePath + "_s.png", image, JPPConstants.Images.SizeSmall, info); SaveImageAtSquareSize(savePath + "_m.png", image, JPPConstants.Images.SizeMedium, info); SaveImageAtSquareSize(savePath + ".png", image, JPPConstants.Images.SizeLarge, info); image.Dispose(); return(true); } catch { // Problem return(false); } }
/// <summary> /// Saves the three player qrcodes /// </summary> /// <param name="filePath">The file path (no file name)</param> /// <param name="fileNameNoExt">The file name without extension</param> /// <param name="stream">The image stream</param> public static Boolean SavePlayerQRCodes(string filePath, string fileNameNoExt, string qrString) { var qrValue = qrString; var barcodeWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new EncodingOptions { Height = 250, Width = 250, Margin = 0 } }; using (var bitmap = barcodeWriter.Write(qrValue)) using (var stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); try { Image image = Image.FromStream(stream); ImageSaveInfo info = new ImageSaveInfo(0, 0, 0, 0, ImageSaveInfo.ImageType.Player); String savePath = ""; if (HttpContext.Current.Server.MapPath(filePath).Contains(".jpg")) { savePath = HttpContext.Current.Server.MapPath(filePath).Replace(".jpg", ""); } savePath = HttpContext.Current.Server.MapPath(filePath).Replace(".png", ""); SaveImageAtSquareSize(savePath + "_s.png", image, JPPConstants.Images.SizeSmall, info); SaveImageAtSquareSize(savePath + "_m.png", image, JPPConstants.Images.SizeMedium, info); SaveImageAtSquareSize(savePath + ".png", image, JPPConstants.Images.SizeLarge, info); image.Dispose(); return(true); } catch { // Problem return(false); } } }
/// <summary> /// Saves the three quest icons /// </summary> /// <param name="newFileNameAndPath">The file and path for saving the images</param> /// <param name="iconNameNoExt">The icon name without extension</param> /// <param name="userGeneratedQuest">Is this a user generated quest?</param> public static Boolean SaveQuestIcons(string newFileNameAndPath, string iconNameNoExt, bool userGeneratedQuest) { try { Image image = Image.FromFile(HttpContext.Current.Server.MapPath(JPPConstants.Images.IconPath + iconNameNoExt + ".png")); ImageSaveInfo info = new ImageSaveInfo(0, 0, 0, 0, userGeneratedQuest ? ImageSaveInfo.ImageType.CommunityQuest : ImageSaveInfo.ImageType.SystemQuest); String savePath = ""; if (HttpContext.Current.Server.MapPath(newFileNameAndPath).Contains(".jpg")) { savePath = HttpContext.Current.Server.MapPath(newFileNameAndPath).Replace(".jpg", ""); } savePath = HttpContext.Current.Server.MapPath(newFileNameAndPath).Replace(".png", ""); SaveImageAtSquareSize(savePath + "_s.png", image, JPPConstants.Images.SizeSmall, info); SaveImageAtSquareSize(savePath + "_m.png", image, JPPConstants.Images.SizeMedium, info); SaveImageAtSquareSize(savePath + ".png", image, JPPConstants.Images.SizeLarge, info); image.Dispose(); return true; } catch { return false; } }
/// <summary> /// Saves the three player qrcodes /// </summary> /// <param name="filePath">The file path (no file name)</param> /// <param name="fileNameNoExt">The file name without extension</param> /// <param name="stream">The image stream</param> public static Boolean SavePlayerQRCodes(string filePath, string fileNameNoExt, string qrString) { var qrValue = qrString; var barcodeWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new EncodingOptions { Height = 250, Width = 250, Margin = 0 } }; using (var bitmap = barcodeWriter.Write(qrValue)) using (var stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); try { Image image = Image.FromStream(stream); ImageSaveInfo info = new ImageSaveInfo(0, 0, 0, 0, ImageSaveInfo.ImageType.Player); String savePath = ""; if (HttpContext.Current.Server.MapPath(filePath).Contains(".jpg")) { savePath = HttpContext.Current.Server.MapPath(filePath).Replace(".jpg", ""); } savePath = HttpContext.Current.Server.MapPath(filePath).Replace(".png", ""); SaveImageAtSquareSize(savePath + "_s.png", image, JPPConstants.Images.SizeSmall, info); SaveImageAtSquareSize(savePath + "_m.png", image, JPPConstants.Images.SizeMedium, info); SaveImageAtSquareSize(savePath + ".png", image, JPPConstants.Images.SizeLarge, info); image.Dispose(); return true; } catch { // Problem return false; } } }
/// <summary> /// Saves the three player images /// </summary> /// <param name="filePath">The file path (no file name)</param> /// <param name="fileNameNoExt">The file name without extension</param> /// <param name="stream">The image stream</param> public static Boolean SavePlayerImages(string filePath, string fileNameNoExt, Stream stream) { try { Image image = Image.FromStream(stream); ImageSaveInfo info = new ImageSaveInfo(0, 0, 0, 0, ImageSaveInfo.ImageType.Player); String savePath = ""; if (HttpContext.Current.Server.MapPath(filePath).Contains(".jpg")) { savePath = HttpContext.Current.Server.MapPath(filePath).Replace(".jpg", ""); } savePath = HttpContext.Current.Server.MapPath(filePath).Replace(".png", ""); SaveImageAtSquareSize(savePath + "_s.png", image, JPPConstants.Images.SizeSmall, info); SaveImageAtSquareSize(savePath + "_m.png", image, JPPConstants.Images.SizeMedium, info); SaveImageAtSquareSize(savePath + ".png", image, JPPConstants.Images.SizeLarge, info); image.Dispose(); return true; } catch { // Problem return false; } }
/// <summary> /// Saves an image (from a stream) at a specific size /// </summary> /// <param name="filePath">The file name and path for saving</param> /// <param name="stream">The image stream</param> /// <param name="size">The size for saving</param> public static void SaveImageAtSquareSize(string filePath, Image originalImage, int size, ImageSaveInfo info) { // New empty image and graphics for manipulation Bitmap newImage = new Bitmap(size, size); Graphics g = Graphics.FromImage(newImage); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.CompositingMode = CompositingMode.SourceOver; g.CompositingQuality = CompositingQuality.HighQuality; // Size cases LargerSide largerSide = LargerSide.Same; if (originalImage.Width > originalImage.Height) largerSide = LargerSide.Width; else if (originalImage.Height > originalImage.Width) largerSide = LargerSide.Height; int offsetWidth = 0; int offsetHeight = 0; int scaledWidth = originalImage.Width; int scaledHeight = originalImage.Height; float scale = 1.0f; switch (largerSide) { // Both are the same, just use width calculation case LargerSide.Same: // Width is longer, so stretch on height case LargerSide.Width: scale = size / (float)originalImage.Height; break; // Height is longer, so stretch on width case LargerSide.Height: scale = size / (float)originalImage.Width; break; } // Figure out offsets scaledWidth = (int)(originalImage.Width * scale); scaledHeight = (int)(originalImage.Height * scale); offsetHeight = (size - scaledHeight) / 2; offsetWidth = (size - scaledWidth) / 2; // Handle achievements and quests (do nothing here for Players) int inset = (int)(size * JPPConstants.Images.QuadBorderOffsetPercent); int insetSize = size - (inset * 2) - 1; // Adjust slightly for smaller images switch (info.Type) { case ImageSaveInfo.ImageType.Achievement: // Quads Brush create = new SolidBrush(info.Create <= 0 ? JPPConstants.Images.QuadCreateOffColor : JPPConstants.Images.QuadCreateOnColor); Brush explore = new SolidBrush(info.Explore <= 0 ? JPPConstants.Images.QuadExploreOffColor : JPPConstants.Images.QuadExploreOnColor); Brush learn = new SolidBrush(info.Learn <= 0 ? JPPConstants.Images.QuadLearnOffColor : JPPConstants.Images.QuadLearnOnColor); Brush social = new SolidBrush(info.Socialize <= 0 ? JPPConstants.Images.QuadSocializeOffColor : JPPConstants.Images.QuadSocializeOnColor); g.FillPie(create, 0, 0, size, size, 180, 90); g.FillPie(explore, 0, 0, size, size, 0, 90); g.FillPie(learn, 0, 0, size, size, 270, 90); g.FillPie(social, 0, 0, size, size, 90, 90); // Adjust image offsetWidth += inset; offsetHeight += inset; scaledWidth -= (inset * 2); scaledHeight -= (inset * 2); break; case ImageSaveInfo.ImageType.SystemQuest: case ImageSaveInfo.ImageType.CommunityQuest: // Background Brush brush = new SolidBrush(info.Type == ImageSaveInfo.ImageType.SystemQuest ? JPPConstants.Images.QuestSystemColor : JPPConstants.Images.QuestCommunityColor); g.FillPie(brush, 0, 0, size, size, 0, 360); // Adjust image offsetWidth += inset; offsetHeight += inset; scaledWidth -= (inset * 2); scaledHeight -= (inset * 2); break; } // Draw the image g.DrawImage(originalImage, offsetWidth, offsetHeight, scaledWidth, scaledHeight); // Now put the borders on if (info.Type != ImageSaveInfo.ImageType.Player) { // Clip the edges Pen clipPen = new Pen(Color.FromArgb(0, 255, 255, 255), size * JPPConstants.Images.QuadBorderWidthPercent * 12); int clipInset = (int)(size * -0.1f) - 1; // Adjust for smaller images int clipSize = size - (clipInset * 2); g.CompositingMode = CompositingMode.SourceCopy; g.DrawArc(clipPen, clipInset, clipInset, clipSize, clipSize, 0, 360); g.CompositingMode = CompositingMode.SourceOver; // Set up widths int penWidth = (int)(size * JPPConstants.Images.QuadBorderOffsetPercent); int halfWidth = penWidth / 2; switch (info.Type) { case ImageSaveInfo.ImageType.Achievement: // Quads Pen create = new Pen(info.Create <= 0 ? JPPConstants.Images.QuadCreateOffColor : JPPConstants.Images.QuadCreateOnColor, penWidth); Pen explore = new Pen(info.Explore <= 0 ? JPPConstants.Images.QuadExploreOffColor : JPPConstants.Images.QuadExploreOnColor, penWidth); Pen learn = new Pen(info.Learn <= 0 ? JPPConstants.Images.QuadLearnOffColor : JPPConstants.Images.QuadLearnOnColor, penWidth); Pen social = new Pen(info.Socialize <= 0 ? JPPConstants.Images.QuadSocializeOffColor : JPPConstants.Images.QuadSocializeOnColor, penWidth); g.DrawArc(create, halfWidth, halfWidth, size - penWidth, size - penWidth, 180, 90); g.DrawArc(explore, halfWidth, halfWidth, size - penWidth, size - penWidth, 0, 90); g.DrawArc(learn, halfWidth, halfWidth, size - penWidth, size - penWidth, 270, 90); g.DrawArc(social, halfWidth, halfWidth, size - penWidth, size - penWidth, 90, 90); break; case ImageSaveInfo.ImageType.CommunityQuest: case ImageSaveInfo.ImageType.SystemQuest: Pen pen = new Pen( info.Type == ImageSaveInfo.ImageType.SystemQuest ? JPPConstants.Images.QuestSystemColor : JPPConstants.Images.QuestCommunityColor, penWidth); g.DrawArc(pen, halfWidth, halfWidth, size - penWidth, size - penWidth, 0, 360); break; } // White Border Pen borderPen = new Pen(Color.FromKnownColor(KnownColor.White), size * JPPConstants.Images.QuadBorderWidthPercent); g.DrawArc(borderPen, inset, inset, insetSize, insetSize, 0, 360); } // All done newImage.Save(filePath, ImageFormat.Png); // Cleanup newImage.Dispose(); }
/// <summary> /// Saves the three achievement icons /// </summary> /// <param name="newFileNameAndPath">The file and path for saving the images</param> /// <param name="iconNameNoExt">The icon name without extension</param> /// <param name="create">Create points for the achievement</param> /// <param name="explore">Explore points for the achievement</param> /// <param name="learn">Learn points for the achievement</param> /// <param name="socialize">Socialize points for the achievement</param> public static bool SaveAchievementIcons(string newFileNameAndPath, string iconNameNoExt, int create, int explore, int learn, int socialize) { String imagePath = ""; try { Image image = Image.FromFile(HttpContext.Current.Server.MapPath(JPPConstants.Images.IconPath + iconNameNoExt + ".png")); ImageSaveInfo info = new ImageSaveInfo(create, explore, learn, socialize, ImageSaveInfo.ImageType.Achievement); String savePath = ""; if (HttpContext.Current.Server.MapPath(newFileNameAndPath).Contains(".jpg")) { savePath = HttpContext.Current.Server.MapPath(newFileNameAndPath).Replace(".jpg", ""); } savePath = HttpContext.Current.Server.MapPath(newFileNameAndPath).Replace(".png", ""); imagePath = savePath; SaveImageAtSquareSize(savePath + "_s.png", image, JPPConstants.Images.SizeSmall, info); SaveImageAtSquareSize(savePath + "_m.png", image, JPPConstants.Images.SizeMedium, info); SaveImageAtSquareSize(savePath + ".png", image, JPPConstants.Images.SizeLarge, info); image.Dispose(); return true; } catch { return false; } }
/// <summary> /// Saves an image (from a stream) at a specific size /// </summary> /// <param name="filePath">The file name and path for saving</param> /// <param name="stream">The image stream</param> /// <param name="size">The size for saving</param> public static void SaveImageAtSquareSize(string filePath, Image originalImage, int size, ImageSaveInfo info) { // New empty image and graphics for manipulation Bitmap newImage = new Bitmap(size, size); Graphics g = Graphics.FromImage(newImage); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.CompositingMode = CompositingMode.SourceOver; g.CompositingQuality = CompositingQuality.HighQuality; // Size cases LargerSide largerSide = LargerSide.Same; if (originalImage.Width > originalImage.Height) { largerSide = LargerSide.Width; } else if (originalImage.Height > originalImage.Width) { largerSide = LargerSide.Height; } int offsetWidth = 0; int offsetHeight = 0; int scaledWidth = originalImage.Width; int scaledHeight = originalImage.Height; float scale = 1.0f; switch (largerSide) { // Both are the same, just use width calculation case LargerSide.Same: // Width is longer, so stretch on height case LargerSide.Width: scale = size / (float)originalImage.Height; break; // Height is longer, so stretch on width case LargerSide.Height: scale = size / (float)originalImage.Width; break; } // Figure out offsets scaledWidth = (int)(originalImage.Width * scale); scaledHeight = (int)(originalImage.Height * scale); offsetHeight = (size - scaledHeight) / 2; offsetWidth = (size - scaledWidth) / 2; // Handle achievements and quests (do nothing here for Players) int inset = (int)(size * JPPConstants.Images.QuadBorderOffsetPercent); int insetSize = size - (inset * 2) - 1; // Adjust slightly for smaller images switch (info.Type) { case ImageSaveInfo.ImageType.Achievement: // Quads Brush create = new SolidBrush(info.Create <= 0 ? JPPConstants.Images.QuadCreateOffColor : JPPConstants.Images.QuadCreateOnColor); Brush explore = new SolidBrush(info.Explore <= 0 ? JPPConstants.Images.QuadExploreOffColor : JPPConstants.Images.QuadExploreOnColor); Brush learn = new SolidBrush(info.Learn <= 0 ? JPPConstants.Images.QuadLearnOffColor : JPPConstants.Images.QuadLearnOnColor); Brush social = new SolidBrush(info.Socialize <= 0 ? JPPConstants.Images.QuadSocializeOffColor : JPPConstants.Images.QuadSocializeOnColor); g.FillPie(create, 0, 0, size, size, 180, 90); g.FillPie(explore, 0, 0, size, size, 0, 90); g.FillPie(learn, 0, 0, size, size, 270, 90); g.FillPie(social, 0, 0, size, size, 90, 90); // Adjust image offsetWidth += inset; offsetHeight += inset; scaledWidth -= (inset * 2); scaledHeight -= (inset * 2); break; case ImageSaveInfo.ImageType.SystemQuest: case ImageSaveInfo.ImageType.CommunityQuest: // Background Brush brush = new SolidBrush(info.Type == ImageSaveInfo.ImageType.SystemQuest ? JPPConstants.Images.QuestSystemColor : JPPConstants.Images.QuestCommunityColor); g.FillPie(brush, 0, 0, size, size, 0, 360); // Adjust image offsetWidth += inset; offsetHeight += inset; scaledWidth -= (inset * 2); scaledHeight -= (inset * 2); break; } // Draw the image g.DrawImage(originalImage, offsetWidth, offsetHeight, scaledWidth, scaledHeight); // Now put the borders on if (info.Type != ImageSaveInfo.ImageType.Player) { // Clip the edges Pen clipPen = new Pen(Color.FromArgb(0, 255, 255, 255), size * JPPConstants.Images.QuadBorderWidthPercent * 12); int clipInset = (int)(size * -0.1f) - 1; // Adjust for smaller images int clipSize = size - (clipInset * 2); g.CompositingMode = CompositingMode.SourceCopy; g.DrawArc(clipPen, clipInset, clipInset, clipSize, clipSize, 0, 360); g.CompositingMode = CompositingMode.SourceOver; // Set up widths int penWidth = (int)(size * JPPConstants.Images.QuadBorderOffsetPercent); int halfWidth = penWidth / 2; switch (info.Type) { case ImageSaveInfo.ImageType.Achievement: // Quads Pen create = new Pen(info.Create <= 0 ? JPPConstants.Images.QuadCreateOffColor : JPPConstants.Images.QuadCreateOnColor, penWidth); Pen explore = new Pen(info.Explore <= 0 ? JPPConstants.Images.QuadExploreOffColor : JPPConstants.Images.QuadExploreOnColor, penWidth); Pen learn = new Pen(info.Learn <= 0 ? JPPConstants.Images.QuadLearnOffColor : JPPConstants.Images.QuadLearnOnColor, penWidth); Pen social = new Pen(info.Socialize <= 0 ? JPPConstants.Images.QuadSocializeOffColor : JPPConstants.Images.QuadSocializeOnColor, penWidth); g.DrawArc(create, halfWidth, halfWidth, size - penWidth, size - penWidth, 180, 90); g.DrawArc(explore, halfWidth, halfWidth, size - penWidth, size - penWidth, 0, 90); g.DrawArc(learn, halfWidth, halfWidth, size - penWidth, size - penWidth, 270, 90); g.DrawArc(social, halfWidth, halfWidth, size - penWidth, size - penWidth, 90, 90); break; case ImageSaveInfo.ImageType.CommunityQuest: case ImageSaveInfo.ImageType.SystemQuest: Pen pen = new Pen( info.Type == ImageSaveInfo.ImageType.SystemQuest ? JPPConstants.Images.QuestSystemColor : JPPConstants.Images.QuestCommunityColor, penWidth); g.DrawArc(pen, halfWidth, halfWidth, size - penWidth, size - penWidth, 0, 360); break; } // White Border Pen borderPen = new Pen(Color.FromKnownColor(KnownColor.White), size * JPPConstants.Images.QuadBorderWidthPercent); g.DrawArc(borderPen, inset, inset, insetSize, insetSize, 0, 360); } // All done newImage.Save(filePath, ImageFormat.Png); // Cleanup newImage.Dispose(); }