/// <summary> /// 将字符串转换为图片,用于水印 /// </summary> /// <imgvpath>生成的文字水印图片存储位置</imgvpath> /// <FontModel>字体模型,包含文本,字体颜色,大小等</imgvpath> /// <returns></returns> public static System.Drawing.Image ConverFontToImg(string imgvpath, FontModel model) { int width = model.intsize * model.text.Length * 2; int height = model.intsize * 2; //-----------根据指定宽高,生成图片 string imgppath = function.VToP(imgvpath); SafeC.CreateDir(imgvpath); //System.Drawing.Image image, image2 = System.Drawing.Image.FromFile(imgppath); System.Drawing.Image image = new Bitmap(width, height); Graphics g = Graphics.FromImage(image); //g.FillRectangle(new SolidBrush(Color.FromArgb(model.GetRGB(model.background, "red"), model.GetRGB(model.background, "yellow"), model.GetRGB(model.background, "blue"))), 0, 0, width, height); //背景色 g.DrawImage(image, 0, 0, image.Width, image.Height); //------------指定文字与字体样式 FontStyle fs = FontStyle.Regular; Font f = new Font(model.family, model.intsize, fs); SizeF crSize = g.MeasureString(model.text, f); //string color = "0x" + "#333333".Replace("#", ""); //SolidBrush b = new SolidBrush(Color.FromArgb(255, Color.FromArgb(Convert.ToInt32(color, 16))));//定义字体画笔 SolidBrush b = new SolidBrush(Color.FromArgb(model.GetRGB(model.color, "red"), model.GetRGB(model.color, "yellow"), model.GetRGB(model.color, "blue"))); StringFormat StrFormat = new StringFormat(); int ap = 40;//透明度 if (ap > 100 || ap < 0) { ap = 100; } int m_alpha = 255 * (ap / 100); int posx = 3, posy = 3;//文字的起始位置 SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, 0, 0, 0)); g.DrawString(model.text, f, semiTransBrush2, new PointF(posx + 1, posy + 1), StrFormat); //?无用 g.DrawString(model.text, f, b, new PointF(posx, posy), StrFormat); g.Dispose(); return(image); }
public static void CreateDir(string dirPath, string dirName) { SafeC.CreateDir(dirPath, dirName); }
//------------IO相关(本地文件写入,读取) public static void CreateDir(string ppath) { SafeC.CreateDir(ppath); }