public ImageView upload()
        {
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];
            HttpRequestBase request = context.Request;



            byte[]       imageBytes = Convert.FromBase64String(request["base64"].ToString().Split(',')[1]);
            MemoryStream ms         = new System.IO.MemoryStream(imageBytes);
            Image        image      = Image.FromStream(ms);


            string date = DateTime.Now.ToString("yyyyMMdd");
            string dir  = Path.Combine(ConfigurationManager.AppSettings["UploadPath"], date);
            string guid = Guid.NewGuid().ToString("N");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            string filename = guid + ".jpeg";
            string path     = Path.Combine(dir, filename);

            image.Save(path);

            ImageResizeOptions imageResizeOptions = new ImageResizeOptions();

            if (context.Request["type"] == "touxiang")
            {
                imageResizeOptions.cut    = true;
                imageResizeOptions.Width  = 200;
                imageResizeOptions.Height = 200;
            }
            else if (context.Request["type"] == "tiyanshi")
            {
                imageResizeOptions.cut    = true;
                imageResizeOptions.Width  = 600;
                imageResizeOptions.Height = 700;
            }
            else
            {
                imageResizeOptions.cut    = false;
                imageResizeOptions.Width  = 600;
                imageResizeOptions.Height = 0;
            }
            string    src       = date + "/" + ImageManager.ResizeImage(path, imageResizeOptions);
            ImageView imageView = new ImageView()
            {
                src    = src,
                fileID = request["fileID"],
                url    = ConfigurationManager.AppSettings["UploadUrl"]
            };

            return(imageView);
        }
Пример #2
0
        /// <summary>
        /// Resize style
        /// </summary>
        /// <param name="name"> Style name </param>
        /// <param name="resizeWidth"> Max width (0 for auto) </param>
        /// <param name="resizeHeight"> Max height (0 for auto) </param>
        /// <param name="resizeMode"> Resize mode </param>
        public HasImageStyleAttribute(
            string name,
            int resizeWidth,
            int resizeHeight,
            ImageResizeMode resizeMode = ImageResizeMode.PreserveAspectRatio,
            Resampler resampler        = Resampler.Auto,
            int quality = 100) : base(name)
        {
            var resizeOptions = new ImageResizeOptions
            {
                Width  = resizeWidth,
                Height = resizeHeight,
                Mode   = resizeMode
            };

            var decodeOptions = new ImageEncodeOptions
            {
                Quality = quality
            };

            Style = new ImageStyle(name, resizeOptions, decodeOptions);
        }
