google-static-maps - 在谷歌静态地图中使用自定义图标
问题描述
我需要在谷歌地图的不同条件下使用自定义图标。在我的程序中有一个生成此图像的函数。在某些情况下,来自 Google 的照片是正确的,在某些情况下,地图上会放置默认图标。
生成的网址:
在我的应用程序中以正确模式生成的照片(64*34, 1.1 KB):
Google 以正确模式返回的照片:
和:
在我的应用程序中以错误模式生成的照片(64*64, 3.5 KB):
Google 以错误模式返回的照片:
public FileResult Icons4(float angle)
{
try
{
//It changes according to the conditions of this route
string path = "myPath";
Bitmap orgbmp = (Bitmap)Image.FromFile(path);
Bitmap bmp = CropTranslate(orgbmp);
Bitmap tempRotatedImage = new Bitmap((bmp.Width * 2) + 10, (bmp.Height * 2) + 10, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(tempRotatedImage))
{
g.Clear(Color.Transparent);
g.DrawImage(bmp, new Point(((int)(bmp.Width / 2) + 3), ((int)(bmp.Height / 2)) + 3));
}
Bitmap rotatedImage = new Bitmap(tempRotatedImage.Width, tempRotatedImage.Height);
using (Graphics g = Graphics.FromImage(rotatedImage))
{
g.TranslateTransform(tempRotatedImage.Width / 2, tempRotatedImage.Height / 2);
g.RotateTransform(angle);
g.TranslateTransform(-tempRotatedImage.Width / 2, -tempRotatedImage.Height / 2);
g.DrawImage(tempRotatedImage, new Point(0, 0));
}
Bitmap croped = CropTranslate(rotatedImage);
double wid = rotatedImage.Width;
double hig = rotatedImage.Height;
if (wid > 64 || hig > 64)
{
if (croped.Width > croped.Height)
{
wid = 64;
hig = (int)(64 / wid) * 64;
}
else
{
wid = (int)((64 / hig) * 64);
hig = 64;
}
}
Size newSize = new Size((int)wid , (int)hig );
Bitmap newBmp = new Bitmap(croped, newSize);
ImageConverter converter = new ImageConverter();
var bytes = (byte[])converter.ConvertTo(newBmp, typeof(byte[]));
return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, "test.png");
}
catch (Exception x)
{
WriteLog("cntrlr" + GetException(x));
}
return null;
}
private Bitmap CropTranslate(Bitmap bm)
{
int minX = 0;
int maxX = 0;
int minY = 0;
int maxY = 0;
int transparent = 0;//Alpha code of translucent is 0
int translucent = 254;//Alpha code of translucent is 1-254
for (int i = 0; i < bm.Width; i++)
{
bool trnsprnt = true;
for (int j = 0; j < bm.Height; j++)
{
byte alphaValue = bm.GetPixel(i, j).A;
if (alphaValue == transparent)
{ }
else if (alphaValue <= translucent)
{ }
else
{ trnsprnt = false; break; }
}
if (trnsprnt)
{
minX = i < 6 ? 0 : i - 5;
//maxX = i;
}
else
{
break;
}
}
for (int i = bm.Width - 1; i >= 0; i--)
{
bool trnsprnt = true;
for (int j = 0; j < bm.Height; j++)
{
byte alphaValue = bm.GetPixel(i, j).A;
if (alphaValue == transparent)
{ }
else if (alphaValue <= translucent)
{ }
else
{ trnsprnt = false; break; }
}
if (trnsprnt)
{
maxX = i > bm.Width - 6 ? bm.Width : i + 5;
}
else
{
break;
}
}
for (int i = 0; i < bm.Height; i++)
{
bool trnsprnt = true;
for (int j = 0; j < bm.Width; j++)
{
byte alphaValue = bm.GetPixel(j, i).A;
if (alphaValue == transparent)
{ }
else if (alphaValue <= translucent)
{ }
else
{ trnsprnt = false; break; }
}
if (trnsprnt)
{
minY = i < 6 ? 0 : i - 5;
}
else
{
break;
}
}
for (int i = bm.Height - 1; i >= 0; i--)
{
bool trnsprnt = true;
for (int j = 0; j < bm.Width; j++)
{
byte alphaValue = bm.GetPixel(j, i).A;
if (alphaValue == transparent)
{ }
else if (alphaValue <= translucent)
{ }
else
{ trnsprnt = false; break; }
}
if (trnsprnt)
{
maxY = i > bm.Height - 6 ? bm.Height : i + 5;
}
else
{
break;
}
}
int width = maxX - minX;
int height = maxY - minY;
Rectangle cropRect = new Rectangle(minX, minY, width, height);
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(bm, new Rectangle(0, 0, target.Width, target.Height),
cropRect,
GraphicsUnit.Pixel);
}
return target;
}
解决方案
推荐阅读
- c++ - 测试程序未运行
- python - ValueError:索引和数据应该具有相同的大小
- c# - EPPlus - 在 EPP5 中运行 Workbook.Calculate 时,不计算带有通配符的计数
- c# - 强制本地化在特定控制器中使用特定语言
- c# - .Add() 方法在 C# 中使用“for”循环构建列表时不起作用
- flutter - 如果它已经在索引堆栈中初始化,我如何将值传递给小部件?
- nginx - Laravel 8 URL 字符串,带有重复的 URL 名称作为参数
- r - 如何抑制 rmarkdown 文档中的下载进度消息/警告/r 中的闪亮?
- webrtc - WebRTC;点 1 到点 2 的共享屏幕连接已建立,轨道已发送但没有视频
- kubernetes - gcsfuse 在 GKE 上自行终止