当前位置: 代码迷 >> 综合 >> C# 常见方法封装,生成水印,测试代码性能等等
  详细解决方案

C# 常见方法封装,生成水印,测试代码性能等等

热度:35   发布时间:2024-02-07 08:52:28.0

第一个:在一张图片上进行文字添加

参数:文件路径,添加内容

返回:文件名称

  private string AddTextToImg(string fileName, string text){string phyfileName = Server.MapPath("..\\upload\\Pic\\") + fileName;string newPath = Server.MapPath("..\\upload\\Pic\\") + "timestamp_" + fileName;//var watermarkedStream = new MemoryStream();FileStream imgstream = File.OpenRead(phyfileName);using (var img = Image.FromStream(imgstream)){using (var graphic = Graphics.FromImage(img)){var font1 = new Font("微软雅黑", 16, FontStyle.Bold, GraphicsUnit.Pixel);//var color = Color.FromArgb(128, 255, 255, 255);var color = Color.Red;var brush = new SolidBrush(color);//var point = new Point(img.Width - 10, img.Height - 50);var point = new Point(10, 5);graphic.DrawString(text, font1, brush, point);img.Save(newPath);img.Dispose();imgstream.Dispose();if (File.Exists(phyfileName) && File.Exists(newPath)){File.Delete(phyfileName);}//img.Save(watermarkedStream, ImageFormat.Jpeg);}}return "timestamp_" + fileName;/*if (!File.Exists(phyfileName)){//throw new FileNotFoundException("The file don't exist!");}if (text == string.Empty){return;}//还需要判断文件类型是否为图像类型,这里就不赘述了System.Drawing.Image image = System.Drawing.Image.FromFile(phyfileName);Bitmap bitmap = new Bitmap(image, image.Width, image.Height);Graphics g = Graphics.FromImage(bitmap);float fontSize = 24.0f;    //字体大小float textWidth = text.Length * fontSize;  //文本的长度//下面定义一个矩形区域,以后在这个矩形里画上白底黑字float rectX = 0;float rectY = 0;float rectWidth = text.Length * (fontSize + 8);float rectHeight = fontSize + 8;//声明矩形域RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);Font font = new Font("宋体", fontSize);   //定义字体Brush whiteBrush = new SolidBrush(Color.Red);   //白笔刷,画文字用Brush blackBrush = new SolidBrush(Color.Black);   //黑笔刷,画背景用g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);g.DrawString(text, font, whiteBrush, textArea);//MemoryStream ms = new MemoryStream();//保存为Jpg类型//bitmap.Save(ms, ImageFormat.Jpeg);//输出处理后的图像,这里为了演示方便,我将图片显示在页面中了//Response.Clear();//Response.ContentType = "image/jpeg";//Response.BinaryWrite(ms.ToArray());//保存加水印过后的图片,删除原始图片string newPath = Server.MapPath("..\\upload\\Pic\\") +"timestamp_"+ fileName;image.Save(newPath);image.Dispose();//已生成新水印图片,if (File.Exists(phyfileName) && File.Exists(newPath)){File.Delete(phyfileName);}g.Dispose();bitmap.Dispose();image.Dispose();* */}

第二个:测试代码性能

调用:using (new TestGC("List<string>"))

{

}

 internal sealed class TestGC : IDisposable {private Stopwatch sw=new Stopwatch();private string text;private int count;//构造开始便进行一次重置public TestGC(string t) {PrepareForOperation();text = t;//同时统计一次垃圾回收次数:count = GC.CollectionCount(0);//时间重新开始计时sw.Start();}//using内容结束时,释放资源,统计垃圾回收次数public void Dispose() {Console.WriteLine("{0},执行时间:{1}ms,垃圾回收次数:{2}",text,sw.ElapsedMilliseconds,GC.CollectionCount(0)-count);}//void IDisposable.Dispose()//{//    throw new NotImplementedException();//}}///相当于进行垃圾回收重置操作,即先垃圾回收上一次遗留,并确保完成///并重新开始垃圾回收private static void PrepareForOperation() {GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();}

每次开发相关功能,或是写测试案例,都要在电脑上找,不是很方便,这里统一放在这里,方便自己在不同终端上找,也方便其他伙伴,精力有限,未经优化整理,请见谅

  相关解决方案