C#:保存JPEG时水印图像质量较差

时间:2013-05-03 14:15:55

标签: c# image watermark

我正在尝试为图像添加水印并以最佳质量保存,但保存JPEG水印时质量较差(但主图像的质量很好)。

string wtrSrc = @"D:\watermark.png";
string imgSrc = @"D:\image.jpg";
string imgOutJPG = @"D:\result.jpg";          
string imgOutPNG = @"D:\result.png";

// create new image
Bitmap imgOutput = new Bitmap(imgSrc);
Graphics outputGraphics = Graphics.FromImage(imgOutput);

// image quality
outputGraphics.CompositingQuality = CompositingQuality.HighQuality;
outputGraphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
outputGraphics.SmoothingMode = SmoothingMode.HighQuality;
outputGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

// create watermark image
System.Drawing.Image wtrmark = System.Drawing.Image.FromFile(wtrSrc);        

// add waternark
float wtrmarkX = 10;
float wtrmarkY = 10;
outputGraphics.DrawImage(wtrmark, wtrmarkX, wtrmarkY);

//set jpeg quality    
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;

// save result
imgOutput.Save(imgOutJPG, GetEncoder(ImageFormat.Jpeg), myEncoderParameters);
imgOutput.Save(imgOutPNG, ImageFormat.Png);

// clean
wtrmark.Dispose();
imgOutput.Dispose();
outputGraphics.Dispose();

...

    // ImageCodecInfo 
    private ImageCodecInfo GetEncoder(ImageFormat format)
    {

        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }

PNG保存效果很好,但我需要保存JPEG。 这是PNG和JPEG之间的区别 http://i.stack.imgur.com/CqwgZ.jpg

我按照这篇文章http://www.codeproject.com/Articles/2927/Creating-a-Watermarked-Photograph-with-GDI-for-NET获得了相同的结果。

有没有办法让JPEG上的水印看起来不错?

1 个答案:

答案 0 :(得分:1)

首先使用

在Windows中的C:\ Imagemagick文件夹中输入Imagemagick

http://www.imagemagick.org/download/binaries/ImageMagick-6.8.8-7-Q16-x86-dll.exe

安装时添加C:\ ImageMagick;在PATH环境变量中

并使用以下代码进行维修

    using System.IO;
    using System.Drawing;

    [WebMethod]
        public Byte[] WatermarkImage(Byte[] image)
        {
    MemoryStream ms = new MemoryStream(image);
    Image image2 = Image.FromStream(ms);
    image2.Save(Server.MapPath("~") + "\\imageTest.jpg");
    string strCmdText;
    strCmdText = "/C convert.exe -draw \"gravity south fill black text 0,0 'Parth' \" \"" + Server.MapPath("~") +"\\imageTest.jpg \" \""+Server.MapPath("~") +"\\imageTest_result.jpg \"";
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);
    System.Threading.Thread.Sleep(1000);
    byte[] imgdata = System.IO.File.ReadAllBytes(Server.MapPath("~") +"\\imageTest_result.jpg");
    return imgdata;
        }

在您的网页中使用

      protected void Button1_Click(object sender, EventArgs e)
         {
             if (this.FileUpload1.HasFile)
             {
        if (FileUpload1.PostedFile.ContentType == "image/jpeg")
        {
            if (FileUpload1.PostedFile.ContentLength < 512000)
            {
                string filename = Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~") + "\\" + filename);
                Watermark w = new Watermark();
                Byte[] b = w.WatermarkImage(GetBytesFromFile(Server.MapPath("~") + "\\" + filename));
                MemoryStream ms = new MemoryStream(b);
                System.Drawing.Image image2 = System.Drawing.Image.FromStream(ms);
                image2.Save(Server.MapPath("~") + "\\imageTest_op.jpg");
                Image1.ImageUrl = "imageTest_op.jpg";
            }
                 }
             }
         }