public CreateQrCode ( string plainText, ECCLevel eccLevel, bool utf8BOM = false ) : |
||
plainText | string | |
eccLevel | ECCLevel | |
utf8BOM | bool | |
리턴 |
private void renderQRCode() { string level = comboBox1.SelectedItem.ToString(); QRCodeGenerator.ECCLevel eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3); QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(textBoxQRCode.Text, eccLevel); pictureBoxQRCode.BackgroundImage = qrCode.GetGraphic(20, Color.Black, Color.White, getIconBitmap()); }
public Form2() { InitializeComponent(); QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode("https://play.google.com/store/apps/details?id=eu.dirtyharry.opsiadmin", QRCodeGenerator.ECCLevel.Q); appbox.BackgroundImage = qrCode.GetGraphic(2); }
/// <summary> /// 生成带参数二维码 /// </summary> /// <returns></returns> public static bool GetOauthCode(string code, string filePath) { try { // 生成二维码的内容 QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q); QRCode qrcode = new QRCode(qrCodeData); // qrcode.GetGraphic 方法可参考最下发“补充说明” Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false); MemoryStream ms = new MemoryStream(); qrCodeImage.Save(ms, ImageFormat.Jpeg); // 如果想保存图片 可使用 qrCodeImage.Save(filePath + code + ".jpg"); return(true); } catch (Exception ex) { _logerror.Error("生成二维码异常:" + ex.Message); return(false); } }
/// <summary> /// 创建二维码返回Base64字符串 /// </summary> /// <param name="plainText">二维码内容</param> public static string CreateQRCodeToBase64(string plainText, bool hasEdify = true) { try { string result = ""; if (String.IsNullOrEmpty(plainText)) { return(""); } QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator(); //QRCodeGenerator.ECCLevel:纠错能力,Q级:约可纠错25%的数据码字 QRCodeData qrCodeData = qrGenerator.CreateQrCode(plainText, QRCodeGenerator.ECCLevel.Q); QRCode qrcode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrcode.GetGraphic(15); MemoryStream ms = new MemoryStream(); qrCodeImage.Save(ms, ImageFormat.Jpeg); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); if (hasEdify) { result = "data:image/jpeg;base64," + Convert.ToBase64String(arr); } else { result = Convert.ToBase64String(arr); } return(result); } catch (Exception ex) { throw new Exception("创建二维码返回Base64字符串方法异常", ex); } }
private void RefreshQRCode() { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://passport.bilibili.com/qrcode/getLoginUrl"); myRequest.CookieContainer = new CookieContainer(); //将请求的结果发送给客户端(界面、应用) HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); cookie = myResponse.Headers.Get("Set-Cookie"); string callback = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); JSONCallback.QRUrl.QRUrl json = JsonConvert.DeserializeObject <JSONCallback.QRUrl.QRUrl>(callback); if (json.code == 0) { oathkey = json.data.oauthKey; QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(json.data.url, QRCodeGenerator.ECCLevel.Q); QRCode qrcode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrcode.GetGraphic(6, Color.Black, Color.White, null, 15, 6, true); QRCodePicBox.Image = qrCodeImage; materialLabel1.Text = "请使用哔哩哔哩客户端扫码登录或扫码下载APP"; timer1.Start(); } }
private void renderQRCode() { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(textBoxQRCode.Text, QRCodeGenerator.ECCLevel.M); pictureBoxQRCode.BackgroundImage = qrCode.GetGraphic(20); }
/// <summary> /// 添加车票信息到图片并存放 /// </summary> /// <param name="tickInfo"></param> public static Image MakeTrainTickImage(TrainTicketInfo tickInfo) { if (tickInfo.logid == null) { return(null); } if (TrainTicket.BaseTicketImage == null) { try { TicketImageTool.LoadAllBaseTrainTicketsImage(); } catch { MessageBox.Show("车票模板为空:" + TrainTicket.BaseTickfilePath + "\n" + "请检查是否含有车票模板", "车票目录", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); System.Diagnostics.Process.Start(TrainTicket.BaseTickfilePath); } return(null); } var ticketnumber = new Random().Next(0, TrainTicket.BaseTicketImage.Count); var image = TrainTicket.BaseTicketImage[ticketnumber]; //Console.WriteLine(image.Width + " " + image.Height); Bitmap bitmap = new Bitmap(image, image.Width, image.Height); Graphics g = Graphics.FromImage(bitmap); //cutImage(image, new Point(136,69),50,30); //字体矩形位置 : //x = 图片的长度的中心位置 - 字体长度的一半 - 字行距 //y = 图片的高度的中心位置 - 字体大小的一半 - 偏移(去掉偏移,是居中位置) string year = null; if (tickInfo.date.Length >= 4) { year = tickInfo.date[0].ToString() + tickInfo.date[1].ToString() + tickInfo.date[2].ToString() + tickInfo.date[3].ToString();//年 } string moth = null; if (tickInfo.date.Length >= 7) { moth = tickInfo.date[5].ToString() + tickInfo.date[6].ToString();//月 } string day = null; if (tickInfo.date.Length >= 10) { day = tickInfo.date[8].ToString() + tickInfo.date[9].ToString();//日 } string fickrates = null; if (tickInfo.ticket_rates.Contains("¥")) { fickrates = tickInfo.ticket_rates.Replace("¥", string.Empty); } string Rates = null; if (fickrates != null && fickrates.Contains("元")) { Rates = fickrates.Replace("元", string.Empty); } string Start_Station = null; string Start_Stationpinyin = null; if (!String.IsNullOrWhiteSpace(tickInfo.starting_station)) { Start_Station = tickInfo.starting_station.Remove(tickInfo.starting_station.Length - 1); Start_Stationpinyin = PingYinHelper.GetPinyin(tickInfo.starting_station.Remove(tickInfo.starting_station.Length - 1)); } string destination_Station = null; string Des_Stationpinyin = null; if (!String.IsNullOrWhiteSpace(tickInfo.destination_station)) { destination_Station = tickInfo.destination_station.Remove(tickInfo.destination_station.Length - 1); Des_Stationpinyin = PingYinHelper.GetPinyin(tickInfo.destination_station.Remove(tickInfo.destination_station.Length - 1)); } string S_station = null; string D_station = null; int S_pinyinaddX = 0; int D_pinyinaddX = 0; int max_pinyinmove_length = 9; bool D_ISLongPinyin = false; bool S_ISLongPinyin = false; if (Start_Stationpinyin != null && Start_Stationpinyin.Length <= max_pinyinmove_length) { S_pinyinaddX = 42 - Start_Stationpinyin.Length; } else { S_ISLongPinyin = true; } if (Des_Stationpinyin != null && Des_Stationpinyin.Length <= max_pinyinmove_length) { D_pinyinaddX = 42 - Des_Stationpinyin.Length; } else { D_ISLongPinyin = true; } if (Start_Station != null && Start_Station.Length <= 2) { S_station += Start_Station[0]; S_station += " "; S_station += Start_Station[1]; } else { S_station = Start_Station; } if (destination_Station != null && destination_Station.Length <= 2) { D_station += destination_Station[0]; D_station += " "; D_station += destination_Station[1]; } else { D_station = destination_Station; } string IDinfo = null; char addidinfo; for (int i = 0; i < tickInfo.ID.Length; i++) { if (i >= 10 && i <= 13) { addidinfo = '*'; } else { addidinfo = tickInfo.ID[i]; } IDinfo += addidinfo; } var reduce = new Random().Next(0, 20); AddInfoToImage(tickInfo.ticket_num, g, 40, "arial.ttf", 50, 35 - reduce, RedBrush, FontStyle.Bold); AddInfoToImage(tickInfo.jianpiao, g, 32, 680, 20, blackBrush); AddInfoToImage(S_station, g, 41, 110, 80, blackBrush); if (Start_Stationpinyin != null) { if (S_ISLongPinyin) { AddInfoToImage(Start_Stationpinyin, g, 30, "标准仿宋体简.ttf", 110 + Start_Stationpinyin.Length + S_pinyinaddX, 140, blackBrush, FontStyle.Regular, 0.8f, 1); } else { AddInfoToImage(Start_Stationpinyin, g, 30, "标准仿宋体简.ttf", 110 + Start_Stationpinyin.Length + S_pinyinaddX, 140, blackBrush); } } AddInfoToImage("站", g, 30, 290, 90, blackBrush); AddInfoToImage(D_station, g, 41, 600, 80, blackBrush); if (Des_Stationpinyin != null) { if (D_ISLongPinyin) { AddInfoToImage(Des_Stationpinyin, g, 30, "标准仿宋体简.ttf", 600 + Des_Stationpinyin.Length + D_pinyinaddX, 140, blackBrush, FontStyle.Regular, 0.8f, 1); } else { AddInfoToImage(Des_Stationpinyin, g, 30, "标准仿宋体简.ttf", 600 + Des_Stationpinyin.Length + D_pinyinaddX, 140, blackBrush); } } AddInfoToImage("站", g, 30, 780, 90, blackBrush); //////////////////////////////////////////////// if (tickInfo.train_num.Length < 4) { AddInfoToImage(tickInfo.train_num, g, 40, "车次.ttf", 400 + 20 - tickInfo.train_num.Length, 80, blackBrush); } else if (tickInfo.train_num.Length == 4) { AddInfoToImage(tickInfo.train_num, g, 40, "车次.ttf", 408, 80, blackBrush); } else if (tickInfo.train_num.Length == 5) { AddInfoToImage(tickInfo.train_num, g, 40, "车次.ttf", 392, 80, blackBrush); } ///////////////////////////////////////////////////////////////// AddInfoToImage(tickInfo.train_che, g, 32, "TechnicBold.ttf", 587, 182, blackBrush); AddInfoToImage("车", g, 20, 637, 195, blackBrush); AddInfoToImage(tickInfo.train_hao, g, 32, "TechnicBold.ttf", 660, 182, blackBrush); AddInfoToImage("号", g, 20, 732, 195, blackBrush); //AddInfoToImage("限乘当日当次车", g, 9, 20, 110, blackBrush); ////////////////////////////////////////////////// AddInfoToImage("网", g, 27, 400, 242, blackBrush); AddInfoToImage(tickInfo.seat_category, g, 25, 642, 240, blackBrush); AddInfoToImage(year, g, 34, "TechnicBold.ttf", 79, 182, blackBrush, FontStyle.Regular); AddInfoToImage("年", g, 20, 170, 195, blackBrush); AddInfoToImage(moth, g, 34, "TechnicBold.ttf", 200, 182, blackBrush, FontStyle.Regular); AddInfoToImage("月", g, 20, 255, 195, blackBrush); AddInfoToImage(day, g, 34, "TechnicBold.ttf", 285, 182, blackBrush, FontStyle.Regular); AddInfoToImage("日", g, 20, 337, 195, blackBrush); AddInfoToImage(tickInfo.train_starttime, g, 34, "TechnicBold.ttf", 376, 182, blackBrush, FontStyle.Bold); AddInfoToImage("开", g, 20, 480, 195, blackBrush); //AddInfoToImage("¥", g, 31, "华文宋体.ttf", 80, 240, blackBrush); AddInfoToImage(Rates, g, 33, "TechnicBold.ttf", 119, 240, blackBrush); int yuan_X = 0; if (Rates != null && Rates.Length > 4) { yuan_X += 25; } AddInfoToImage("元", g, 20, 205 + yuan_X, 250, blackBrush); // AddInfoToImage(tickInfo.ticket_num, g, 13, 20, 9, RedBrush); AddInfoToImage(tickInfo.name, g, 31, 460f, 397f, blackBrush); AddInfoToImage(IDinfo, g, 33.5f, "TechnicBoldshort.ttf", 78f, 397f, blackBrush); //AddInfoToImage(tickInfo.bottomid, g, 25f, 80f, 560f, blackBrush,FontStyle.Bold,1f,2f); AddInfoToImage(tickInfo.bottomid, g, 20f, "仿宋_GB2312.ttf", 90f, 560f, blackBrush, FontStyle.Regular, 1.147f, 1.6f); AddInfoToImage(Start_Station, g, 27f, 440f, 560f, blackBrush, FontStyle.Bold); int Shou_X = 0; if (Start_Station != null && Start_Station.Length <= 2) { Shou_X -= 38; } AddInfoToImage("售", g, 27f, 550f + Shou_X, 560f, blackBrush, FontStyle.Bold); string strCode = string.Format("网络异常\n车票读取信息为{0}\n{1}\n{2}→{3}\n{4}年{5}月{6}日{7}\n{8}车{9}号\n{10}", tickInfo.ticket_num, tickInfo.train_num, S_station, D_station, year, moth, day, tickInfo.train_starttime, tickInfo.train_che, tickInfo.train_hao, tickInfo.name); // string strCode = "https://www.12306.cn/index/"; QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q); QRCode qrcode = new QRCode(qrCodeData); // qrcode.GetGraphic 方法可参考最下发“补充说明” Bitmap qrCodeImage = qrcode.GetGraphic(3, Color.FromArgb(160, 0, 0, 0), Color.FromArgb(0, 0, 0, 0), null, 16, 6, false); AddImageToImage(g, ResizeImage(qrCodeImage, qrCodeImage.Width - 12, qrCodeImage.Height), new Point(735, 377)); var FinalImage = byteToImage(Bitmap2Byte(bitmap)); //"售" //bitmap.Save(TrainTicket.savePath + tickInfo.GetHashCode() + ".jpg", ImageFormat.Jpeg); //TrainTicket.TrainTicketImages.Add(byteToImage(Bitmap2Byte(bitmap))); g.Dispose(); bitmap.Dispose(); //image.Dispose(); return(FinalImage); }
public ActionResult GenerateAuthQrCode(string key) { var ProvisionUrl = string.Format("otpauth://totp/{0}:{1}?secret={2}", Config.Title, User.Identity.Name, key); QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(ProvisionUrl, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrCode.GetGraphic(20); return File(Helpers.Utility.ImageToByte(qrCodeImage), "image/png"); }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string result = ""; string type = ""; string total = ""; int pageSize = 2; try { string poststr = getpost(); if (!string.IsNullOrEmpty(poststr)) { JObject jobj = JObject.Parse(poststr); type = jobject(jobj, "type"); string namevalue = jobject(jobj, "name"); string contendvalue = jobject(jobj, "content"); string pageNow = jobject(jobj, "pageNow"); string id = jobject(jobj, "id"); switch (type) { case "liuyan": break; case "all": DataTable dt = gettoMsg(pageNow); total = dt.Rows.Count.ToString(); result = "{\"code\":\"0\",\"total\" :\"" + total + "\",\"data\":" + DataTableToJson(dt) + "}"; break; case "search": string bstructure = " id|name|creditCode|regStatus|regDate"; string sql = ArrayKeyWord(contendvalue, bstructure); DataTable dtMsg = ShowMsg(sql); total = dtMsg.Rows.Count.ToString(); result = "{\"code\":\"0\",\"total\" :\"" + total + "\",\"data\":" + DataTableToJson(dtMsg) + "}"; break; case "byId": DataTable dt_id = SearchById(id); total = dt_id.Rows.Count.ToString(); result = "{\"code\":\"0\",\"total\" :\"" + total + "\",\"data\":" + DataTableToJson(dt_id) + "}"; break; case "dele": string[] RegisterCodes = new string[5] { "9", "8", "4", "6", "3" }; var flag = true; var registerCodesStr = string.Empty; foreach (var registerCode in RegisterCodes) { if (string.IsNullOrEmpty(registerCode)) { continue; } var workoreder = registerCode; if (!flag || workoreder == null) { registerCodesStr += registerCode + " ; "; flag = false; continue; } if (int.Parse(registerCode) > 5) { flag = true; } else { flag = false; } if (flag == false) { registerCodesStr += registerCode; continue; } } if (!string.IsNullOrEmpty(registerCodesStr)) { Console.WriteLine("不成功的有:" + registerCodesStr); } break; case "getname": string urls = "http://*****:*****@"DownLoad\"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } var epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; var r1 = new Random().Next(9); var r2 = new Random().Next(9); var r3 = new Random().Next(9); var r4 = new Random().Next(9); var r = r1 + r2 + r3 + r4; string filePath; //只能使用xls格式的excel文件 filePath = dirPath + epoch + r + ".txt"; FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write); fs.Write(results.ResultByte, 0, results.ResultByte.Length); fs.Flush(); fs.Close(); break; case "list": string dataSource = "phone=15015051037"; var url = "https://www.e-irobot.com/api/get_phone"; // 使用ASCII码字符 byte[] datalogins = Encoding.ASCII.GetBytes(dataSource); HttpWebRequest res = (HttpWebRequest)HttpWebRequest.Create(new Uri(url)); res.Method = "Post"; res.ContentType = "application/x-www-form-urlencoded"; res.ContentLength = datalogins.Length; Stream newstream = res.GetRequestStream(); newstream.Write(datalogins, 0, datalogins.Length); newstream.Close(); //接收相应 HttpWebResponse resp = (HttpWebResponse)res.GetResponse(); Stream respStream = resp.GetResponseStream(); StreamReader reader = new StreamReader(respStream, Encoding.UTF8); string resultsss = reader.ReadToEnd(); reader.Close(); res.Abort(); ////发送信息 //WebRequest req = WebRequest.Create(url); //req.Method = "post"; //req.ContentType = "application/x-www-form-urlencoded"; //req.CookieContainer = cookies;//设置cookies ////Accept - Language:zh - CN,zh; q = 0.8 //req.Headers.Add("Accept-Language", "zh - CN,zh; q = 0.8"); ////指定客户端代理的方式 ////req.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2560.0 Safari/537.36"); ////添加Post参数 //Stream reqStream = req.GetRequestStream(); //reqStream.Write(data, 0, data.Length); //reqStream.Close(); ////接收相应 //HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); //Stream respStream = resp.GetResponseStream(); //StreamReader reader = new StreamReader(respStream, Encoding.UTF8); //string resultsss = reader.ReadToEnd(); //reader.Close(); break; case "excel": try { SqlDbOperHandler doh = new SqlDbOperHandler(); doh.Reset(); doh.SqlCmd = "select from Table_yan"; DataTable excelTab = doh.GetDataTable(); ExportExcel(excelTab); } catch (Exception ex) { WriteLog(ex, "C:\\Users\\Amazinghua\\Desktop\\log.txt"); } break; case "datatable": //excel读入内存表,批量插入数据库 string path = System.Web.HttpContext.Current.Server.MapPath("~/excel/"); string path_zip = path + "gs_0805.xls"; //目标文件 //DataTable fakeTable = ReadExcelToDataTable(path_zip); //BatchInsertBySqlBulkCopy(fakeTable, "gs_0805"); FileInfo fi = new FileInfo(path_zip); string test = (fi.Length / 1024).ToString(); if (int.Parse(test) <= 2) { } else { } break; case "gstest": break; } } } catch (Exception ex) { WriteLog(ex, "C:\\Users\\Amazinghua\\Desktop\\log.txt"); } context.Response.Write(result); context.Response.End(); }
private void renderQR(string msg) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeGenerator.QRCode qrCode= qrGenerator.CreateQrCode(msg, QRCodeGenerator.ECCLevel.L); codebox.BackgroundImage = qrCode.GetGraphic(8); }
/// <summary> /// Excel导出 /// </summary> /// <param name="keyValuePairs">字典表【名称,数据】</param> /// <param name="sWebRootFolder">网站根文件夹</param> /// <param name="tuple">item1:The virtual path of the file to be returned.|item2:The Content-Type of the file</param> public static void Export(Dictionary <string, DataTable> keyValuePairs, string sWebRootFolder, out Tuple <string, string> tuple) { if (string.IsNullOrWhiteSpace(sWebRootFolder)) { tuple = Tuple.Create("", Excel); } string sFileName = $"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx"; var savePath = Path.Combine(sWebRootFolder, sFileName); if (!Directory.Exists(Path.GetDirectoryName(savePath))) { Directory.CreateDirectory(Path.GetDirectoryName(savePath)); } FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName)); using (ExcelPackage package = new ExcelPackage(file)) { foreach (var item in keyValuePairs) { string worksheetTitle = item.Key; //表名称 var dt = item.Value; //数据表 // 添加worksheet ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(worksheetTitle); var jColumns = 0; for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { if (i == 0) { //添加表头 worksheet.Cells[1, j + 1].Value = dt.Columns[j].ColumnName; worksheet.Cells[1, j + 1].Style.Font.Bold = true; //字体为粗体 worksheet.Cells[1, j + 1].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center; //水平居中 worksheet.Cells[1, j + 1].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; //设置样式类型 worksheet.Cells[1, j + 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(159, 197, 232)); //设置单元格背景色 } else { if (dt.Columns[j].ColumnName == "二维码") { if (!string.IsNullOrEmpty(dt.Rows[i][j].ToString())) { using (QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator()) { //QRCodeGenerator.ECCLevel:纠错能力,Q级:约可纠错25%的数据码字 QRCodeData qrCodeData = qrGenerator.CreateQrCode(dt.Rows[i][j].ToString(), QRCodeGenerator.ECCLevel.Q); QRCode qrcode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrcode.GetGraphic(15); using (MemoryStream stream = new MemoryStream()) { qrCodeImage.Save(stream, ImageFormat.Jpeg); byte[] bytes = new byte[stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(bytes, 0, System.Convert.ToInt32(stream.Length)); System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes); System.Drawing.Bitmap myimge = new System.Drawing.Bitmap(ms); ExcelPicture picture = worksheet.Drawings.AddPicture(Common.GuId(), myimge); //插入图片 worksheet.Row(i + 1).Height = 80.00D; picture.SetPosition(i, 5, 24, 5); //设置图片的位置 picture.SetSize(80, 80); //设置图片的大小 } } } jColumns = j + 1; } else { //添加值 worksheet.Cells[i + 1, j + 1].Value = dt.Rows[i][j].ToString(); } worksheet.Cells[i + 1, j + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; //水平居中 worksheet.Cells[i + 1, j + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center; //垂直居中 //worksheet.Row(i+1).Height = 85;//设置行高 } } } if (jColumns == 0) { //自动列宽 worksheet.Cells.AutoFitColumns(); } else { //自动列宽 worksheet.Cells.AutoFitColumns(); worksheet.Column(jColumns).Width = 20.00D;//设置列宽 } } package.Save(); } tuple = Tuple.Create(sFileName, Excel); }
private void btnAdd_Click(object sender, EventArgs e) { if (this.txtmasp.Text.Trim().Length <= 0) { this.errorProvider1.SetError(this.txtmasp, "Nhập mã sản phẩm"); return; } else if (SPController.getSanPham(this.txtmasp.Text) != null) { this.errorProvider1.SetError(this.txtmasp, "Mã Sản Phẩm Đã Tồn Tại"); return; } else if (this.txttensp.Text.Trim().Length <= 0) { this.errorProvider1.SetError(this.txttensp, "Nhập tên Sản Phẩm"); return; } else if (this.txtgiasp.Text.Trim().Length <= 0) { this.errorProvider1.SetError(this.txtgiasp, "Nhập giá sản phẩm"); return; } else if (Convert.ToDateTime(dtpngayin.Value.ToString()) <= Convert.ToDateTime(DateTime.Today.ToString())) { this.errorProvider1.SetError(this.dtpngayin, "Ngày in phải nhỏ hơn hoặc bằng ngày hiện tại"); return; } else { SanPham sanpham = new SanPham(); sanpham.MaSanPham = this.txtmasp.Text.Trim(); sanpham.TenSanPham = this.txttensp.Text.Trim(); sanpham.GiaSanPham = this.txtgiasp.Text.Trim(); sanpham.NgayIn = this.dtpngayin.Value; ListViewItem information = new ListViewItem(this.ID.ToString()); information.SubItems.Add(new ListViewItem.ListViewSubItem(information, txtmasp.Text)); information.SubItems.Add(new ListViewItem.ListViewSubItem(information, txttensp.Text)); information.SubItems.Add(new ListViewItem.ListViewSubItem(information, txtgiasp.Text)); information.SubItems.Add(new ListViewItem.ListViewSubItem(information, dtpngayin.Value.ToString())); this.listInformation.Items.Add(information); Bitmap bm = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height); this.pictureBox1.DrawToBitmap(bm, this.pictureBox1.ClientRectangle); this.pictureBox1.CreateGraphics().Clear(Color.White); bm = new Bitmap(this.pictureBox1.Width + 2, this.pictureBox1.Height + 2); if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); pictureBox1.Image = null; } //In ma 3D QRCoder.QRCodeGenerator qr = new QRCoder.QRCodeGenerator(); var data = qr.CreateQrCode(txttensp.Text, QRCoder.QRCodeGenerator.ECCLevel.H); var code = new QRCoder.QRCode(data); pictureBox1.Image = code.GetGraphic(5); MemoryStream stream = new MemoryStream(); code.GetGraphic(5).Save(stream, ImageFormat.Jpeg); sanpham.ImgQCode = stream.ToArray(); //In ma 2D string barcode = txttensp.Text; try { Zen.Barcode.Code128BarcodeDraw br = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum; pictureBox2.Image = br.Draw(barcode, 40); MemoryStream stream1 = new MemoryStream(); br.Draw(barcode, 40).Save(stream1, ImageFormat.Jpeg); sanpham.Img1D = stream1.ToArray(); } catch (Exception ex) { } if (SPController.AddSanPham(sanpham) == false) { MessageBox.Show("Lỗi thêm user", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } txttensp.Text = string.Empty; txtmasp.Text = string.Empty; txtgiasp.Text = string.Empty; dtpngayin.Value = DateTime.Now; this.errorProvider1.Clear(); DisPlayInformation(); } }