c# 图片加水印
程序员文章站
2022-12-06 19:20:35
/// /// 图片水印 /// /// 服务器图片相对路径 /// 保存文件名 /// 水印文件相对路径 /// 图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下 /// 附加水印图片质量,0-100 /// 水印的透明... ......
/// <summary> /// 图片水印 /// </summary> /// <param name="imgpath">服务器图片相对路径</param> /// <param name="filename">保存文件名</param> /// <param name="watermarkfilename">水印文件相对路径</param> /// <param name="watermarkstatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param> /// <param name="quality">附加水印图片质量,0-100</param> /// <param name="watermarktransparency">水印的透明度 1--10 10为不透明</param> public static void addimagesignpic(string imgpath, string filename, string watermarkfilename, int watermarkstatus, int quality, int watermarktransparency) { if (!file.exists(utils.getmappath(imgpath))) return; byte[] _imagebytes = file.readallbytes(utils.getmappath(imgpath)); image img = image.fromstream(new system.io.memorystream(_imagebytes)); filename = utils.getmappath(filename); if (watermarkfilename.startswith("/") == false) watermarkfilename = "/" + watermarkfilename; watermarkfilename = utils.getmappath(watermarkfilename); if (!file.exists(watermarkfilename)) return; graphics g = graphics.fromimage(img); //设置高质量插值法 //g.interpolationmode = system.drawing.drawing2d.interpolationmode.high; //设置高质量,低速度呈现平滑程度 //g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality; image watermark = new bitmap(watermarkfilename); if (watermark.height >= img.height || watermark.width >= img.width) return; imageattributes imageattributes = new imageattributes(); colormap colormap = new colormap(); colormap.oldcolor = color.fromargb(255, 0, 255, 0); colormap.newcolor = color.fromargb(0, 0, 0, 0); colormap[] remaptable = { colormap }; imageattributes.setremaptable(remaptable, coloradjusttype.bitmap); float transparency = 0.5f; if (watermarktransparency >= 1 && watermarktransparency <= 10) transparency = (watermarktransparency / 10.0f); float[][] colormatrixelements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; colormatrix colormatrix = new colormatrix(colormatrixelements); imageattributes.setcolormatrix(colormatrix, colormatrixflag.default, coloradjusttype.bitmap); int xpos = 0; int ypos = 0; switch (watermarkstatus) { case 1: xpos = (int)(img.width * (float).01); ypos = (int)(img.height * (float).01); break; case 2: xpos = (int)((img.width * (float).50) - (watermark.width / 2)); ypos = (int)(img.height * (float).01); break; case 3: xpos = (int)((img.width * (float).99) - (watermark.width)); ypos = (int)(img.height * (float).01); break; case 4: xpos = (int)(img.width * (float).01); ypos = (int)((img.height * (float).50) - (watermark.height / 2)); break; case 5: xpos = (int)((img.width * (float).50) - (watermark.width / 2)); ypos = (int)((img.height * (float).50) - (watermark.height / 2)); break; case 6: xpos = (int)((img.width * (float).99) - (watermark.width)); ypos = (int)((img.height * (float).50) - (watermark.height / 2)); break; case 7: xpos = (int)(img.width * (float).01); ypos = (int)((img.height * (float).99) - watermark.height); break; case 8: xpos = (int)((img.width * (float).50) - (watermark.width / 2)); ypos = (int)((img.height * (float).99) - watermark.height); break; case 9: xpos = (int)((img.width * (float).99) - (watermark.width)); ypos = (int)((img.height * (float).99) - watermark.height); break; } g.drawimage(watermark, new rectangle(xpos, ypos, watermark.width, watermark.height), 0, 0, watermark.width, watermark.height, graphicsunit.pixel, imageattributes); imagecodecinfo[] codecs = imagecodecinfo.getimageencoders(); imagecodecinfo ici = null; foreach (imagecodecinfo codec in codecs) { if (codec.mimetype.indexof("jpeg") > -1) ici = codec; } encoderparameters encoderparams = new encoderparameters(); long[] qualityparam = new long[1]; if (quality < 0 || quality > 100) quality = 80; qualityparam[0] = quality; encoderparameter encoderparam = new encoderparameter(system.drawing.imaging.encoder.quality, qualityparam); encoderparams.param[0] = encoderparam; if (ici != null) img.save(filename, ici, encoderparams); else img.save(filename); g.dispose(); img.dispose(); watermark.dispose(); imageattributes.dispose(); }
/// <summary> /// 文字水印 /// </summary> /// <param name="imgpath">服务器图片相对路径</param> /// <param name="filename">保存文件名</param> /// <param name="watermarktext">水印文字</param> /// <param name="watermarkstatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param> /// <param name="quality">附加水印图片质量,0-100</param> /// <param name="fontname">字体</param> /// <param name="fontsize">字体大小</param> public static void addimagesigntext(string imgpath, string filename, string watermarktext, int watermarkstatus, int quality, string fontname, int fontsize) { byte[] _imagebytes = file.readallbytes(utils.getmappath(imgpath)); image img = image.fromstream(new system.io.memorystream(_imagebytes)); filename = utils.getmappath(filename); graphics g = graphics.fromimage(img); font drawfont = new font(fontname, fontsize, fontstyle.regular, graphicsunit.pixel); sizef crsize; crsize = g.measurestring(watermarktext, drawfont); float xpos = 0; float ypos = 0; switch (watermarkstatus) { case 1: xpos = (float)img.width * (float).01; ypos = (float)img.height * (float).01; break; case 2: xpos = ((float)img.width * (float).50) - (crsize.width / 2); ypos = (float)img.height * (float).01; break; case 3: xpos = ((float)img.width * (float).99) - crsize.width; ypos = (float)img.height * (float).01; break; case 4: xpos = (float)img.width * (float).01; ypos = ((float)img.height * (float).50) - (crsize.height / 2); break; case 5: xpos = ((float)img.width * (float).50) - (crsize.width / 2); ypos = ((float)img.height * (float).50) - (crsize.height / 2); break; case 6: xpos = ((float)img.width * (float).99) - crsize.width; ypos = ((float)img.height * (float).50) - (crsize.height / 2); break; case 7: xpos = (float)img.width * (float).01; ypos = ((float)img.height * (float).99) - crsize.height; break; case 8: xpos = ((float)img.width * (float).50) - (crsize.width / 2); ypos = ((float)img.height * (float).99) - crsize.height; break; case 9: xpos = ((float)img.width * (float).99) - crsize.width; ypos = ((float)img.height * (float).99) - crsize.height; break; } g.drawstring(watermarktext, drawfont, new solidbrush(color.white), xpos + 1, ypos + 1); g.drawstring(watermarktext, drawfont, new solidbrush(color.black), xpos, ypos); imagecodecinfo[] codecs = imagecodecinfo.getimageencoders(); imagecodecinfo ici = null; foreach (imagecodecinfo codec in codecs) { if (codec.mimetype.indexof("jpeg") > -1) ici = codec; } encoderparameters encoderparams = new encoderparameters(); long[] qualityparam = new long[1]; if (quality < 0 || quality > 100) quality = 80; qualityparam[0] = quality; encoderparameter encoderparam = new encoderparameter(system.drawing.imaging.encoder.quality, qualityparam); encoderparams.param[0] = encoderparam; if (ici != null) img.save(filename, ici, encoderparams); else img.save(filename); g.dispose(); img.dispose(); }
上一篇: 华夏出入口车牌识别摄像机
下一篇: 可牛影像怎么制作人像的柔焦效果?