【收集】常用的一些代码块<1>
来源:互联网 发布:python黑帽子中文下载 编辑:程序博客网 时间:2024/05/01 12:26
一、记录日志
protected override void OnStart(string[] args) { this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + "\n"); this.WriteLog("客户端数据同步服务:【服务启动】"); } protected override void OnStop() { this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss")+ "\n"); this.WriteLog("客户端数据同步服务:【服务停止】"); } protected override void OnShutdown() { this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + "\n"); this.WriteLog("客户端数据同步服务:【计算机关闭】"); } #region 记录日志 /// <summary> /// 记录日志 /// </summary> /// <param name="msg"></param> private void WriteLog(string msg) { //string path = @"C:\log.txt"; //该日志文件会存在windows服务程序目录下 string path = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt"; FileInfo file = new FileInfo(path); if (!file.Exists) { FileStream fs; fs = File.Create(path); fs.Close(); } using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine(DateTime.Now.ToString() + " " + msg); } } } #endregion
二、生成二维码
/// <summary> /// 生成用户名片 /// </summary> /// <returns></returns> public ActionResult creQRcode() { string name = Request["name"]==null? "暂无" : Request["name"]; string tel = Request["phone"] == null ? "400 XXXX 421" : Request["phone"]; string TITLE = Request["phone"] == null ? "400 XXXX 421" : Request["phone"]; string ORG = Request["userTypeName"] == null ? "暂无" : Request["userTypeName"]; string ADR = Request["address"] == null ? "XXXXX" : Request["address"]; string URL = "http://www.yhfy.cn/"; string EMAIL = Request["email"] == null ? "bXXXXX1@126.com" : Request["email"]; string comment = Request["comment"] == null ? "无" : Request["comment"]; StringBuilder card = new StringBuilder(); card.Append("BEGIN:VCARD"); card.Append("\r\nFN:" + name);//姓名 card.Append("\r\nTEL;CELL:" + tel);//手机号 card.Append("\r\nTITLE:" + TITLE);// card.Append("\r\nORG:" + ORG+ "-" + comment); card.Append("\r\nTEL;WORK:" + "400-6690421"); card.Append("\r\nADR;WORK:" + ADR); card.Append("\r\nURL:" + URL); card.Append("\r\nEMAIL;WORK:" + EMAIL); card.Append("\r\nEND:VCARD\r\n"); Bitmap bmp = GetTwoDimensionCode(card.ToString(), string.Empty, 200, 200, "微软雅黑"); string path = "~/Upload/MyQRCoed/"; string path2 = Request.MapPath(path); if (!Directory.Exists(path2)) { Directory.CreateDirectory(path2); } string guid = Guid.NewGuid().ToString(); string newPath = path2 + guid + ".jpg"; bmp.Save(newPath, ImageFormat.Jpeg); string src = "../Upload/MyQRCoed/" + guid + ".jpg"; return Content(src); } /// <summary> /// 绘制二维码 /// </summary> /// <param name="strSource"></param> /// <param name="text"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="fontName"></param> /// <returns></returns> public static Bitmap GetTwoDimensionCode(string strSource, string text, int width, int height, string fontName) { // 创建Bitmap对象 Bitmap bmp = new Bitmap(width, height); // 从image创建 Graphics对象 Graphics objGraphics = Graphics.FromImage(bmp); // 填上背景色 objGraphics.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height); // ThoughtWorks.QRCode.Codec.QRCodeEncoder qrCodeEncoder = new ThoughtWorks.QRCode.Codec.QRCodeEncoder(); // 设置编码方法 qrCodeEncoder.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE; // 设置大小 qrCodeEncoder.QRCodeScale = 3; // 适用于信息量较少的情形,图像越小保存的信息量越少 // qrCodeEncoder.QRCodeScale = 4; // 设置版本 qrCodeEncoder.QRCodeVersion = 0; // 设置错误校验的级别,正因为二维码有纠错能力,才能够加入logo qrCodeEncoder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M; Image image = qrCodeEncoder.Encode(strSource, Encoding.GetEncoding("utf-8")); // 写入二维码 int x = (int)(width - image.Width) / 2; int y = (int)(height - image.Height) / 2; objGraphics.DrawImage(image, new Point(x, y)); // 添加Logo图标 image = MvcTMM.Properties.Resources.logo; x = (int)(width - image.Width) / 2; y = (int)(height - image.Height) / 2; objGraphics.DrawImage(image, new Point(x, y)); // 写入字符串 //objGraphics.DrawString(text, new Font(fontName, 13, FontStyle.Bold), // Brushes.Black, new PointF(43, 15)); return bmp; }
三、图片加水印
using System;using System.Collections;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.IO;using System.Linq;using System.Web;namespace MvcTMM.BaseCode{ /// <summary> /// 调用方法:传入图片的路径,添加水印的文字 /// Img.LetterWatermark(@"C:\Users\Administrator\Desktop\Img加水印\Img加水印\bin\1.jpg", "YHFY:种植前田地状态"); /// </summary> public class ImgAddText { #region 文字水印 ///// <summary> ///// 文字水印处理方法 ///// </summary> ///// <param name="path">图片路径(绝对路径)</param> ///// <param name="letter">水印文字</param> ///// <param name="location">水印位置</param> ///// <param name="size">字体大小</param> /// <returns>图片路径</returns> public static string LetterWatermark(string path, string letter, string location = "LB", int size = 20) { #region //水印的颜色样式 Color color = System.Drawing.Color.FromArgb(224, 207, 201); //添加时间后缀 letter += " | " + DateTime.Now.ToLocalTime(); string kz_name = Path.GetExtension(path); if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg" || kz_name == ".png") { DateTime time = DateTime.Now; string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString(); Image img = Bitmap.FromFile(path); Graphics gs = Graphics.FromImage(img); ArrayList loca = GetLocation(location, img, size, letter.Length); Font font = new Font("微软雅黑", size); Brush br = new SolidBrush(color); gs.DrawString(letter, font, br, float.Parse(loca[0].ToString()), float.Parse(loca[1].ToString())); gs.Dispose(); string newpath = Path.GetDirectoryName(path) + filename + kz_name; img.Save(newpath); img.Dispose(); File.Copy(newpath, path, true); if (File.Exists(newpath)) { File.Delete(newpath); } } return path; #endregion } /// <summary> /// 文字水印位置的方法 /// </summary> /// <param name="location">位置代码</param> /// <param name="img">图片对象</param> /// <param name="width">宽(当水印类型为文字时,传过来的就是字体的大小)</param> /// <param name="height">高(当水印类型为文字时,传过来的就是字符的长度)</param> private static ArrayList GetLocation(string location, Image img, int width, int height) { ArrayList loca = new ArrayList(); float x = 10; float y = 10; if (location == "LT") { loca.Add(x); loca.Add(y); } else if (location == "T") { x = img.Width / 2 - (width * height) / 2; loca.Add(x); loca.Add(y); } else if (location == "RT") { x = img.Width - width * height; } else if (location == "LC") { y = img.Height / 2; } else if (location == "C") { x = img.Width / 2 - (width * height) / 2; y = img.Height / 2; } else if (location == "RC") { x = img.Width - height; y = img.Height / 2; } else if (location == "LB") { y = img.Height - width - 15; } else if (location == "B") { x = img.Width / 2 - (width * height) / 2; y = img.Height - width - 5; } else { x = img.Width - width * height; y = img.Height - width - 5; } loca.Add(x); loca.Add(y); return loca; } #endregion }}
阅读全文
0 0
- 【收集】常用的一些代码块<1>
- 常用的一些代码块
- JS中一些常用的代码块
- JS中一些常用的代码块
- JS中一些常用的代码块
- 一些常用的JS代码收集
- 收集大家常用的一些Android代码
- 收集了一些常用的Android代码
- 一些基本js,php代码块收集
- 收集的一些常用JS代码和CSS代码
- android 一些常用的功能方法代码块
- android 一些常用的功能方法代码块(二)
- android 一些常用的功能方法代码块(二)
- android 一些常用的功能方法代码块 (一)
- android 一些常用的功能方法代码块
- 一些常用的代码块。后期会慢慢更新
- 一些常用到的js功能代码块
- 收集的一些代码
- Reverse Nodes in k-Group
- C# 使用path获取文件路径
- 每日一练(20171102)
- Microsoft HoloLens开发入门
- 2.读书笔记收获不止Oracle之 意识
- 【收集】常用的一些代码块<1>
- 编码
- HTTP网络请求状态码
- C#替换Word中的文本内容
- hbase学习--1 使用 java api 创建表
- 英文段落每行末尾加连字符
- jQuery的deferred对象详解
- 2.1.4 实现简单图片浏览
- backup dccnn