-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeController.cs
62 lines (53 loc) · 1.88 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using Preveld.Infrastructure;
using Preveld.Models;
using Preveld.ViewModels;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using QRCoder;
namespace Preveld.Controllers
{
public class HomeController : BaseController
{
ApplicationDBContext dbContext = new ApplicationDBContext();
[CustomAuthorize]
public ActionResult Index()
{
var latestValve = dbContext.Valves.OrderByDescending(v => v.Date_of_Inspection).FirstOrDefault();
var latestWrap = dbContext.Wraps.OrderByDescending(w => w.Date_of_last_Inspection).FirstOrDefault();
int valveId = -1;
int wrapId = -1;
if (latestValve != null)
{
valveId = latestValve.ID;
}
if (latestWrap != null)
{
wrapId = latestWrap.ID;
}
var plainText = "{\"valve\":" + valveId + ",\"wrap\":" + wrapId + "}";
QRCodeGenerator _qrCode = new QRCodeGenerator();
QRCodeData _qrCodeData = _qrCode.CreateQrCode(plainText, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(_qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);
LatestRecord record = new LatestRecord();
record.qrCode = BitmapToBytesCode(qrCodeImage);
record.Valve = latestValve;
record.Wrap = latestWrap;
return View(record);
}
[NonAction]
private static Byte[] BitmapToBytesCode(Bitmap image)
{
using (MemoryStream stream = new MemoryStream())
{
image.Save(stream, ImageFormat.Png);
return stream.ToArray();
}
}
}
}