SafeDispatch/Safedispatch_4_0/Helpers/IconHelper.cs

229 lines
8.4 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using Safedispatch_4_0;
using System;
using System.Drawing;
using System.IO;
namespace Dispatcher.Helpers
{
class IconHelper
{
public static String GetIconPathForUnit(String unitName)
{
String iconPath = "";
if ((Vehicle)MainForm2.vehicleHT[unitName] != null)
{
Car crtCar = ((Car)MainForm2.carlist[((Vehicle)MainForm2.vehicleHT[unitName])?.IconID]);
if (crtCar == null)
{
SafeMobileLib.Utils.WriteLine("nullllll", ConsoleColor.Cyan);
return "unknown";
}
iconPath = "";
iconPath = crtCar.listFilePath;
}
return iconPath;
}
public static Image GetIconForUnit(Vehicle veh)
{
Car crtCar = ((Car)MainForm2.carlist[veh.IconID]);
if (crtCar == null)
{
SafeMobileLib.Utils.WriteLine("Null car type", ConsoleColor.Cyan);
return null;
}
String iconPath = crtCar.listFilePath;
try
{
Bitmap bmp = new Bitmap(iconPath);
Image tmp2 = SafeMobileLib.Utils.ScaleImage(bmp, 64, 64); //tmp.ToBitmap();
tmp2.Tag = crtCar.listFilePath;
// add image to the dictionary if not exists
if (!MainForm2.imagesDictionary.ContainsKey(crtCar.listFilePath))
MainForm2.imagesDictionary.Add(crtCar.listFilePath, tmp2);
// set the icon for the unit
return tmp2;
}
catch (Exception)
{
SafeMobileLib.Utils.WriteLine("Grid icon could not be found [" + iconPath + "]");
return null;
}
}
public static void CheckCreateTransparentIcon(int iconID)
{
CheckCreateTransparentIcon(iconID, 50);
}
public static void CheckCreateTransparentIcon(int iconID, int opacityPercentage)
{
Car crtCar = ((Car)MainForm2.carlist[iconID]);
if (crtCar == null)
{
SafeMobileLib.Utils.WriteLine("nullllll", ConsoleColor.Cyan);
return;
}
String iconPath = crtCar.fileName;
try
{
if (File.Exists(iconPath.Replace(".png", "_" + opacityPercentage + "o.png")))
{
// emergency icon was created i can return
return;
}
else
{
Bitmap bmp = new Bitmap(iconPath);
bmp = SafeMobileLib.Utils.MakeTransparent(bmp, (opacityPercentage*1.0f)/100);
bmp = SafeMobileLib.Utils.ChangeColor(bmp, Color.FromArgb(255, 10, 0));
bmp.Save(iconPath.Replace(".png", "_" + opacityPercentage + "o.png"));
// Dispose
bmp.Dispose();
}
}
catch (Exception ex)
{
SafeMobileLib.Utils.WriteLine("CheckCreateTransparentIcon Exception: " + ex.ToString(), ConsoleColor.Red);
}
}
public static void CheckCreateEmergencyIcon(int iconID)
{
Car crtCar = ((Car)MainForm2.carlist[iconID]);
if (crtCar == null)
{
SafeMobileLib.Utils.WriteLine("nullllll", ConsoleColor.Cyan);
return;
}
String iconPath = crtCar.fileName;
try
{
if (File.Exists(iconPath.Replace(".png", "_emerg.png")))
{
// emergency icon was created i can return
return;
}
else
{
Bitmap bmp = new Bitmap(iconPath);
bmp = SafeMobileLib.Utils.MakeGrayscale3(bmp, 1f);
bmp = SafeMobileLib.Utils.ChangeColor(bmp, Color.FromArgb(255, 10, 0));
bmp.Save(iconPath.Replace(".png", "_emerg.png"));
// Dispose
bmp.Dispose();
}
}
catch (Exception ex)
{
SafeMobileLib.Utils.WriteLine("CheckCreateEmergencyIcon: " + ex.ToString());
}
}
public static void CheckCreateNamedIcon(int iconID, String vehicleName, int sc_id)
{
Car crtCar = ((Car)MainForm2.carlist[iconID]);
if (crtCar == null)
{
SafeMobileLib.Utils.WriteLine("nullllll", ConsoleColor.Cyan);
return;
}
String iconPath = crtCar.fileName;
String labeledFileExtension = "_" + iconID + "_" + sc_id + ".png";
try
{
if (File.Exists(iconPath.Replace(".png", labeledFileExtension)))
{
// named icon was created i can return
return;
}
else
{
using (Bitmap bmp = new Bitmap(iconPath))
{
using (Graphics gr1 = Graphics.FromImage(bmp))
{
// Set up string.
Font stringFont = new Font("Segoe UI", 8, FontStyle.Regular);
SizeF stringSize = gr1.MeasureString(vehicleName, stringFont);
stringSize.Width += 4;
stringSize.Height += 2;
using (Bitmap emptyCanvas = new Bitmap(bmp.Width + (int)stringSize.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
using (Graphics gr = Graphics.FromImage(emptyCanvas))
{
// Draw pin with no affects
gr.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
// Draw rectangle representing size of string.
gr.FillRectangle(new SolidBrush(Color.FromArgb(255, 40, 123, 167)), bmp.Width / 2, bmp.Height - stringSize.Height, stringSize.Width + bmp.Width / 2, stringSize.Height);
// Draw string to screen.
gr.DrawString(vehicleName, stringFont, Brushes.White, new PointF(bmp.Width / 2, bmp.Height - stringSize.Height + 1));
/*
// Draw image with ImageAttributes
gr.DrawImage(bmp, new Rectangle(0, 0, landmarkPin.Width, landmarkPin.Height), 0, 0,
landmarkPin.Width, landmarkPin.Height, GraphicsUnit.Pixel, imageAttributes);
// add landmark type icon
string iconPath = safemobileDirPath + @"\resource\landmarks\" + lt.IconName;
using (Image img = Utils.ScaleImage(Bitmap.FromFile(iconPath), 44, 44))
{
gr.DrawImage(img, 19, 18);
}
*/
Image resizedImage = SafeMobileLib.Utils.ScaleImage(emptyCanvas, emptyCanvas.Width, emptyCanvas.Height);
resizedImage.Save(iconPath.Replace(".png", labeledFileExtension));
//bitmap.Save(iconNameAndPath);
emptyCanvas.Dispose();
// Dispose
bmp.Dispose();
resizedImage.Dispose();
}
}
}
};
/*
Bitmap bmp = new Bitmap(iconPath);
bmp = Utils.ChangeColor(bmp, Color.FromArgb(255, 0, 0));
bmp.Save(iconPath.Replace(".png", "_emerg.png"));
// Dispose
bmp.Dispose();*/
}
}
catch (Exception ex)
{
SafeMobileLib.Utils.WriteLine("CheckCreateNamesIcon: " + ex.ToString());
}
}
}
}