C# aspx 添加水印

  protected void Page_Load(object sender, EventArgs e)
        {
            string img = "";
            if (Request.Params["img"].ToString() == "1")
            {
                img = "图片1.png";
            }
            if (Request.Params["img"].ToString() == "2")
            {
                img = "图片2.png";
            }
            Response.ClearContent();
            Response.ContentType = "image/png";
            byte[] result = WatermarkImage(img);
            Response.OutputStream.Write(result, 0, result.Length);
            Response.End();
        }
protected byte[] WatermarkImage(string fileName)
        {
            fileName = Server.MapPath("~/Application/EPIntegral/EPZWH/EPZWHImages/" + fileName + ".jpg");
            byte[] imageBytes = null;
            if (File.Exists(fileName))
            {
                // Normally you'd put this in a config file somewhere.
                string watermark = UserID.ToString() + " " + Name.ToString();

                Image image = Image.FromFile(fileName);

                Graphics graphic;
                if (image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format4bppIndexed && image.PixelFormat != PixelFormat.Format1bppIndexed)
                {
                    // Graphic is not a Indexed (GIF) image
                    graphic = Graphics.FromImage(image);
                }
                else
                {
                    /* Cannot create a graphics object from an indexed (GIF) image. 
                     * So we're going to copy the image into a new bitmap so 
                     * we can work with it. */
                    Bitmap indexedImage = new Bitmap(image);
                    graphic = Graphics.FromImage(indexedImage);

                    // Draw the contents of the original bitmap onto the new bitmap. 
                    graphic.DrawImage(image, 0, 0, image.Width, image.Height);
                    image = indexedImage;
                }
                graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;

                Font myFont = new Font("Arial", 15);
                //SolidBrush brush = new SolidBrush(Color.FromArgb(255, Color.Red));
                Brush brush = new SolidBrush(Color.FromArgb(64, Color.Red));             /* This gets the size of the graphic so we can determine 
                 * the loop counts and placement of the watermarked text. */
                SizeF textSize = graphic.MeasureString(watermark, myFont);

                graphic.TranslateTransform(0, 0);
                graphic.RotateTransform(30);
                //// Write the text across the image. 
                int fy = 0;
                for (int y = 50; y < image.Height; y += 30)
                {
                    int flag = 0;
                    for (int x = 50; x < image.Width; x += 30)
                    {
                        PointF pointF = new PointF(x + fy * 33, y - flag * 200);
                        flag++;
                        //float py = 30 * flag;//
                        //flag = flag * -1;
                        //以指定角度对画板进行旋转
                        //graphic.RotateTransform(360+py); 
                        graphic.DrawString(watermark, myFont, brush, pointF);
                        x += Convert.ToInt32(textSize.Width);
                    }
                    fy++;
                    y += Convert.ToInt32(textSize.Height);
                }

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    image.Save(memoryStream, ImageFormat.Jpeg);
                    imageBytes = memoryStream.ToArray();
                }

            }
            return imageBytes;
        }

 

posted @ 2017-03-27 17:20  微笑代表淡定.Net  阅读(470)  评论(0)    收藏  举报