Example #1
0
        /// <summary>
        /// Checks the name of the user.
        /// </summary>
        /// <param name="entry">The entry.</param>
        /// <param name="userName">Name of the user.</param>
        /// <returns>
        ///   <c>true</c> if user name is existed, <c>false</c> otherwise.</returns>
        public static bool CheckUserName(this DirectoryEntry entry, string userName)
        {
            try
            {
                entry.CheckNullObject("entry");
                userName.CheckEmptyString("userName");

                var directorySearcher = new DirectorySearcher()
                {
                    SearchRoot = entry,
                    Filter = "(&(objectClass=user) (cn=" + userName + "))"
                };

                if ((directorySearcher.FindAll()?.Count ?? 0) > 0)
                {
                    return true;
                };

                return false;
            }
            catch (Exception ex)
            {
                throw ex.Handle( new { userName });
            }
        }
        /// <summary>
        /// Gets the bytes.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="imageFormat">The image format.</param>
        /// <param name="qualityLevel">The quality level.
        /// <remarks>
        /// A quality level of 0 corresponds to the greatest compression, and a quality level of 100 corresponds to the least compression.
        /// https://msdn.microsoft.com/library/bb882583(v=vs.110).aspx
        /// </remarks></param>
        /// <returns>System.Byte[].</returns>
        /// <exception cref="OperationFailureException">GetBytes</exception>
        public static byte[] GetBytes(this Bitmap image, ImageFormat imageFormat = null, long qualityLevel = 100)
        {
            try
            {
                image.CheckNullObject("image");

                if (imageFormat == null)
                {
                    imageFormat = ImageFormat.Jpeg;
                }

                if (qualityLevel > 100 || qualityLevel < 1)
                {
                    qualityLevel = 100;
                }

                using (var memoryStream = new MemoryStream())
                {
                    var jpegEncoder = GetImageEncoder(imageFormat);
                    var encoder = Encoder.Quality;
                    var encoderParameters = new EncoderParameters(1);

                    var myEncoderParameter = new EncoderParameter(encoder, qualityLevel);
                    encoderParameters.Param[0] = myEncoderParameter;
                    image.Save(memoryStream, jpegEncoder, encoderParameters);

                    return memoryStream.ToBytes();
                }
            }
            catch (Exception ex)
            {
                throw ex.Handle();
            }
        }
Example #3
0
        /// <summary>
        /// To the stream.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <returns>The <see cref="Stream" />  for byte array.</returns>
        public static Stream ToStream(this byte[] bytes)
        {
            try
            {
                bytes.CheckNullObject("bytes");

                return new MemoryStream(bytes);
            }
            catch (Exception ex)
            {
                throw ex.Handle();
            }
        }
