///<summary>Gets the full sized image, with settings applied from doc. Throws exceptions.</summary> public static Bitmap GetFullImage(Document doc, string patientFolder) { //No need to check RemotingRole; no call to db. string shortFileName = doc.FileName; //If no file name associated with the document, then there cannot be a thumbnail, //because thumbnails have the same name as the original image document. if (shortFileName.Length < 1) { throw new ODException("No image file associated with document."); } string fullName = ODFileUtils.CombinePaths(patientFolder, shortFileName); //If the document no longer exists, then there is no corresponding thumbnail image. if (PrefC.AtoZfolderUsed == DataStorageType.LocalAtoZ && !File.Exists(fullName)) { throw new ODException("No image file found for document."); } //If the specified document is not an image return 'not available'. if (!ImageHelper.HasImageExtension(fullName)) { throw new ODException("Document is not associated to an image file format."); } Bitmap sourceImage = null; if (PrefC.AtoZfolderUsed == DataStorageType.LocalAtoZ) { sourceImage = new Bitmap(fullName); } else //Cloud { OpenDentalCloud.Core.TaskStateThumbnail state = CloudStorage.GetThumbnail(patientFolder, shortFileName); if (state.FileContent != null) { using (MemoryStream stream = new MemoryStream(state.FileContent)) { sourceImage = new Bitmap(Image.FromStream(stream)); } } else { sourceImage = new Bitmap(1, 1); } } Bitmap fullImage = ImageHelper.ApplyDocumentSettingsToImage(doc, sourceImage, ImageSettingFlags.ALL); sourceImage.Dispose(); return(fullImage); }
///<summary>Gets the corresponding thumbnail image for the given document object. The document is expected to be an image, and a 'not available' image is returned if the document is not an image. The thumbnail for every document is in a folder named 'thumbnails' within the same directly level.</summary> public static Bitmap GetThumbnail(Document doc, string patFolder, int size) { //No need to check RemotingRole; no call to db. string shortFileName = doc.FileName; //If no file name associated with the document, then there cannot be a thumbnail, //because thumbnails have the same name as the original image document. if (shortFileName.Length < 1) { return(NoAvailablePhoto(size)); } string fullName = ODFileUtils.CombinePaths(patFolder, shortFileName); //If the document no longer exists, then there is no corresponding thumbnail image. if (PrefC.AtoZfolderUsed == DataStorageType.LocalAtoZ && !File.Exists(fullName)) { return(NoAvailablePhoto(size)); } //If the specified document is not an image return 'not available'. if (!ImageHelper.HasImageExtension(fullName)) { return(NoAvailablePhoto(size)); } //Create Thumbnails folder if it does not already exist for this patient folder. string thumbPath = ODFileUtils.CombinePaths(patFolder, "Thumbnails"); if (PrefC.AtoZfolderUsed == DataStorageType.LocalAtoZ && !Directory.Exists(thumbPath)) { try { Directory.CreateDirectory(thumbPath); } catch { throw new ApplicationException(Lans.g("Documents", "Error: Could not create 'Thumbnails' folder for patient: ") + thumbPath); } } string thumbFileExt = Path.GetExtension(shortFileName); string thumbCoreFileName = shortFileName.Substring(0, shortFileName.Length - thumbFileExt.Length); string thumbFileName = ODFileUtils.CombinePaths(new string[] { patFolder, "Thumbnails", thumbCoreFileName + "_" + size + thumbFileExt }); //Use the existing thumbnail if it already exists and it was created after the last document modification. if (PrefC.AtoZfolderUsed == DataStorageType.LocalAtoZ && File.Exists(thumbFileName)) { try { DateTime thumbModifiedTime = File.GetLastWriteTime(thumbFileName); if (thumbModifiedTime > doc.DateTStamp) { return((Bitmap)Bitmap.FromFile(thumbFileName)); } } catch (Exception) { try { File.Delete(thumbFileName); //File may be invalid, corrupted, or unavailable. This was a bug in previous versions. }catch (Exception) { //we tried our best, and it just wasn't good enough } } } //Add thumbnail Bitmap thumbBitmap; //Gets the cropped/flipped/rotated image with any color filtering applied. Bitmap sourceImage = null; try { if (PrefC.AtoZfolderUsed == DataStorageType.LocalAtoZ) { sourceImage = new Bitmap(fullName); } else //Cloud { OpenDentalCloud.Core.TaskStateThumbnail state = CloudStorage.GetThumbnail(patFolder, shortFileName); if (state.FileContent != null) { using (MemoryStream stream = new MemoryStream(state.FileContent)) { sourceImage = new Bitmap(Image.FromStream(stream)); } } else { sourceImage = new Bitmap(1, 1); } } } catch { return(NoAvailablePhoto(size)); } Bitmap fullImage = ImageHelper.ApplyDocumentSettingsToImage(doc, sourceImage, ImageSettingFlags.ALL); sourceImage.Dispose(); thumbBitmap = ImageHelper.GetThumbnail(fullImage, size); fullImage.Dispose(); if (PrefC.AtoZfolderUsed == DataStorageType.LocalAtoZ) //Only save thumbnail to local directory if using local AtoZ { try { thumbBitmap.Save(thumbFileName); } catch { //Oh well, we can regenerate it next time if we have to! } } return(thumbBitmap); }