第一个:在一张图片上进行文字添加
参数:文件路径,添加内容
返回:文件名称
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();
}
}
第三种:为了尽量少写一般处理程序,即一个一般处理程序文件,执行多个方法,进行封装
调用:
public class DW : BaseHttpHandler{
}
public class BaseHttpHandler : IHttpHandler, IRequiresSessionState
{
protected MyHttpRequest Request;
protected HttpResponse Response;
protected HttpSessionState Session;
protected HttpServerUtility Server;
protected HttpApplicationState Application = HttpContext.Current.Application;
protected dbo conn = common.make();
protected UserInfo ui = new UserInfo();
public virtual void ProcessRequest(HttpContext context)
{
//不让浏览器缓存
context.Response.Buffer = true;
context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
context.Response.AddHeader("pragma", "no-cache");
context.Response.AddHeader("cache-control", "");
context.Response.CacheControl = "no-cache";
context.Response.ContentType = "text/plain";
#region 设置允许跨域,允许复杂请求
//header("Access-Control-Allow-Origin: *"); # 跨域处理
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (context.Request.HttpMethod == "OPTIONS")
{
context.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,PATCH,OPTIONS");
context.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
//header('Access-Control-Allow-Headers:x-requested-with'); //php设置
//response.addHeader("Access-Control-Allow-Origin", "*");
context.Response.AddHeader("Access-Control-Allow-Headers", "x-requested-with");
//HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
context.Response.End();
}
#endregion
Request = new MyHttpRequest(context.Request);
Response = context.Response;
Session = context.Session;
Server = context.Server;
string method = context.Request["Method"].ToString();
MethodInfo methodInfo = this.GetType().GetMethod(method);
methodInfo.Invoke(this, null);
}
}
每次开发相关功能,或是写测试案例,都要在电脑上找,不是很方便,这里统一放在这里,方便自己在不同终端上找,也方便其他伙伴,精力有限,未经优化整理,请见谅