示例#1
0
        public IHttpActionResult createWatermark(UrlImage url)
        {
            RestClient client  = new RestClient(ConfigurationManager.AppSettings["baseApiUrl"].ToString());
            var        request = new RestRequest("api/watermark/createWatermark", Method.POST);

            request.AddObject(url);
            IRestResponse response = client.Execute(request);
            var           content  = response.Content;
            JObject       json     = JObject.Parse(content);

            return(Ok(json));
        }
示例#2
0
        public IHttpActionResult createWatermark(UrlImage url)
        {
            DefaultResponse def    = new DefaultResponse();
            string          domain = ConfigurationManager.AppSettings["domain"].ToString();

            string path1 = Path.Combine(HttpContext.Current.Server.MapPath(url.url1));
            string path2 = Path.Combine(HttpContext.Current.Server.MapPath(url.url2));

            Image imgPhoto = Image.FromFile(path2);
            int   phWidth  = imgPhoto.Width;
            int   phHeight = imgPhoto.Height;

            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);

            Image imgWatermark = new Bitmap(path1);
            int   wmWidth      = imgWatermark.Width;
            int   wmHeight     = imgWatermark.Height;

            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

            grPhoto.DrawImage(
                imgPhoto,
                new Rectangle(0, 0, phWidth, phHeight),
                0,
                0,
                wmWidth,
                phHeight,
                GraphicsUnit.Pixel);

            Bitmap bmWatermark = new Bitmap(bmPhoto);

            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grWatermark = Graphics.FromImage(bmWatermark);

            ImageAttributes imageAttributes = new ImageAttributes();

            ColorMap colorMap = new ColorMap();

            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

            ColorMap[] remapTable = { colorMap };

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

            float[][] colorMatrixElements =
            {
                new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 0.0f, 0.0f, 0.3f, 0.0f },
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
            };
            ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

            imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
                                           ColorAdjustType.Bitmap);

            int xPosOfWm = ((phWidth - wmWidth) - 10);
            int yPosOfWm = phHeight - wmHeight;

            grWatermark.DrawImage(imgWatermark,
                                  new Rectangle(0, yPosOfWm, wmWidth, wmHeight),
                                  0,
                                  0,
                                  wmWidth,
                                  wmHeight,
                                  GraphicsUnit.Pixel,
                                  imageAttributes);

            imgPhoto = bmWatermark;
            grPhoto.Dispose();
            grWatermark.Dispose();

            DateTime now      = DateTime.Now;
            var      urlImage = "~/Uploads/result" + now.ToString("yyyyMMddHHmmssfff") + ".jpg";
            var      link     = HttpContext.Current.Server.MapPath(urlImage);
            var      urlRes   = domain + "/Uploads/result" + now.ToString("yyyyMMddHHmmssfff") + ".jpg";

            def.meta = new Meta(200, "Success");
            def.data = urlRes;
            imgPhoto.Save(link);
            imgPhoto.Dispose();
            imgWatermark.Dispose();
            return(Ok(def));
        }