public static bool create_icon(Guid?applicationId, Guid iconId, IconType iconType, byte[] fileContent, ref string errorMessage, ref IconMeta meta) { int width = 100, height = 100, highQualityWidth = 600, highQualityHeight = 600; FolderNames imageFolder = FolderNames.ProfileImages, highQualityImageFolder = FolderNames.HighQualityProfileImage; bool isValid = DocumentUtilities.get_icon_parameters(applicationId, iconType, ref width, ref height, ref highQualityWidth, ref highQualityHeight, ref imageFolder, ref highQualityImageFolder); if (!isValid) { return(false); } DocFileInfo highQualityFile = new DocFileInfo() { FileID = iconId, Extension = "jpg", FolderName = highQualityImageFolder }; DocFileInfo file = new DocFileInfo() { FileID = iconId, Extension = "jpg", FolderName = imageFolder }; byte[] hqContent = new byte[0]; bool succeed = RVGraphics.make_thumbnail(applicationId, fileContent, highQualityFile, ref hqContent, highQualityWidth, highQualityHeight, width, height, ref errorMessage, highQualityFile.Extension, dontStore: false); return(succeed && hqContent != null && hqContent.Length > 0 && RVGraphics.extract_thumbnail(applicationId, highQualityFile, hqContent, file, -1, -1, -1, -1, width, height, ref errorMessage, ref meta)); }
public static bool extract_thumbnail(Guid?applicationId, DocFileInfo sourceFile, byte[] sourceContent, DocFileInfo destFile, int x, int y, int width, int height, int thumbnailWidth, int thumbnailHeight, ref string message, ref IconMeta meta) { try { if (sourceFile == null || sourceContent == null || sourceContent.Length == 0) { return(false); } destFile.Extension = "jpg"; if (!string.IsNullOrEmpty(sourceFile.Extension) && sourceFile.Extension != "jpg") { using (MemoryStream stream = new MemoryStream(sourceContent)) using (MemoryStream newSt = new MemoryStream()) using (Image img = Bitmap.FromStream(stream)) { img.Save(newSt, ImageFormat.Jpeg); img.Dispose(); //sourceFile.delete(applicationId); sourceFile.Extension = "jpg"; sourceContent = newSt.ToArray(); //sourceFile.store(applicationId, newSt.ToArray()); } } Image retImage = null; if (extract_thumbnail(applicationId, sourceFile, sourceContent, destFile, x, y, width, height, thumbnailWidth, thumbnailHeight, ref retImage, ref message, ref meta)) { using (MemoryStream st = new MemoryStream()) { retImage.Save(st, ImageFormat.Jpeg); retImage.Dispose(); destFile.store(applicationId, st.ToArray()); } } return(true); } catch { message = "{\"ErrorText\":\"" + Messages.OperationFailed.ToString() + "\"}"; return(false); } }
public static bool extract_thumbnail(Guid?applicationId, DocFileInfo sourceFile, byte[] sourceContent, DocFileInfo destFile, int x, int y, int width, int height, int thumbnailWidth, int thumbnailHeight, ref Image retImage, ref string message, ref IconMeta meta) { try { if (sourceContent == null || sourceContent.Length == 0) { return(false); } bool result = true; using (MemoryStream stream = new MemoryStream(sourceContent)) using (Bitmap image = Bitmap.FromStream(stream) as Bitmap) { if (image.Width < thumbnailWidth || image.Height < thumbnailHeight) { message = "{\"ErrorText\":\"" + Messages.ImageSizeIsNotValid.ToString() + "\"}"; image.Dispose(); return(false); } if (x < 0 || y < 0 || width <= 0 || height <= 0) { double aspectRatio = (double)thumbnailWidth / (double)thumbnailHeight; int min = ((double)image.Width / (double)thumbnailWidth) < ((double)image.Height / (double)thumbnailHeight) ? image.Width : image.Height; if (min == image.Width) { width = image.Width; height = (int)Math.Floor((double)width / aspectRatio); } else { height = image.Height; width = (int)Math.Floor((double)height * aspectRatio); } x = (image.Width - width) / 2; y = (image.Height - height) / 2; } meta = new IconMeta() { X = x, Y = y, Width = width, Height = height, Icon = destFile, HighQualityIcon = sourceFile }; message = meta.toJson(applicationId); Rectangle rect = new Rectangle(x, y, width, height); using (Bitmap target = new Bitmap(rect.Width, rect.Height)) { Graphics g = Graphics.FromImage(target); g.DrawImage(image, new Rectangle(0, 0, target.Width, target.Height), rect, GraphicsUnit.Pixel); result = make_thumbnail(target as Image, thumbnailWidth, thumbnailHeight, 0, 0, ref retImage, ref message, true); } if (!result) { message = "{\"ErrorText\":\"" + Messages.OperationFailed + "\"}"; } return(result); } } catch { message = "{\"ErrorText\":\"" + Messages.OperationFailed + "\"}"; return(false); } }