示例#1
0
        public Stream WatermarkText(TextWatermarkModel model)
        {
            if (ModelState.IsValid)
            {
                ImageHandler.TextWatermarker txtWatermarker = new ImageHandler.TextWatermarker(model.Image, model.CopyrightText);
                txtWatermarker.ChangeFont(model.FontFamily, model.Bold, model.Italic, model.Underline, model.StrikeThrough);
                txtWatermarker.Position = model.CopyrightPosition;
                txtWatermarker.AddWatermark();
                MemoryStream mem = new MemoryStream();
                //txtWatermarker.WatermarkedImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                string fileName  = model.File.FileName;
                string extension = Path.GetExtension(fileName);
                Response.ContentType = "image/" + extension;
                txtWatermarker.WatermarkedImage.Save(mem, ImageHelper.GetImageFormatByExtension(extension));
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));
                mem.WriteTo(Response.OutputStream);
                mem.Dispose();
                txtWatermarker.Dispose();
                return(Response.OutputStream);
            }
            else
            {
                return(null);
            }
        }
示例#2
0
        public void ProcessRequest(HttpContext context)
        {
            var    response  = context.Response;
            var    request   = context.Request;
            string imagePath = null;

            // Check whether the page requesting the image is from your site.
            if (request.UrlReferrer != null)
            {
                // Perform a case-insensitive comparison of the referer.
                if (string.Compare(request.Url.Host, request.UrlReferrer.Host, true, CultureInfo.InvariantCulture) == 0)
                {
                    // The requesting host is correct.
                    // Allow the image to be served(if it exists).
                    imagePath = request.PhysicalPath;
                    if (!File.Exists(imagePath))
                    {
                        response.StatusCode        = 404;
                        response.StatusDescription = "File not found";
                        return;
                    }
                    else
                    {
                        // Serve the image normally
                        response.ContentType = GetMimeTypeByFileExtension(Path.GetExtension(imagePath));
                        response.WriteFile(imagePath);
                        return;
                    }
                }
                else
                {
                    // Add watermark to the image
                    imagePath = request.PhysicalPath;

                    // Set the content type to the appropriate image type.
                    var extension = Path.GetExtension(imagePath);
                    if (extension != null)
                    {
                        response.ContentType = "image/" + extension.Replace(".", "").ToLower();
                    }
                    else
                    {
                        response.ContentType = "image/*";
                    }

                    // Serve the image.
                    var image = Image.FromFile(imagePath);

                    var txtWatermarker = new ImageHandler.TextWatermarker(image, "zizhujy.com");
                    txtWatermarker.AddWatermark();

                    image = txtWatermarker.WatermarkedImage;
                    //image.Save(response.OutputStream, ImageHelper.GetImageFormatByExtension(Path.GetExtension(imagePath)));

                    var mem = new MemoryStream();
                    image.Save(mem, ImageHelper.GetImageFormatByExtension(Path.GetExtension(imagePath)));

                    // Write the MemoryStream data to the OutputStream
                    response.ContentType = GetMimeTypeByFileExtension(imagePath);
                    mem.WriteTo(response.OutputStream);

                    mem.Dispose();

                    image.Dispose();
                    txtWatermarker.Dispose();
                    return;
                }
            }
            else
            {
                // Add watermark to the image
                imagePath = request.PhysicalPath;

                // Set the content type to the appropriate image type.
                var extension = Path.GetExtension(imagePath);
                if (extension != null)
                {
                    response.ContentType = "image/" + extension.Replace(".", "").ToLower();
                }
                else
                {
                    response.ContentType = "image/*";
                }

                // Serve the image.
                var image          = Image.FromFile(imagePath);
                var txtWatermarker = new ImageHandler.TextWatermarker(image, "zizhujy.com");
                txtWatermarker.AddWatermark();
                image = txtWatermarker.WatermarkedImage;

                // If the image is in PNG format, then it can be saved into response.OutputStream directly
                var format = ImageHelper.GetImageFormatByExtension(Path.GetExtension(imagePath));
                //if (format.Equals(ImageFormat.Png))
                if (true)
                {
                    // Create the PNG in memory
                    var mem = new MemoryStream();
                    image.Save(mem, ImageFormat.Png);

                    // Write the MemoryStream data to the output stream.
                    response.ContentType = GetMimeTypeByFileExtension(imagePath);
                    mem.WriteTo(response.OutputStream);
                    mem.Dispose();
                }
                else
                {
                    image.Save(response.OutputStream, format);
                }

                image.Dispose();
                txtWatermarker.Dispose();
                return;
            }
        }