C#: Watermark image has poor quality when saving JPEG -
i'm trying add watermark image , save @ best quality, when saving jpeg watermark has poor quality (but quality of main image good).
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 saving works well, need save jpeg. here's difference between png , jpeg http://i.stack.imgur.com/cqwgz.jpg
i followed article http://www.codeproject.com/articles/2927/creating-a-watermarked-photograph-with-gdi-for-net , same result.
is there way make watermark on jpeg looks good?
first intall imagemagick in c:\imagemagick folder in windows using
http://www.imagemagick.org/download/binaries/imagemagick-6.8.8-7-q16-x86-dll.exe
while installation add c:\imagemagick; in path enviornment variable
and use below code b service
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; } use in web page
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"; } } } }
Comments
Post a Comment