Мне нужно использовать пользовательские значки в разных условиях на карте Google. В моей программе есть функция, которая создает это изображение.В некоторых случаях фотография из Google верна, а в некоторых случаях значок по умолчанию размещается на карте.
Генерируемый URL:
http://maps.googleapis.com/maps/api/staticmap?center=29.627650000000000,52.518450000000000&zoom=18&size=500x500&maptype=roadmap&markers=icon:http://example.com/Testsvg/icons4?angle=45|29.627500000000000,52.518300000000000&markers=icon:http://example.com/Testsvg/icons4?angle=45|29.627800000000000,52.518600000000000&key=myKey
Фотография, сгенерированная в моем приложении в правильном режиме ( 64 * 34, 1,1 КБ ):
![Photo generated in my application in correct mode (**64*34, 1.1 KB**)](https://i.stack.imgur.com/C0NXl.png)
Фотография, возвращенная Google в правильном режиме:
![Photo returned by Google in correct mode](https://i.stack.imgur.com/DKmBg.png)
И:
Фотография, сгенерированная в моем приложении в неправильном режиме ( 64 * 64, 3,5 КБ ):
![Photo generated in my application in wrong mode (**64*64, 3.5 KB**)](https://i.stack.imgur.com/7s8bP.png)
Фотография, возвращенная Google в неправильном режиме:
![Photo returned by Google in wrong mode](https://i.stack.imgur.com/KB7HP.png)
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;
}