Exemple #1
0
        public void AddWatermark2()
        {
            Image originImage = Image.FromFile(@"C:\1305211066-1784_600-0_6-0.jpg");
            //Image image = ImageHandler.WatermarkHandler.AddCopyrightText(originImage, "copyright", Color.FromArgb(153,0,0,0),Color.FromArgb(153,255,255,255),
            //    1,1,"Arial", FontStyle.Bold, StringAlignment.Far, 72f,72f);

            //Image image = ImageHandler.WatermarkHandler.AddCopyrightText(originImage, "中文", CopyrightPosition.TopLeft);
            //image = ImageHandler.WatermarkHandler.AddCopyrightText(image, "copyright", CopyrightPosition.TopCenter);
            //image = ImageHandler.WatermarkHandler.AddCopyrightText(image, "copyright", CopyrightPosition.TopRight);
            //image = ImageHandler.WatermarkHandler.AddCopyrightText(image, "copyright", CopyrightPosition.MiddleLeft);
            //image = ImageHandler.WatermarkHandler.AddCopyrightText(image, "copyright", CopyrightPosition.MiddleCenter);
            //image = ImageHandler.WatermarkHandler.AddCopyrightText(image, "copyright", CopyrightPosition.MiddleRight);
            //image = ImageHandler.WatermarkHandler.AddCopyrightText(image, "copyright", CopyrightPosition.BottomLeft);
            //image = ImageHandler.WatermarkHandler.AddCopyrightText(image, "copyright", CopyrightPosition.BottomCenter);
            //image = ImageHandler.WatermarkHandler.AddCopyrightText(image, "copyright", CopyrightPosition.BottomRight);

            TextWatermarker txtWatermarkder = new TextWatermarker(originImage, "Hello");

            txtWatermarkder.HorizontalPosition = WatermarkHorizontalPostion.Right;
            txtWatermarkder.VerticalPosition   = WatermarkVerticalPostion.Bottom;
            Image image = txtWatermarkder.AddWatermark();

            Response.ContentType = "image/jpeg";
            image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            originImage.Dispose();
            image.Dispose();
            txtWatermarkder.Dispose();
        }
