예제 #1
0
        public byte[] Transform(byte[] img, TransformationParametrs parametrs)
        {
            byte[]    result;
            Rectangle cropRectangle = new Rectangle(parametrs.TopLeftConerX, parametrs.TopLeftConerY, parametrs.Width, parametrs.Height);

            if (cropRectangle.Width == 0 || cropRectangle.Height == 0)
            {
                throw new ArgumentOutOfRangeException("Пустая область");
            }
            using (MemoryStream outStream = new MemoryStream())
            {
                // Initialize the ImageFactory using the overload to preserve EXIF metadata.
                using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))
                {
                    // Load, resize, set the format and quality and save an image.
                    imageFactory.Load(img);
                    if (imageFactory.Image.Width > 1000 || imageFactory.Image.Height > 1000)
                    {
                        throw new ArgumentException("Слишком большое изображение");
                    }
                    if (parametrs.Flip != Flip.None)
                    {
                        if (parametrs.Flip == Flip.Horizontal)
                        {
                            imageFactory.Flip(false);
                        }
                        else
                        {
                            imageFactory.Flip(true);
                        }
                    }
                    if (parametrs.Rotation != Rotation.None)
                    {
                        if (parametrs.Rotation == Rotation.Clockwise)
                        {
                            imageFactory.Rotate(90);
                        }
                        else
                        {
                            imageFactory.Rotate(-90);
                        }
                    }
                    try
                    {
                        imageFactory.Crop(cropRectangle);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        throw new ArgumentOutOfRangeException("Пустая область");
                    }
                    imageFactory.Save(outStream);
                }

                // Do something with the stream.
                result = outStream.ToArray();
            }

            return(result);
        }
예제 #2
0
        private Tuple <HttpListenerContext, byte[]> ProcessImage(HttpListenerContext httpContext)
        {
            string paramString = httpContext.Request.RawUrl;

            byte[] inputImg;
            byte[] outputImg;
            paramString = paramString.Remove(0, 9);
            paramString = paramString.Replace('/', ' ');
            TransformationParametrs tParams;

            try
            {
                MemoryStream inStream = new MemoryStream();
                httpContext.Request.InputStream.CopyTo(inStream);
                inputImg = inStream.ToArray();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw new Exception(ex.Message);
            }
            if (inputImg.LongLength > 100000)// проверка размера файла
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(new Tuple <HttpListenerContext, byte[]>(httpContext, null));
            }
            try
            {
                tParams = TransformationParametrs.Parse(paramString);
            }
            catch (ArgumentException ae)
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(new Tuple <HttpListenerContext, byte[]>(httpContext, null));
            }

            ImageTransformer.Transformer.ImageTransformer transformer = new ImageTransformer.Transformer.ImageTransformer();

            try
            {
                outputImg = transformer.Transform(inputImg, tParams);
            }
            catch (ArgumentOutOfRangeException aoorEx)
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.NoContent;
                return(new Tuple <HttpListenerContext, byte[]>(httpContext, null));
            }
            catch (ArgumentException argEx)
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(new Tuple <HttpListenerContext, byte[]>(httpContext, null));
            }

            httpContext.Response.StatusCode = (int)HttpStatusCode.OK;
            return(new Tuple <HttpListenerContext, byte[]>(httpContext, outputImg));
        }