Пример #3
0
        private ResizeOptions ToResizeOptions(Image <Rgba32> image, ImageResizeOptions resizeOptions)
        {
            ResizeMode mode;

            switch (resizeOptions.Mode)
            {
            case ImageResizeMode.PreserveAspectRatio: mode = ResizeMode.Max; break;

            case ImageResizeMode.IgnoreAspectRatio: mode = ResizeMode.Stretch; break;

            case ImageResizeMode.ShrinkLarger: mode = ResizeMode.Min; break;

            case ImageResizeMode.Fill: mode = ResizeMode.Min; break;

            default: throw new NotImplementedException();
            }

            var resizeOptionsResult = new ResizeOptions
            {
                Size = new Size(resizeOptions.Width, resizeOptions.Height),
                Mode = mode
            };

            switch (resizeOptions.Resampler)
            {
            // https://www.imagemagick.org/discourse-server/viewtopic.php?t=15742
            case Resampler.Auto:
            {
                if (image.Width < resizeOptions.Width)
                {
                    resizeOptionsResult.Sampler = new MitchellNetravaliResampler();
                    break;
                }

                // TODO split
                resizeOptionsResult.Sampler = new Lanczos2Resampler();
                break;
            }

            case Resampler.Bicubic:
                resizeOptionsResult.Sampler = new BicubicResampler();
                break;

            case Resampler.Box:
                resizeOptionsResult.Sampler = new BoxResampler();
                break;

            case Resampler.CatmullRom:
                resizeOptionsResult.Sampler = new CatmullRomResampler();
                break;

            case Resampler.Hermite:
                resizeOptionsResult.Sampler = new HermiteResampler();
                break;

            case Resampler.MitchellNetravali:
                resizeOptionsResult.Sampler = new MitchellNetravaliResampler();
                break;

            case Resampler.NearestNeighbor:
                resizeOptionsResult.Sampler = new NearestNeighborResampler();
                break;

            case Resampler.Robidoux:
                resizeOptionsResult.Sampler = new RobidouxResampler();
                break;

            case Resampler.RobidouxSharp:
                resizeOptionsResult.Sampler = new RobidouxSharpResampler();
                break;

            case Resampler.Spline:
                resizeOptionsResult.Sampler = new SplineResampler();
                break;

            case Resampler.Lanczos2:
                resizeOptionsResult.Sampler = new Lanczos2Resampler();
                break;

            case Resampler.Lanczos3:
                resizeOptionsResult.Sampler = new Lanczos3Resampler();
                break;

            case Resampler.Lanczos5:
                resizeOptionsResult.Sampler = new Lanczos5Resampler();
                break;

            case Resampler.Lanczos8:
                resizeOptionsResult.Sampler = new Lanczos8Resampler();
                break;

            case Resampler.Triangle:
                resizeOptionsResult.Sampler = new TriangleResampler();
                break;

            case Resampler.Welch:
                resizeOptionsResult.Sampler = new WelchResampler();
                break;

            default:
                throw new NotImplementedException($"'{resizeOptions.Resampler}' Resampler is not implemented");
            }

            return(resizeOptionsResult);
        }
    public override void Process()
    {
        byte[] uploadFileBytes = null;
        string uploadFileName  = null;

        if (UploadConfig.Base64)
        {
            uploadFileName  = UploadConfig.Base64Filename;
            uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
        }
        else
        {
            var file = Request.Files[UploadConfig.UploadFieldName];
            uploadFileName = file.FileName;

            if (!CheckFileType(uploadFileName))
            {
                Result.State = UploadState.TypeNotAllow;
                WriteResult();
                return;
            }
            if (!CheckFileSize(file.ContentLength))
            {
                Result.State = UploadState.SizeLimitExceed;
                WriteResult();
                return;
            }

            uploadFileBytes = new byte[file.ContentLength];
            try
            {
                file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
            }
            catch (Exception)
            {
                Result.State = UploadState.NetworkError;
                WriteResult();
            }
        }

        Result.OriginFileName = uploadFileName;

        var    savePath  = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
        var    localPath = Server.MapPath(savePath);
        string date      = DateTime.Now.ToString("yyyyMMdd");
        string dir       = Path.Combine(ConfigurationManager.AppSettings["UploadPath"], date);

        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        localPath = Path.Combine(dir, uploadFileName);
        try
        {
            if (!Directory.Exists(Path.GetDirectoryName(localPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(localPath));
            }
            File.WriteAllBytes(localPath, uploadFileBytes);
            if (Request["action"] == "uploadimage")
            {
                ImageResizeOptions imageResizeOptions = new ImageResizeOptions();

                imageResizeOptions.cut    = false;
                imageResizeOptions.Width  = 640;
                imageResizeOptions.Height = 0;

                Result.Url   = ConfigurationManager.AppSettings["UploadUrl"] + "/" + date + "/" + ImageManager.ResizeImage(localPath, imageResizeOptions);
                Result.State = UploadState.Success;
            }
            else
            {
                Result.Url   = ConfigurationManager.AppSettings["UploadUrl"] + date + "/" + uploadFileName;
                Result.State = UploadState.Success;
            }
        }
        catch (Exception e)
        {
            Result.State        = UploadState.FileAccessError;
            Result.ErrorMessage = e.Message;
        }
        finally
        {
            WriteResult();
        }
    }