Exemplo n.º 1
0
        internal WebImage Save(HttpContextBase context, Action <string, byte[]> saveAction, string filePath, string imageFormat, bool forceWellKnownExtension)
        {
            filePath = filePath ?? FileName;
            if (String.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath", CommonResources.Argument_Cannot_Be_Null_Or_Empty);
            }

            // GetBytes takes care of executing pending transformations.
            // todo: this could be made more efficient by avoiding cloning array
            // when format is same
            byte[] content = GetBytes(imageFormat);
            if (forceWellKnownExtension)
            {
                ImageFormat saveImageFormat;
                ImageFormat requestedImageFormat = String.IsNullOrEmpty(imageFormat) ? _initialFormat : GetImageFormat(imageFormat);
                var         extension            = Path.GetExtension(filePath).TrimStart('.');
                // TryFromStringToImageFormat accepts mime types and image names. For images supported by System.Drawing.Imaging, the image name maps to the extension.
                // Replace the extension with the current format in the following two events:
                //  * The extension format cannot be converted to a known format
                //  * The format does not match.
                if (!ConversionUtil.TryFromStringToImageFormat(extension, out saveImageFormat) || !saveImageFormat.Equals(requestedImageFormat))
                {
                    extension = requestedImageFormat.ToString().ToLowerInvariant();
                    filePath  = filePath + "." + extension;
                }
            }
            saveAction(VirtualPathUtil.MapPath(context, filePath), content);
            // Update the FileName since it may have changed whilst saving.
            FileName = filePath;
            return(this);
        }
Exemplo n.º 2
0
        internal static WebImage GetImageFromRequest(HttpRequestBase request, string postedFileName = null)
        {
            Debug.Assert(request != null);
            if ((request.Files == null) || (request.Files.Count == 0))
            {
                return(null);
            }
            HttpPostedFileBase file = String.IsNullOrEmpty(postedFileName) ? request.Files[0] : request.Files[postedFileName];

            if (file == null || file.ContentLength < 1)
            {
                return(null);
            }

            // The content type is specified by the browser and is unreliable.
            // Disregard content type, acquire mime type.
            ImageFormat format;
            string      mimeType = MimeMapping.GetMimeMapping(file.FileName);

            if (!ConversionUtil.TryFromStringToImageFormat(mimeType, out format))
            {
                // Unsupported image format.
                return(null);
            }

            WebImage webImage = new WebImage(file.InputStream);

            webImage.FileName = file.FileName;
            return(webImage);
        }
Exemplo n.º 3
0
        public static void Send(string to,
                                string subject,
                                string body,
                                string from = null,
                                string cc   = null,
                                IEnumerable <string> filesToAttach = null,
                                bool isBodyHtml = true,
                                IEnumerable <string> additionalHeaders = null,
                                string bcc             = null,
                                string contentEncoding = null,
                                string headerEncoding  = null,
                                string priority        = null,
                                string replyTo         = null)
        {
            if (filesToAttach != null)
            {
                foreach (string fileName in filesToAttach)
                {
                    if (String.IsNullOrEmpty(fileName))
                    {
                        throw new ArgumentException(HelpersResources.WebMail_ItemInCollectionIsNull, "filesToAttach");
                    }
                }
            }

            if (additionalHeaders != null)
            {
                foreach (string header in additionalHeaders)
                {
                    if (String.IsNullOrEmpty(header))
                    {
                        throw new ArgumentException(HelpersResources.WebMail_ItemInCollectionIsNull, "additionalHeaders");
                    }
                }
            }

            MailPriority priorityValue = MailPriority.Normal;

            if (!String.IsNullOrEmpty(priority) && !ConversionUtil.TryFromStringToEnum(priority, out priorityValue))
            {
                throw new ArgumentException(HelpersResources.WebMail_InvalidPriority, "priority");
            }

            if (String.IsNullOrEmpty(SmtpServer))
            {
                throw new InvalidOperationException(HelpersResources.WebMail_SmtpServerNotSpecified);
            }

            using (MailMessage message = new MailMessage())
            {
                SetPropertiesOnMessage(message, to, subject, body, from, cc, bcc, replyTo, contentEncoding, headerEncoding, priorityValue,
                                       filesToAttach, isBodyHtml, additionalHeaders);
                using (SmtpClient client = new SmtpClient())
                {
                    SetPropertiesOnClient(client);
                    client.Send(message);
                }
            }
        }
Exemplo n.º 4
0
        private static void SetPriority(MailMessage message, string priority)
        {
            MailPriority priorityValue;

            if (!String.IsNullOrEmpty(priority) && ConversionUtil.TryFromStringToEnum(priority, out priorityValue))
            {
                // If we can parse it, set it. Do nothing otherwise
                message.Priority = priorityValue;
            }
        }
Exemplo n.º 5
0
        private static T ConvertStringArgument <T>(string paramName, string value)
        {
            object result;

            if (!ConversionUtil.TryFromString(typeof(T), value, out result))
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
                                                          HelpersResources.Chart_ArgumentConversionFailed, typeof(T).FullName), paramName);
            }
            return((T)result);
        }
Exemplo n.º 6
0
        private static VerticalAlign ParseVerticalAlign(string alignment)
        {
            bool          conversionOk;
            VerticalAlign verticalAlign;

            conversionOk = ConversionUtil.TryFromStringToEnum(alignment, out verticalAlign);
            if (!conversionOk || (verticalAlign == VerticalAlign.NotSet))
            {
                throw new ArgumentException(HelpersResources.WebImage_IncorrectVerticalAlignment);
            }
            return(verticalAlign);
        }
