c#中.gif图像上的水印文字

时间:2015-10-27 10:28:33

标签: c# asp.net animated-gif

您好我正在.GIF张图片上创建水印,但代码仅适用于.JPG张图片。 我使用以下代码

System.Drawing.Image objImage = System.Drawing.Image.FromFile(imageURL);//From File
                    int height = objImage.Height;//Actual image width
                    int width = objImage.Width;//Actual image height
                    System.Drawing.Bitmap bitmapimage = new System.Drawing.Bitmap(objImage, width, height);// create bitmap with same size of Actual image
                    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmapimage);                   
                    SolidBrush brush = new SolidBrush(Color.FromArgb(100, 255, 255, 255));
                    //Adding watermark text on image
                    string hex = "#ADADAD";
                    Color _color = System.Drawing.ColorTranslator.FromHtml(hex);
                    g.DrawString("www.Statckoverflow.com", new Font("Trebuchet MS", 10, System.Drawing.FontStyle.Bold), new SolidBrush(_color), 5, 5);
                    g.DrawString("Copyright © Gitz", new Font("Trebuchet MS", 10, System.Drawing.FontStyle.Bold), new SolidBrush(_color), 5, 20);

                    Response.ContentType = "Image/gif";
                    bitmapimage.Save(Response.OutputStream, ImageFormat.Gif);

此代码仅针对.JPG个文件。对于.GIF图像,它不起作用。图像正在创建,但.GIF图像的动画在创建水印后是strop。如果我在这段代码上做错了,请纠正我?????

1 个答案:

答案 0 :(得分:0)

我认为你应该将gif与帧分开并写入水印并再次在其上创建动画。 对于分离使用此代码:

        System.Drawing.Image objImage = System.Drawing.Image.FromFile("1.gif");//From File
        int height = objImage.Height;//Actual image width
        int width = objImage.Width;//Actual image height
        FrameDimension dimension = new FrameDimension(objImage.FrameDimensionsList[0]);
        // Number of frames
        int frameCount = objImage.GetFrameCount(dimension);
        Bitmap[] Frames = new Bitmap[frameCount];

        // Copy the animation Bitmap frames into the Bitmap array

        for (int Index = 0; Index < frameCount; Index++)
        {
            // Set the current frame within the animation to be copied into the Bitmap array element

            objImage.SelectActiveFrame(FrameDimension.Time, Index);

            // Create a new Bitmap element within the Bitmap array in which to copy the next frame

            Frames[Index] = new Bitmap(objImage.Size.Width, objImage.Size.Height);

            // Copy the current animation frame into the new Bitmap array element

            Graphics.FromImage(Frames[Index]).DrawImage(objImage, new Point(0, 0));
            SolidBrush brush = new SolidBrush(Color.FromArgb(100, 255, 255, 255));
            System.Drawing.Bitmap bitmapimage = new System.Drawing.Bitmap(objImage, width, height);// create bitmap with same size of Actual image
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmapimage);
            //Adding watermark text on image
            string hex = "#ADADAD";
            Color _color = System.Drawing.ColorTranslator.FromHtml(hex);
            g.DrawString("www.Statckoverflow.com", new Font("Trebuchet MS", 10, System.Drawing.FontStyle.Bold), new SolidBrush(_color), 5, 5);
            g.DrawString("Copyright © Gitz", new Font("Trebuchet MS", 10, System.Drawing.FontStyle.Bold), new SolidBrush(_color), 5, 20);

        }