Exemple #2
0
        public void AddWatermark()
        {
            // First, add the image watermark
            Image originImage = Image.FromFile(@"C:\source.jpg");
            Image watermark   = Image.FromFile(@"C:\clock.png");

            ImageWatermarker imgWatermarker = new ImageWatermarker(originImage, watermark);

            // You can set the watermark size, if you not then the watermark will be its original size by default
            imgWatermarker.SetWatermarkHeight(80);
            imgWatermarker.AddWatermark();

            // Second, add the text watermark
            TextWatermarker txtWatermarker = new TextWatermarker(imgWatermarker.WatermarkedImage, "www.zizhujy.com");

            txtWatermarker.Position = WatermarkPostion.TopLeft;
            txtWatermarker.AddWatermark();

            // Response and show the watermarked image in the browser
            Response.ContentType = "image/jpeg";
            // Note: save the secondly watermarked image
            //  (txtWatermarker not imgWatermarker because the txtWatermarker comes after than imgWatermarker)
            txtWatermarker.WatermarkedImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            imgWatermarker.Dispose();
            watermark.Dispose();
            originImage.Dispose();
        }
    void ImageGuarding(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;

        if (context.Request.UrlReferrer != null)
        {
            if (string.Compare(context.Request.Url.Host.Replace("www.", ""), context.Request.UrlReferrer.Host.Replace("www.", ""), true, CultureInfo.InvariantCulture) != 0)
            {
                string guardingType = settings.GetSingleValue("guardingType");
                if (guardingType.Equals("Reject"))
                {
                    context.Response.StatusCode = 403;
                    context.Response.End();
                }
                else if (guardingType.Equals("Block"))
                {
                    string blockImagePath = settings.GetSingleValue("blockImagePath");
                    var    fi             = new FileInfo(context.Server.MapPath(blockImagePath));
                    if (fi.Exists)
                    {
                        //context.Response.Cache.SetCacheability(HttpCacheability.Public);
                        //context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));

                        string extension = Path.GetExtension(fi.Name);

                        context.Response.ContentType = string.Compare(extension.ToUpper(), "JPG") == 0 ? "image/jpeg" : string.Format("image/{0}", extension);

                        context.Response.TransmitFile(fi.FullName);
                        context.Response.End();
                    }
                }
                else if (guardingType.Equals("Watermark"))
                {
                    FileInfo originImageFileInfo = GetOriginalImage();
                    if (originImageFileInfo != null)
                    {
                        TextWatermarker  txtWatermarker = null;
                        ImageWatermarker imgWatermarker = null;
                        Image            originImage    = Bitmap.FromFile(originImageFileInfo.FullName);
                        string           watermarkText  = settings.GetSingleValue("watermarkText");

                        if (!string.IsNullOrWhiteSpace(watermarkText))
                        {
                            txtWatermarker = new TextWatermarker(originImage, watermarkText);
                            txtWatermarker.AddWatermark();
                            originImage = txtWatermarker.WatermarkedImage;
                        }

                        string watermarkImagePath = settings.GetSingleValue("watermarkImagePath");

                        if (!string.IsNullOrWhiteSpace(watermarkImagePath))
                        {
                            FileInfo fi = new FileInfo(context.Server.MapPath(watermarkImagePath));
                            if (fi.Exists)
                            {
                                Image watermarkImage = Bitmap.FromFile(fi.FullName);

                                imgWatermarker          = new ImageWatermarker(originImage, watermarkImage);
                                imgWatermarker.Position = WatermarkPostion.BottomLeft;
                                imgWatermarker.AddWatermark();

                                originImage = imgWatermarker.WatermarkedImage;
                            }
                        }

                        // Serving the watermarked image
                        //context.Response.Cache.SetCacheability(HttpCacheability.Public);
                        //context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));

                        string extension = Path.GetExtension(originImageFileInfo.Name);

                        context.Response.ContentType = string.Compare(extension.ToUpper(), "JPG") == 0 ? "image/jpeg" : string.Format("image/{0}", extension);

                        //originImage.Save(context.Response.OutputStream, ImageHelper.GetImageFormatByExtension(extension));
                        MemoryStream mem = new MemoryStream();
                        originImage.Save(mem, ImageHelper.GetImageFormatByExtension(extension));
                        mem.WriteTo(context.Response.OutputStream);
                        mem.Dispose();
                        originImage.Dispose();

                        if (txtWatermarker != null)
                        {
                            txtWatermarker.Dispose();
                        }
                        if (imgWatermarker != null)
                        {
                            imgWatermarker.Dispose();
                        }

                        context.Response.End();
                    }
                    else
                    {
                        context.Response.StatusCode = 404;
                        context.Response.End();
                    }
                }
                else
                {
                }
            }
            else
            {
                // string.Compare(context.Request.Url.Host, context.Request.UrlReferrer.Host, true, CultureInfo.InvariantCulture) == 0
                // The request host and the referrer host are the same, serve the image normally

                //FileInfo originImageFileInfo = GetOriginalImage();
                //if (originImageFileInfo != null)
                //{
                //    TextWatermarker txtWatermarker = null;
                //    Image originImage = Bitmap.FromFile(originImageFileInfo.FullName);
                //    string watermarkText = context.Request.Url.Host;

                //    if (!string.IsNullOrWhiteSpace(watermarkText))
                //    {
                //        txtWatermarker = new TextWatermarker(originImage, watermarkText);
                //        txtWatermarker.Position = WatermarkPostion.TopLeft;
                //        txtWatermarker.AddWatermark();
                //        originImage = txtWatermarker.WatermarkedImage;
                //    }

                //    watermarkText = context.Request.UrlReferrer.Host;

                //    if (!string.IsNullOrWhiteSpace(watermarkText))
                //    {
                //        txtWatermarker = new TextWatermarker(originImage, watermarkText);
                //        txtWatermarker.AddWatermark();
                //        originImage = txtWatermarker.WatermarkedImage;
                //    }

                //    string extension = Path.GetExtension(originImageFileInfo.Name);

                //    context.Response.ContentType = string.Compare(extension.ToUpper(), "JPG") == 0 ? "image/jpeg" : string.Format("image/{0}", extension);

                //    //originImage.Save(context.Response.OutputStream, ImageHelper.GetImageFormatByExtension(extension));
                //    MemoryStream mem = new MemoryStream();
                //    originImage.Save(mem, ImageHelper.GetImageFormatByExtension(extension));
                //    mem.WriteTo(context.Response.OutputStream);
                //    mem.Dispose();
                //    originImage.Dispose();

                //    if (txtWatermarker != null)
                //        txtWatermarker.Dispose();

                //    context.Response.End();
                //}
                //else
                //{
                //    context.Response.StatusCode = 404;
                //    context.Response.End();
                //}
            }
        }
    }