我有一个热图,目前在我要移植到 Azure 存储的独立服务器上运行。如何在 Azure 存储中保存 butmap 文件。最初,我的 web.config 文件中有一个条目,将图像缓存指向另一个驱动器 (IE) 上的直接路径,现在所有内容都将位于存储帐户的 ~/map_cache 文件夹中。我如何修改它以在 Azure 中本地存储。
protected void Page_Load(object sender, EventArgs e)
{
xxxxxxxdb = new xxxxxxx(ConfigurationManager.AppSettings["xxxxxxx"]);
string imageCachePath = Server.MapPath("/map_cache/HotSpots");
int fileExpirationTime = int.Parse(ConfigurationManager.AppSettings["HotspotImageExpirationTime"]);
Bitmap bitmap;
string requestParam = Page.Request.Params["id"];
string bitmapFileName = Path.Combine(imageCachePath, requestParam + ".png");
if (File.Exists(bitmapFileName) && File.GetCreationTime(bitmapFileName) > DateTime.Now.AddHours(-fileExpirationTime))
{
bitmap = (Bitmap)Image.FromFile(bitmapFileName);
}
else
{
int zoomLevel = requestParam.Length;
double tileX = 0;
double tileY = 0;
for (int index = 0; index < zoomLevel; index++)
{
int digit = int.Parse(requestParam[index].ToString());
tileY += ((digit & 2) / 2) * Math.Pow(2, (zoomLevel - index - 1));
tileX += (digit & 1) * Math.Pow(2, (zoomLevel - index - 1));
}
double pixelXMin = tileX * 256;
double pixelYMin = tileY * 256;
double pixelXMax = (tileX + 1) * 256 - 1;
double pixelYMax = (tileY + 1) * 256 - 1;
double longMin = ((pixelXMin * 360) / (256 * Math.Pow(2, zoomLevel))) - 180;
double longMax = ((pixelXMax * 360) / (256 * Math.Pow(2, zoomLevel))) - 180;
double latMin = Math.Asin((Math.Exp((0.5 - pixelYMin / 256 / Math.Pow(2, zoomLevel)) * 4 * Math.PI) - 1) /
(Math.Exp((0.5 - pixelYMin / 256 / Math.Pow(2, zoomLevel)) * 4 * Math.PI) + 1)) * 180 /
Math.PI;
double latMax = Math.Asin((Math.Exp((0.5 - pixelYMax / 256 / Math.Pow(2, zoomLevel)) * 4 * Math.PI) - 1) /
(Math.Exp((0.5 - pixelYMax / 256 / Math.Pow(2, zoomLevel)) * 4 * Math.PI) + 1)) * 180 /
Math.PI;
double pixelResolution = (Math.Cos(latMax * Math.PI / 180) * 2 * Math.PI * 6378137) / (256 * Math.Pow(2, zoomLevel));
double pixelArea = Math.Pow(pixelResolution, 2);
double maxHotspotDensity = Math.Max(120.0 / zoomLevel, 3.0) / pixelArea;
bitmap = GenerateBlankBitmap();
var accidents = from hs in db.cs_PT_VEGeoDatas
where hs.Latitude <= latMin && hs.Latitude >= latMax
&& hs.Longitude >= longMin && hs.Longitude <= longMax
select new { hs.Latitude, hs.Longitude };
Dictionary<Point, HotSpot> hotSpots = new Dictionary<Point, HotSpot>();
foreach (var accident in accidents)
{
int pixelX, pixelY;
LatLongToPixelXY(accident.Latitude, accident.Longitude, zoomLevel, out pixelX, out pixelY);
pixelX %= 256;
pixelY %= 256;
for (int ix = -doublePixelSize; ix <= doublePixelSize; ix++)
{
for (int iy = -doublePixelSize; iy <= doublePixelSize; iy++)
{
Point point;
bool borderPoint = false;
if (zoomLevel < doublePixelZoomLevel)
{
point = new Point(pixelX, pixelY);
}
else
{
if (pixelX + ix >= 0 && pixelX + ix <= 255 && pixelY + iy >= 0 && pixelY + iy <= 255)
{
point = new Point(pixelX + ix, pixelY + iy);
borderPoint = (ix == -doublePixelSize) || (iy == -doublePixelSize) ||
(ix == doublePixelSize) || (iy == doublePixelSize);
}
else
{
break;
}
}
HotSpot hotSpot;
if (hotSpots.ContainsKey(point))
{
hotSpot = hotSpots[point];
hotSpot.borderPoint &= borderPoint;
hotSpot.count += 1;
}
else
{
hotSpot = new HotSpot { borderPoint = borderPoint, count = 1 };
hotSpots.Add(point, hotSpot);
}
if (zoomLevel < doublePixelZoomLevel)
{
break;
}
}
if (zoomLevel < doublePixelZoomLevel)
{
break;
}
}
}
foreach (var hotspotPixel in hotSpots)
{
double hc = hotspotPixel.Value.count;
double hcDensity = hc / pixelArea;
Color color;
if (!hotspotPixel.Value.borderPoint)
{
color = Color.FromArgb(255, 255,
(int)
Math.Max((maxHotspotDensity - hcDensity) / maxHotspotDensity * 255, 0.0),
0);
}
else
{
color = Color.Black;
}
bitmap.SetPixel(hotspotPixel.Key.X, hotspotPixel.Key.Y, color);
}
bitmap.Save(bitmapFileName);
}
WritePngToStream(bitmap, Response.OutputStream);
}
目前我收到以下错误消息
A generic error occurred in GDI+.
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Runtime.InteropServices.ExternalException:GDI+ 中发生一般错误。
源错误:
在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。
堆栈跟踪:
[ExternalException (0x80004005): A general error occurred in GDI+.] System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +772265 HotSpotTileServer.Page_Load(Object sender, EventArgs e) in C:\Projects\xxx \xxx\SpeedTrap\HotSpotTileServer.aspx.cs:141 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25 System.EventHandler.Invoke(Object sender, EventArgs e) +0 System.Web.UI.Control.LoadRecursive() +71 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint) +3048
版本信息:Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.0.30319.1