protected string GetValueText() { var dalc = WebManager.GetService <IDalc>(DalcServiceName); var relexParser = new RelExQueryParser(false); var exprResolver = WebManager.GetService <NI.Common.Expressions.IExpressionResolver>("defaultExprResolver"); var qContext = new Hashtable(); if (DataContext is IDictionary) { qContext = new Hashtable((IDictionary)DataContext); } qContext["q"] = String.Empty; Query q = (Query)relexParser.Parse(Convert.ToString(exprResolver.Evaluate(qContext, GetRelex()))); q.Root = new QueryConditionNode((QField)ValueFieldName, Conditions.Equal, (QConst)Value); //& q.Root; var data = new Hashtable(); if (dalc.LoadRecord(data, q)) { var text = Convert.ToString(data[TextFieldName]); return(LocalizationEnabled?WebManager.GetLabel(text, this):text); } else { Value = null; return(""); } }
public static void CropImage(Stream input, Stream output, float relStartX, float relStartY, float relEndX, float relEndY, ImageFormat saveAsFormat) { Image img = null; try { img = Image.FromStream(input); } catch (Exception ex) { throw new Exception(WebManager.GetLabel("Invalid image format")); } var absStartX = (int)(relStartX * img.Size.Width); var absStartY = (int)(relStartY * img.Size.Height); var absEndX = (int)(relEndX * img.Size.Width); var absEndY = (int)(relEndY * img.Size.Height); var resizedBitmap = new Bitmap(absEndX - absStartX, absEndY - absStartY); using (Graphics g = Graphics.FromImage(resizedBitmap)) { g.DrawImage(img, new Rectangle(0, 0, resizedBitmap.Width, resizedBitmap.Height), new Rectangle(absStartX, absStartY, resizedBitmap.Width, resizedBitmap.Height), GraphicsUnit.Pixel); } resizedBitmap.Save(output, saveAsFormat ?? ImageFormat.Png); }
public static IFileObject SaveAndResizeImage(Stream input, IFileSystem fs, IFileObject file, int maxWidth, int maxHeight, ImageFormat saveAsFormat) { Image img; MemoryStream imgSrcStream = new MemoryStream(); byte[] buf = new byte[1024 * 50]; int bufRead = 0; do { bufRead = input.Read(buf, 0, buf.Length); if (bufRead > 0) { imgSrcStream.Write(buf, 0, bufRead); } } while (bufRead > 0); imgSrcStream.Position = 0; try { img = Image.FromStream(imgSrcStream); } catch (Exception ex) { throw new Exception(WebManager.GetLabel("Invalid image format")); } if (img.Size.Width == 0 || img.Size.Height == 0) { throw new Exception(WebManager.GetLabel("Invalid image size")); } var sizeIsWidthOk = (maxWidth <= 0 || img.Size.Width <= maxWidth); var sizeIsHeightOk = (maxHeight <= 0 || img.Size.Height <= maxHeight); var sizeIsOk = sizeIsWidthOk && sizeIsHeightOk; var originalImgFmt = ResolveImageFormat(Path.GetExtension(file.Name)) ?? ImageFormat.Bmp; var formatIsOk = (saveAsFormat == null && !originalImgFmt.Equals(ImageFormat.Bmp) && !originalImgFmt.Equals(ImageFormat.Tiff)) || originalImgFmt.Equals(saveAsFormat); if (!formatIsOk || !sizeIsOk) { var saveAsFormatResolved = saveAsFormat != null ? saveAsFormat : (originalImgFmt == ImageFormat.Jpeg?ImageFormat.Jpeg:ImageFormat.Png); var newFmtExtension = GetImageFormatExtension(saveAsFormatResolved); var newFile = fs.ResolveFile(file.Name + (Path.GetExtension(file.Name).ToLower() == newFmtExtension ? String.Empty : newFmtExtension)); newFile.CreateFile(); if (!sizeIsOk) { var newWidth = img.Size.Width; var newHeight = img.Size.Height; if (!sizeIsWidthOk) { newWidth = maxWidth; newHeight = (int)Math.Floor(((double)img.Size.Height) * (((double)maxWidth) / ((double)img.Size.Width))); if (maxHeight < 0 || newHeight <= maxHeight) { sizeIsHeightOk = true; } } if (!sizeIsHeightOk) { newHeight = maxHeight; newWidth = (int)Math.Floor(((double)img.Size.Width) * (((double)maxHeight) / ((double)img.Size.Height))); } var resizedBitmap = new Bitmap(img, newWidth, newHeight); var imageProps = img.PropertyItems; foreach (PropertyItem propItem in imageProps) { resizedBitmap.SetPropertyItem(propItem); } SaveImage(resizedBitmap, newFile.GetContent().OutputStream, saveAsFormatResolved); } else { SaveImage(img, newFile.GetContent().OutputStream, saveAsFormatResolved); } newFile.Close(); return(newFile); } file.CreateFile(); imgSrcStream.Position = 0; file.CopyFrom(imgSrcStream); file.Close(); return(file); }
/// <summary> /// Resolves label for context control. /// </summary> public static string GetLabel(this Control ctrl, string label) { return(WebManager.GetLabel(label, ctrl)); }