/// <summary> /// 图片加水印文字 /// </summary> /// <param name="imgPath">原图片路径</param> /// <param name="newImgPath">新图片路径</param> /// <param name="text">水印文字</param> /// <param name="Alpha">透明度</param> /// <param name="fontsize">字体大小</param> public static bool PicAddWaterText(string imgPath,string newImgPath, string text, int Alpha = 1, int fontsize = 30) { try { //text = text + "仅限xx使用"; //System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(oldstream);//文件流 System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(imgPath); int imgPhotoWidth = imgPhoto.Width; int imgPhotoHeight = imgPhoto.Height; Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics gbmPhoto = Graphics.FromImage(bmPhoto); //gif背景色 gbmPhoto.Clear(Color.FromName("White")); gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight), 0, 0, imgPhotoWidth, imgPhotoHeight, GraphicsUnit.Pixel); imgPhoto.Dispose(); System.Drawing.Font font = null; System.Drawing.SizeF crSize = new SizeF(); font = new Font("宋体", fontsize, FontStyle.Regular); //测量指定区域 crSize = gbmPhoto.MeasureString(text, font); float y = (imgPhotoHeight - crSize.Height)/2; float x = (imgPhotoWidth - crSize.Width)/2; System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat(); StrFormat.Alignment = System.Drawing.StringAlignment.Center; //画两次制造透明效果 var writeColor = Color.FromArgb(Alpha, System.Drawing.ColorTranslator.FromHtml("#999")); //var writeColor = Color.FromArgb(255, Color.Red); //System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, Color.Red)); System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(writeColor); //float _x = -20, _y = y + 10; //if (imgPhotoHeight > imgPhotoWidth) //{ // // gbmPhoto.RotateTransform(-60); // _y = 0; _x = 0; //} //else { gbmPhoto.RotateTransform(320); } gbmPhoto.TranslateTransform(x,y); var Var_trans = gbmPhoto.Transform; Var_trans.Shear(0.4F,0.00F); gbmPhoto.Transform = Var_trans; //gbmPhoto.DrawString(text, font, semiTransBrush2, _x, _y); // gbmPhoto.DrawString(text, font, semiTransBrush2, imgPhotoWidth, imgPhotoHeight / 2); gbmPhoto.DrawString(text, font, semiTransBrush2, 10, 10); //gbmPhoto.DrawString(text, font, semiTransBrush2, 0, y); //System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(writeColor); // gbmPhoto.DrawString(text, font, semiTransBrush, _x, _y); //gbmPhoto.DrawString(text, font, semiTransBrush, imgPhotoWidth / 2, imgPhotoHeight / 2); //gbmPhoto.DrawString(text, font, semiTransBrush, 0, y); //gbmPhoto.DrawString(text, font, semiTransBrush2, 0, y); //Stream newStream = new MemoryStream(); bmPhoto.Save(newImgPath, System.Drawing.Imaging.ImageFormat.Jpeg); gbmPhoto.Dispose(); bmPhoto.Dispose(); //imgPhoto1.Dispose(); return true; } catch (Exception e) { return false; } }
分享一段,图片加水印的代码。本人修改过多次
猜你喜欢
转载自www.cnblogs.com/Blogs-Wang/p/10736101.html
今日推荐
周排行