Exemplo n.º 7
0
        private static HorizontalAlign ParseHorizontalAlign(string alignment)
        {
            bool            conversionOk;
            HorizontalAlign horizontalAlign;

            conversionOk = ConversionUtil.TryFromStringToEnum(alignment, out horizontalAlign);
            if (!conversionOk || (horizontalAlign == HorizontalAlign.Justify) || (horizontalAlign == HorizontalAlign.NotSet))
            {
                throw new ArgumentException(HelpersResources.WebImage_IncorrectHorizontalAlignment);
            }
            return(horizontalAlign);
        }
Exemplo n.º 8
0
 private static string NormalizeFormat(string format)
 {
     if (String.IsNullOrEmpty(format))
     {
         throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "format");
     }
     if (format.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
     {
         format = format.Substring(6);
     }
     return(ConversionUtil.NormalizeImageFormat(format));
 }
Exemplo n.º 9
0
        /// <summary>
        /// Method to convert a string to a ChartImageFormat.
        /// The chart image needs to be normalized to allow for alternate names such as 'jpg', 'xpng' etc
        /// to be mapped to their appropriate ChartImageFormat.
        /// </summary>
        private static ChartImageFormat ConvertStringToChartImageFormat(string format)
        {
            object result;

            format = NormalizeFormat(format);
            if (!ConversionUtil.TryFromString(typeof(ChartImageFormat), format, out result))
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
                                                          HelpersResources.Image_IncorrectImageFormat, format), "format");
            }
            return((ChartImageFormat)result);
        }
Exemplo n.º 10
0
        private static ImageFormat GetImageFormat(string format)
        {
            Debug.Assert(!String.IsNullOrEmpty(format), "format cannot be null");

            ImageFormat result;

            if (!ConversionUtil.TryFromStringToImageFormat(format, out result))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, HelpersResources.Image_IncorrectImageFormat, format), "format");
            }

            return(result);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Adds text watermark to a WebImage.
        /// </summary>
        /// <param name="text">Text to use as a watermark.</param>
        /// <param name="fontColor">Watermark color. Can be specified as a string (e.g. "White") or as a hex value (e.g. "#00FF00").</param>
        /// <param name="fontSize">Font size in points.</param>
        /// <param name="fontStyle">Font style: bold, italics, etc.</param>
        /// <param name="fontFamily">Font family name: e.g. Microsoft Sans Serif</param>
        /// <param name="horizontalAlign">Horizontal alignment for watermark text. Can be "right", "left", or "center".</param>
        /// <param name="verticalAlign">Vertical alignment for watermark text. Can be "top", "bottom", or "middle".</param>
        /// <param name="opacity">Watermark text opacity. Should be between 0 and 100.</param>
        /// <param name="padding">Size of padding around watermark text in pixels.</param>
        /// <returns>Modified WebImage instance with added watermark.</returns>
        public WebImage AddTextWatermark(
            string text,
            string fontColor       = "Black",
            int fontSize           = 12,
            string fontStyle       = "Regular",
            string fontFamily      = "Microsoft Sans Serif",
            string horizontalAlign = "Right",
            string verticalAlign   = "Bottom",
            int opacity            = 100,
            int padding            = 5)
        {
            if (String.IsNullOrEmpty(text))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "text");
            }

            Color color;

            if (!ConversionUtil.TryFromStringToColor(fontColor, out color))
            {
                throw new ArgumentException(HelpersResources.WebImage_IncorrectColorName);
            }

            if ((opacity < 0) || (opacity > 100))
            {
                throw new ArgumentOutOfRangeException("opacity", String.Format(CultureInfo.InvariantCulture, CommonResources.Argument_Must_Be_Between, 0, 100));
            }

            int alpha = 255 * opacity / 100;

            color = Color.FromArgb(alpha, color);

            if (fontSize <= 0)
            {
                throw new ArgumentOutOfRangeException(
                          "fontSize",
                          String.Format(CultureInfo.InvariantCulture, CommonResources.Argument_Must_Be_GreaterThan, 0));
            }

            FontStyle fontStyleEnum;

            if (!ConversionUtil.TryFromStringToEnum(fontStyle, out fontStyleEnum))
            {
                throw new ArgumentException(HelpersResources.WebImage_IncorrectFontStyle);
            }

            FontFamily fontFamilyClass;

            if (!ConversionUtil.TryFromStringToFontFamily(fontFamily, out fontFamilyClass))
            {
                throw new ArgumentException(HelpersResources.WebImage_IncorrectFontFamily);
            }

            HorizontalAlign horizontalAlignEnum = ParseHorizontalAlign(horizontalAlign);
            VerticalAlign   verticalAlignEnum   = ParseVerticalAlign(verticalAlign);

            if (padding < 0)
            {
                throw new ArgumentOutOfRangeException(
                          "padding",
                          String.Format(CultureInfo.InvariantCulture, CommonResources.Argument_Must_Be_GreaterThanOrEqualTo, 0));
            }

            WatermarkTextTransformation transformation =
                new WatermarkTextTransformation(text, color, fontSize, fontStyleEnum, fontFamilyClass, horizontalAlignEnum, verticalAlignEnum, padding);

            _transformations.Add(transformation);
            return(this);
        }