using PWAPPv2.Source.Attachments; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Windows.Media.Imaging; using System.Windows.Navigation; namespace PWAPPv2.Source.DataObjects { public class PWImage : PWFile { private string ImagePath; private BitmapImage bitmap; public string FileType { get; } //private Bitmap bmp; //private Bitmap thumb; //private Bitmap zoom; public PWImage(string path) { ImagePath = path; bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(@ImagePath); bitmap.DecodePixelWidth = 100; bitmap.EndInit(); string extension = Path.GetExtension(path).ToLower(); if (extension == ".jpeg" || extension == ".jpg") { FileType = "image/jpeg"; } else if (extension == ".png") { FileType = "image/png"; } else if (extension == ".tiff" || extension == ".tif") { FileType = "image/tiff"; } else if(extension == ".gif") { FileType = "image/gif"; } else if(extension == ".bmp") { FileType = "image/bmp"; } //bmp = (Bitmap)System.Drawing.Image.FromFile(ImagePath); //thumb = resize(bmp, 50, 50); //zoom = resize(bmp, bmp.Width, bmp.Height); } public string GetPath() { return ImagePath; } public BitmapImage GetBitmapImage() { return bitmap; } public string GetBase64String() { using (System.Drawing.Image image = System.Drawing.Image.FromFile(ImagePath)) { using (MemoryStream m = new MemoryStream()) { image.Save(m, image.RawFormat); byte[] imageBytes = m.ToArray(); return Convert.ToBase64String(imageBytes); } } } public string GetBase64ThumbString() { using (System.Drawing.Image image = System.Drawing.Image.FromFile(ImagePath)) { Image thumb = resize(image, new Size(50, 50)); using (MemoryStream m = new MemoryStream()) { thumb.Save(m, image.RawFormat); byte[] imageBytes = m.ToArray(); return Convert.ToBase64String(imageBytes); } } } public string GetBase64ZoomString() { using (System.Drawing.Image image = System.Drawing.Image.FromFile(ImagePath)) { Image zoom = resize(image, new Size(image.Width + 800, image.Height + 800)); using (MemoryStream m = new MemoryStream()) { zoom.Save(m, image.RawFormat); byte[] imageBytes = m.ToArray(); return Convert.ToBase64String(imageBytes); } } } private Image resize(Image toResize, Size size) { return new Bitmap(toResize, size); } public string ShortFileName() { return ImagePath.Substring(ImagePath.LastIndexOf("\\") + 1); } public string GetJsonContents() { string str = "'Base64FileContents':'" + GetBase64String() + "',\n" + "'Base64ThumbContents':'" + GetBase64ThumbString() + "',\n" + "'Base64ZoomContents':'" + GetBase64ZoomString() + "'\n"; return str; } public List GetFileUploads(string attToken, string ContentId) { List fileUploads = new List(); PWFileUpload baseImage = new PWFileUpload(); baseImage.ContentId = ContentId; baseImage.Base64Content = GetBase64String(); baseImage.AttToken = attToken; PWFileUpload zoom = new PWFileUpload(); zoom.Base64Content = GetBase64ZoomString(); zoom.ContentId = ContentId; zoom.Tag = "_zoom"; zoom.AttToken = attToken; PWFileUpload thumb = new PWFileUpload(); thumb.Base64Content= GetBase64ThumbString(); thumb.ContentId = ContentId; thumb.Tag = "_th"; thumb.AttToken = attToken; fileUploads.Add(baseImage); fileUploads.Add(zoom); fileUploads.Add(thumb); return fileUploads; } } }