Example #4
0
        /// <summary>
        /// Reads the stream to bytes.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="closeWhenFinish">if set to <c>true</c> [close when finish].</param>
        /// <returns>System.Byte[][].</returns>
        public static byte[] ReadStreamToBytes(this Stream stream, bool closeWhenFinish = false)
        {
            long originalPosition = 0;

            try
            {
                stream.CheckNullObject("stream");

                if (stream.CanSeek)
                {
                    originalPosition = stream.Position;
                    stream.Position = 0;
                }

                byte[] readBuffer = new byte[4096];

                int totalBytesRead = 0;
                int bytesRead;

                while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
                {
                    totalBytesRead += bytesRead;

                    if (totalBytesRead == readBuffer.Length)
                    {
                        int nextByte = stream.ReadByte();
                        if (nextByte != -1)
                        {
                            byte[] temp = new byte[readBuffer.Length * 2];
                            Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                            Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                            readBuffer = temp;
                            totalBytesRead++;
                        }
                    }
                }

                byte[] buffer = readBuffer;
                if (readBuffer.Length != totalBytesRead)
                {
                    buffer = new byte[totalBytesRead];
                    Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
                }
                return buffer;
            }
            finally
            {
                if (stream != null)
                {
                    if (stream.CanSeek)
                    {
                        stream.Position = originalPosition;
                    }

                    if (closeWhenFinish)
                    {
                        stream.Close();
                        stream.Dispose();
                    }
                }
            }
        }
        /// <summary>
        /// Resizes the specified image.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="maxWidth">The maximum width.</param>
        /// <param name="maxHeight">The maximum height.</param>
        /// <param name="keepRatio">if set to <c>true</c> [keep ratio].</param>
        /// <returns>Bitmap.</returns>
        public static Bitmap Resize(this Bitmap image, int maxWidth, int maxHeight, bool keepRatio = true)
        {
            Bitmap bmpOut = null;

            try
            {
                image.CheckNullObject("image");

                if (image.Width < maxWidth && image.Height < maxHeight)
                {
                    bmpOut = image;
                }
                else
                {
                    var newWidth = 0;
                    var newHeight = 0;

                    if (keepRatio)
                    {
                        decimal ratio;

                        if (image.Width > image.Height)
                        {
                            ratio = (decimal)maxWidth / image.Width;
                            newWidth = maxWidth;
                            var lnTemp = image.Height * ratio;
                            newHeight = (int)lnTemp;
                        }
                        else
                        {
                            ratio = (decimal)maxHeight / image.Height;
                            newHeight = maxHeight;
                            var lnTemp = image.Width * ratio;
                            newWidth = (int)lnTemp;
                        }
                    }
                    else
                    {
                        newWidth = (image.Width > maxWidth) ? maxWidth : image.Width;
                        newHeight = (image.Height > maxHeight) ? maxHeight : image.Height;
                    }

                    bmpOut = new Bitmap(newWidth, newHeight);
                    var g = Graphics.FromImage(bmpOut);
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    g.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
                    g.DrawImage(image, 0, 0, newWidth, newHeight);

                    image.Dispose();
                }
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }
        /// <summary>
        /// Gets the route information.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="version">The version.</param>
        /// <param name="parameter1">The parameter1.</param>
        /// <param name="parameter2">The parameter2.</param>
        /// <returns><c>true</c> if succeed to match, <c>false</c> otherwise.</returns>
        internal static bool GetRouteInfo(this HttpRequest request, out string resourceName, out string version, out string parameter1, out string parameter2)
        {
            request.CheckNullObject("request");

            var match = methodMatch.Match(request.Url.PathAndQuery);

            resourceName = match.Success ? match.Result("${resource}") : string.Empty;
            version = match.Success ? match.Result("${version}") : string.Empty;
            parameter1 = match.Success ? match.Result("${parameter1}") : string.Empty;
            parameter2 = match.Success ? match.Result("${parameter2}") : string.Empty;

            return match.Success;
        }
        /// <summary>
        /// Gets the proxy information.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="clientIdentifier">The client identifier.</param>
        /// <param name="destination">The destination.</param>
        /// <param name="httpMethod">The HTTP method.</param>
        /// <param name="body">The body.</param>
        /// <returns><c>true</c> if succeed to match, <c>false</c> otherwise.</returns>
        internal static bool GetProxyInfo(this HttpRequest request, out string clientIdentifier, out string destination, out string httpMethod, out string body)
        {
            request.CheckNullObject("request");

            var match = proxyMatch.Match(request.Url.PathAndQuery);

            clientIdentifier = match.Success ? match.Result("${client}") : string.Empty;
            destination = match.Success ? match.Result("${destination}") : string.Empty;
            httpMethod = request.HttpMethod;
            body = request.GetPostJson();

            return match.Success;
        }
        /// <summary>
        /// Fills the route information.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="context">The context.</param>
        /// <returns><c>true</c> if succeed to match url format and fill route information, <c>false</c> otherwise.</returns>
        internal static bool FillRouteInfo(this HttpRequest request, RuntimeContext context)
        {
            try
            {
                request.CheckNullObject("request");
                context.CheckNullObject("context");

                var match = methodMatch.Match(request.Url.PathAndQuery);

                context.ResourceName = match.Success ? match.Result("${resource}") : string.Empty;
                context.Version = match.Success ? match.Result("${version}") : string.Empty;
                context.Parameter1 = match.Success ? match.Result("${parameter1}") : string.Empty;
                context.Parameter2 = match.Success ? match.Result("${parameter2}") : string.Empty;

                return match.Success;
            }
            catch (Exception ex)
            {
                throw ex.Handle();
            }
        }