-
Notifications
You must be signed in to change notification settings - Fork 2
/
getTaskImg.ashx
86 lines (81 loc) · 2.18 KB
/
getTaskImg.ashx
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<%@ WebHandler Language="C#" Class="getTaskImg" %>
using System;
using System.IO;
using System.Web;
using System.Drawing;
public class getTaskImg : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string basefile = context.Server.MapPath("images/taskicon.png");
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "image/png";
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetExpires(DateTime.Now.AddDays(1));
Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0, 0, 0));
int id = Convert.ToInt32(Request.QueryString["id"]);
if (id < 1)
{
Response.WriteFile(basefile);
return;
}
LoadImageFile(context, id, basefile);
}
public bool IsReusable
{
get
{
return false;
}
}
static object _Lock = new object();
void DellOldFiles(string dir, string validprefix)
{
var files = Directory.EnumerateFiles(dir);
foreach (var file in files)
{
string filename = Path.GetFileName(file);
if (!filename.StartsWith(validprefix))
{
File.Delete(file);
}
}
}
void LoadImageFile(HttpContext context, int id, string basefile)
{
string dir = context.Server.MapPath($"images/cache/dispo/");
string prefix = ReferenceVersion.REFSVERSION();
string filename = $"{dir}{prefix}_{id}.png";
if (!File.Exists(filename))
{
lock (_Lock)
{
if (!File.Exists(filename))
{
DellOldFiles(dir, prefix);
Color color = ColorTranslator.FromHtml(DefectDispo.GetDispColor(id));
using (Bitmap bitmap = new Bitmap(basefile))
{
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
Color gotColor = bitmap.GetPixel(x, y);
if (gotColor.A == 0)
{
bitmap.SetPixel(x, y, color);
}
}
}
bitmap.Save(filename);
}
}
}
}
context.Response.AddHeader("Content-Length", (new FileInfo(filename)).Length.ToString());
context.Response.TransmitFile(filename);
}
}