public static Photo ToProfilePhoto(this user usr, Imgsize size, bool returnNoPhotoThumbnail = false) { Photo photo = null; if (usr.profilePhoto.HasValue) { photo = usr.image.ToModel(size); } else if (!string.IsNullOrEmpty(usr.externalProfilePhoto)) { // detect if twitter or facebook if (!string.IsNullOrEmpty(usr.FBID)) { photo = new Photo { bigUrl = usr.externalProfilePhoto, url = usr.externalProfilePhoto }; } } else if (returnNoPhotoThumbnail) { photo = new Photo { bigUrl = GeneralConstants.PHOTO_NO_PROFILE, url = GeneralConstants.PHOTO_NO_PROFILE }; } return(photo); }
/// <summary> /// returns path to thumbnail, if thumbnail does not exist it creates it then returns the new path /// </summary> /// <param name="filePath">the path to the thumbnail, usually stored in db</param> /// <param name="imgsize"></param> /// <param name="returnLocalFileOnly"></param> /// <returns></returns> public static string by_size(string filePath, Imgsize type) { var dim = getImageDimensionsFromSize(type); int width = dim.Width; int height = dim.Height; // appends dimension to path string suffix = type.ToString().ToLower(); string ofile; string thumb = ofile = filePath; string ext = thumb.Substring(thumb.LastIndexOf(".") + 1); string part1 = thumb.Substring(0, thumb.LastIndexOf(".")); if (part1.IndexOf(".") == -1) { thumb = part1 + "_" + suffix + "." + ext; } else { string part2 = part1.Substring(0, part1.LastIndexOf(".")); thumb = part2 + "_" + suffix + "." + ext; } bool fileExist = false; #if AZURE fileExist = photoContainer.DoesBlobItemExists(thumb); #else #if RACKSPACE // check if thumbnail exists on rackspace first if (!returnLocalFileOnly) { CloudFile cf = new CloudFile(Constants.RACKSPACE_CONTAINER_IMAGES); fileExist = cf.DoesBlobItemExists(Path.GetFileName(thumb)); if (fileExist) { thumb = cf.GetBlobItemUri(Path.GetFileName(thumb)); } } #endif if (!fileExist) { fileExist = File.Exists(GeneralConstants.APP_ROOT_DIR + thumb); } #endif if (!fileExist) { thumb = thumbnail(ofile, suffix, width, height, type); } #if AZURE if (!string.IsNullOrEmpty(thumb)) { return(photoContainer.GetBlobItemUri(thumb)); } else { return(""); } #else return(thumb); #endif }
/// <summary> /// returns path to thumbnail, if thumbnail does not exist it creates it then returns the new path /// </summary> /// <param name="filePath">the path to the thumbnail, usually stored in db</param> /// <param name="type"></param> /// <returns></returns> public static string by_size(string filePath, Imgsize type) { var dim = getImageDimensionsFromSize(type); int width = dim.Width; int height = dim.Height; // appends dimension to path string suffix = width + "x" + height; string ofile; string thumb = ofile = filePath; string ext = thumb.Substring(thumb.LastIndexOf(".") + 1); string part1 = thumb.Substring(0, thumb.LastIndexOf(".")); if (part1.IndexOf(".") == -1) { thumb = part1 + "." + suffix + "." + ext; } else { string part2 = part1.Substring(0, part1.LastIndexOf(".")); thumb = part2 + "." + suffix + "." + ext; } bool fileExist = false; if (!fileExist) { fileExist = System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + thumb); } if (!fileExist) { thumb = thumbnail(ofile, suffix, width, height, type); } return(thumb); }
private static Size getImageDimensionsFromSize(Imgsize size) { switch (size) { case Imgsize.THUMB: return(new Size(200, 300)); case Imgsize.BLOG: return(new Size(800, 500)); case Imgsize.GALLERY: return(new Size(75, 75)); case Imgsize.MAX: return(new Size(900, 675)); case Imgsize.USER_THUMB: return(new Size(50, 50)); case Imgsize.USER_PROFILE: return(new Size(270, 270)); default: throw new ArgumentException(); } }
public XmlDocument SaveToXmlDoc() { XmlDocument xmlDoc = new XmlDocument(); XmlElement root = xmlDoc.CreateElement(NodeName); xmlDoc.AppendChild(root); XmlNode path = xmlDoc.CreateElement("BASE"); root.AppendChild(path); path.InnerXml = Imgsize.ToXmlString() + _imagefilename.ToXmlString("PATH") + Correctrect.ToXmlString().ToXmlString("CORRECTRECT"); foreach (string s in new string[] { "特征点-FEATUREPOINTSAREA", "考号-KAOHAOAREA", "姓名-NAMEAREA", "选择题-SINGLECHOICES", "非选择题-UNCHOOSES", "选区变黑-BLACKAREA", "选区变白-WHITEAREA" }) { string name = s.Substring(0, s.IndexOf("-")); string ENname = s.Substring(s.IndexOf("-") + 1); XmlNode list = xmlDoc.CreateElement(ENname + "S"); root.AppendChild(list); int i = 0; if (_dic.ContainsKey(name)) { foreach (Area I in _dic[name]) { XmlElement xe = xmlDoc.CreateElement(ENname); xe.SetAttribute("ID", i.ToString()); xe.InnerXml = I.ToXmlString(); list.AppendChild(xe); i++; } } } return(xmlDoc); }
public static IEnumerable <Photo> ToModel(this IQueryable <image> images, Imgsize size) { foreach (var image in images) { yield return(image.ToModel(size)); } }
private static Image createResizedImage(Image originalImage, Size newSize, Imgsize type) { //Detach image from its source var oImageOriginal = (Image)originalImage.Clone(); //Resize new image var oResizedImage = new Bitmap(newSize.Width, newSize.Height); Graphics oGraphic = Graphics.FromImage(oResizedImage); oGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality; oGraphic.CompositingQuality = CompositingQuality.HighSpeed; oGraphic.SmoothingMode = SmoothingMode.HighSpeed; oGraphic.InterpolationMode = InterpolationMode.Low; var oRectangle = new Rectangle(0, 0, newSize.Width, newSize.Height); oGraphic.DrawImage(oImageOriginal, oRectangle); // cleanup oGraphic.Dispose(); if (type != Imgsize.BANNER && newSize.Width != newSize.Height) { float size; float ratioX; float ratioY; if (newSize.Width > newSize.Height) { size = newSize.Width; ratioX = size; ratioY = (float)newSize.Height / (float)newSize.Width; ratioY *= size; } else { size = newSize.Height; ratioY = size; ratioX = (float)newSize.Width / (float)newSize.Height; ratioX *= size; } int sizeX = (int)ratioX; int sizeY = (int)ratioY; var backgroundImage = new Bitmap((int)size, (int)size, PixelFormat.Format32bppPArgb); Graphics g = Graphics.FromImage(backgroundImage); g.CompositingQuality = CompositingQuality.AssumeLinear; g.SmoothingMode = SmoothingMode.Default; g.InterpolationMode = InterpolationMode.High; g.FillRectangle(Brushes.White, 0, 0, size, size); g.DrawImageUnscaled(oResizedImage, (int)((size - sizeX) / 2), (int)((size - sizeY) / 2)); g.Dispose(); return(backgroundImage); } oImageOriginal.Dispose(); return(oResizedImage); }
public static string GetProfilePhoto(this user usr, Imgsize sz = Imgsize.SMALL) { if (usr.profilePhoto.HasValue) { return(Img.by_size(usr.image.url, sz).ToHtmlImage()); } return(GeneralConstants.PHOTO_NO_PROFILE.ToHtmlImage()); }
public static Photo ToModel(this user_image img, Imgsize size, Imgsize fullsize = Imgsize.MAX) { return(new Photo() { id = img.id, url = Img.by_size(img.url, size), url_fullsize = Img.by_size(img.url, fullsize) }); }
public static Photo ToModel(this image img, Imgsize size, string externalid = null) { return(new Photo() { id = img.id, bigUrl = img.url, url = Img.by_size(img.url, size), externalid = externalid }); }
private static bool createImage(string srcName, string destName, int desiredWidth, int desiredHeight, Imgsize type) { var source = AppDomain.CurrentDomain.BaseDirectory + srcName; var destination = AppDomain.CurrentDomain.BaseDirectory + destName; // Capture the original size of the uploaded image Image src = null; try { src = Image.FromFile(source); } catch (Exception ex) { if (src != null) { src.Dispose(); } throw; } //Resize new image //Image tmp = src.GetThumbnailImage(desiredWidth, desiredHeight, null, IntPtr.Zero); Size imgSize = new Size(desiredWidth, desiredHeight); Image tmp = createResizedImage(src, imgSize, type); try { File.Delete(destination); } catch (Exception) { } try { tmp.Save(destination, destination.ToImageFormat()); } catch { return(false); } finally { src.Dispose(); tmp.Dispose(); } return(true); }
public static List <Photo> ToModel(this IEnumerable <product_image> images, Imgsize thumbnailSize, Imgsize?bigSize = null) { var data = new List <Photo>(); foreach (var image in images) { var photo = new Photo(); photo.id = image.id; photo.url = Img.by_size(image.url, thumbnailSize); if (bigSize.HasValue) { photo.bigUrl = Img.by_size(image.url, bigSize.Value); } data.Add(photo); } return(data); }
private static Size getImageDimensionsFromSize(Imgsize size) { switch (size) { case Imgsize.PICO: return(new Size(16, 16)); case Imgsize.ICON: return(new Size(32, 32)); case Imgsize.THUMB: return(new Size(50, 50)); case Imgsize.SMALL: return(new Size(100, 100)); case Imgsize.COMPACT: return(new Size(160, 160)); case Imgsize.MEDIUM: return(new Size(240, 240)); case Imgsize.LARGE: return(new Size(480, 480)); case Imgsize.GRANDE: return(new Size(600, 600)); case Imgsize.ORIGINAL: return(new Size(1024, 1024)); case Imgsize.BANNER: return(new Size(880, 150)); default: throw new ArgumentException(); } }
private static Image createResizedImage(Image originalImage, Size poSize, Imgsize type) { //Detach image from its source Image oImageOriginal = (Image)originalImage.Clone(); //Resize new image var oResizedImage = new Bitmap(poSize.Width, poSize.Height); Graphics oGraphic = Graphics.FromImage(oResizedImage); oGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality; oGraphic.CompositingQuality = CompositingQuality.HighSpeed; oGraphic.SmoothingMode = SmoothingMode.HighSpeed; oGraphic.InterpolationMode = InterpolationMode.Low; Rectangle oRectangle = new Rectangle(0, 0, poSize.Width, poSize.Height); oGraphic.DrawImage(oImageOriginal, oRectangle); // cleanup oGraphic.Dispose(); oImageOriginal.Dispose(); return(oResizedImage); }
private static void convert(string source, string destination, int desiredWidth, int desiredHeight, Imgsize type) { createImage(source, destination, desiredWidth, desiredHeight, type); }
/// <summary> /// generates a thumbnail given the path of the original images /// </summary> /// <param name="filePath"></param> /// <param name="suffix"></param> /// <param name="desiredWidth"></param> /// <param name="desiredHeight"></param> /// <returns></returns> private static string thumbnail(string filePath, string suffix, float desiredWidth, float desiredHeight, Imgsize type) { string thumb = filePath; string file = filePath; string ext = thumb.Substring(thumb.LastIndexOf(".") + 1); thumb = thumb.Substring(0, thumb.IndexOf(".")) + "." + suffix + "." + ext; bool exists = System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + file); if (!exists) { //Syslog.Write(ErrorLevel.ERROR, string.Concat("Cannot find file: ", AppDomain.CurrentDomain.BaseDirectory + file)); return(""); } // These are the ratio calculations int width; int height; Image img = null; try { img = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + file); width = img.Width; height = img.Height; } catch (OutOfMemoryException) { Syslog.Write(ErrorLevel.ERROR, "Invalid image: " + filePath); return(""); } finally { if (img != null) { img.Dispose(); } } float factor = 0; if (width > 0 && height > 0) { float wfactor = desiredWidth / width; float hfactor = desiredHeight / height; factor = wfactor < hfactor ? wfactor : hfactor; } if (factor != 0 && factor < 1) { int twidth = Convert.ToInt32(Math.Floor(factor * width)); int theight = Convert.ToInt32(Math.Floor(factor * height)); convert(file, thumb, twidth, theight, type); } else { if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + thumb)) { System.IO.File.Delete(AppDomain.CurrentDomain.BaseDirectory + thumb); } System.IO.File.Copy(AppDomain.CurrentDomain.BaseDirectory + file, AppDomain.CurrentDomain.BaseDirectory + thumb); } return(thumb); }
/// <summary> /// generates a thumbnail given the path of the original images /// </summary> /// <param name="filePath"></param> /// <param name="suffix"></param> /// <param name="desiredWidth"></param> /// <param name="desiredHeight"></param> /// <returns></returns> private static string thumbnail(string filePath, string suffix, float desiredWidth, float desiredHeight, Imgsize type) { string thumb = filePath; string file = filePath; string ext = thumb.Substring(thumb.LastIndexOf(".") + 1); thumb = thumb.Substring(0, thumb.LastIndexOf(".")) + "_" + suffix + "." + ext; #if AZURE file = file.Substring(file.LastIndexOf("/") + 1); thumb = thumb.Substring(thumb.LastIndexOf("/") + 1); bool exists = photoContainer.DoesBlobItemExists(file); #else bool exists = File.Exists(GeneralConstants.APP_ROOT_DIR + file); #endif if (!exists) { // delete from db new Thread(() => { Syslog.Write(String.Concat("Cannot find file: ", GeneralConstants.APP_ROOT_DIR + file)); using (var db = new tradelrDataContext()) { var repository = new TradelrRepository(db); repository.SetIsolationToNoLock(); repository.DeleteImage(file); } }).Start(); return(""); } // These are the ratio calculations #if AZURE Image img = Image.FromStream(photoContainer.GetBlobContentStream(file)); #else Image img = Image.FromFile(GeneralConstants.APP_ROOT_DIR + file); #endif int width = img.Width; int height = img.Height; img.Dispose(); float factor = 0; if (width > 0 && height > 0) { float wfactor = desiredWidth / width; float hfactor = desiredHeight / height; factor = wfactor < hfactor ? wfactor : hfactor; } if (factor > 0) { int twidth = Convert.ToInt32(Math.Floor(factor * width)); int theight = Convert.ToInt32(Math.Floor(factor * height)); convert(file, thumb, twidth, theight, type); } else { #if AZURE photoContainer.DeleteBlobItem(thumb); photoContainer.CopyContent(file, thumb); #else if (File.Exists(GeneralConstants.APP_ROOT_DIR + thumb)) { File.Delete(GeneralConstants.APP_ROOT_DIR + thumb); } File.Copy(GeneralConstants.APP_ROOT_DIR + file, GeneralConstants.APP_ROOT_DIR + thumb); #endif } return(thumb); }
private static bool createImage(string srcName, string destName, int desiredWidth, int desiredHeight, Imgsize type) { var source = GeneralConstants.APP_ROOT_DIR + srcName; var destination = GeneralConstants.APP_ROOT_DIR + destName; // Capture the original size of the uploaded image Image src = null; try { #if AZURE src = Image.FromStream(photoContainer.GetBlobContentStream(srcName)); #else src = Image.FromFile(source); #endif } catch (Exception ex) { if (src != null) { src.Dispose(); } Syslog.Write(ex); throw; } //Resize new image //Image tmp = src.GetThumbnailImage(desiredWidth, desiredHeight, null, IntPtr.Zero); Size imgSize = new Size(desiredWidth, desiredHeight); Image tmp = createResizedImage(src, imgSize, type); try { File.Delete(destination); } catch (Exception ex) { Syslog.Write(ex); } try { #if AZURE var ms = new MemoryStream(); tmp.Save(ms, destName.ToImageFormat()); ms.Seek(0, SeekOrigin.Begin); photoContainer.AddBlobItem(destName, ms); #else tmp.Save(destination, destination.ToImageFormat()); #endif } catch (Exception ex) { Syslog.Write(ex); Syslog.Write(destination); return(false); } finally { src.Dispose(); tmp.Dispose(); } return(true); }
public static Contact ToModel(this user value, long?sessionid, long subdomainid, Imgsize imgsize = Imgsize.MEDIUM) { Debug.Assert(value.organisation.HasValue); var contact = new Contact() { isOwner = value.id == sessionid, isPrivate = value.organisation1.subdomain == subdomainid, id = value.id, email = value.email, fbuserid = value.FBID, firstName = value.firstName, lastName = value.lastName, fullName = value.ToEmailName(true), gender = value.gender, notes = value.notes, phone = value.phoneNumber, title = value.title, profilePhoto = value.ToProfilePhoto(imgsize), contactTypeLink = value.ToContactTypeLink(), orgid = value.organisation.HasValue?value.organisation.Value.ToString():"", companyName = value.organisation1.name, address = value.organisation1.address, city = value.organisation1.city.HasValue ? value.organisation1.MASTERcity.name : "", coPhone = value.organisation1.phone, country = value.organisation1.country, countryName = value.organisation1.country.HasValue ? Country.GetCountry(value.organisation1.country.Value).name : "", fax = value.organisation1.fax, postcode = value.organisation1.postcode, companyLogo = value.organisation1.logo.HasValue ? value.organisation1.image.ToModel(imgsize) : null, mapData = new GoogleMapData() { latitude = value.organisation1.latitude.HasValue ? value.organisation1.latitude.Value : 0, longtitude = value.organisation1.longtitude.HasValue ? value.organisation1.longtitude.Value : 0, mapZoom = value.organisation1.zoom.HasValue ? value.organisation1.zoom.Value : 0 }, state = value.organisation1.state, timezone = value.timezone, currency = value.organisation1.MASTERsubdomain.currency.ToString(), domainFlags = (SubdomainFlags)value.organisation1.MASTERsubdomain.flags, isFbConnected = !string.IsNullOrEmpty(value.FBID), shippingAddress = value.organisation1.shippingAddressID.HasValue? value.organisation1.address1.ToHtmlString():"", billingAddress = value.organisation1.billingAddressID.HasValue ? value.organisation1.address2.ToHtmlString():"", role = (UserRole)value.role }; if (value.permissions.HasValue) { contact.permissions = (UserPermission)value.permissions.Value; } else { if ((contact.role & UserRole.CREATOR) == UserRole.CREATOR) { contact.permissions = UserPermission.ADMIN; } else { contact.permissions = UserPermission.USER; } } return(contact); }
public static IEnumerable <Photo> ToModel(this IEnumerable <blog_image> images, Imgsize size, Imgsize fullsize = Imgsize.MAX) { foreach (var image in images) { yield return(new Photo() { id = image.id, url = Img.by_size(image.url, size), url_fullsize = Img.by_size(image.url, fullsize) }); } }
public static IEnumerable <Photo> ToModel(this IQueryable <user_image> images, Imgsize size, Imgsize fullsize = Imgsize.MAX) { foreach (var image in images) { yield return(image.ToModel(size, fullsize)); } }