2025-06-03 15:11:16 +02:00

505 lines
17 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Dundas.Charting.WebControl;
using System.ComponentModel;
using System.IO;
using System.Net.NetworkInformation;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace PDFGenerator
{
public class BarreCircolari : IDisposable
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public enum _ProgressShape
{
Round,
Flat
}
public enum _TextMode
{
None,
Value,
Percentage,
Custom
}
private long _Value;
private long _Maximum = 100;
private int _LineWitdh = 1;
private float _BarWidth = 14f;
private System.Drawing.Color _ProgressColor1 = Color.Orange;
private Color _ProgressColor2 = Color.Orange;
private Color _ProgressColor3 = Color.Orange;
private Color _ProgressColor4 = Color.Orange;
private Color _LineColor = Color.Silver;
private LinearGradientMode _GradientMode = LinearGradientMode.Vertical;
private _ProgressShape ProgressShapeVal;
private _TextMode ProgressTextMode;
private long _SecondBarValue;
private Color _SecondBarColor1 = Color.Blue;
private Color _SecondBarColor2 = Color.Blue;
private float _SecondBarWidth = 12f;
private int _Width;
private int _Height;
private string Text;
private Color BackColor;
private Color ForeColor;
private Font Font;
private Rectangle ClientRectangle;
private Size Size;
public string pathImmagine { get; set; }
public Bitmap outImmagine { get; set; }
public void AggiornaValoriCerchio(ref decimal num1, ref decimal num2)
{
// Estrai la parte intera dei numeri
int intPart1 = (int)num1;
int intPart2 = (int)num2;
// Controlla se le parti intere sono uguali
if (intPart1 == intPart2)
{
// Se una delle parti intere è 0
if (intPart1 == 0 || intPart2 == 0)
{
// Aumenta la parte intera del numero maggiore di 2
if (intPart1 > intPart2)
{
num1 = (int)num1 + 2 + (num1 - intPart1); // Aggiunge 2 alla parte intera
num2 = (int)num2 + 1 + (num2 - intPart2); // Aggiunge 1 alla parte intera
}
else
{
num1 = (int)num1 + 1 + (num1 - intPart1); // Aggiunge 1 alla parte intera
num2 = (int)num2 + 2 + (num2 - intPart2); // Aggiunge 2 alla parte intera
}
}
else
{
// Aumenta solo la parte intera del numero maggiore di 1
if (intPart1 > intPart2)
{
num1 = (int)num1 + 1 + (num1 - intPart1); // Aggiunge 1 alla parte intera
}
else
{
num2 = (int)num2 + 1 + (num2 - intPart2); // Aggiunge 1 alla parte intera
}
}
}
}
public BarreCircolari(int Width, int Height, string _Text, Font font,
Color _Backcolor, Color forecolor, Color b1, Color b2,
Rectangle rectangle, long value, long _SecondBarValue)
{
_Value = value;
ProgressShape = _ProgressShape.Flat;
TextMode = _TextMode.None; //_TextMode.Percentage;
BarColor1 = b1; // Color.FromArgb(196, 189, 151);
BarColor2 = b1; //Color.FromArgb(196, 189, 151);
BarWidth = 25;
SecondBarColor1 = b2; //Color.FromArgb(148, 138, 84);
SecondBarColor2 = b2; //Color.FromArgb(148, 138, 84);
SecondBarWidth = 14;
SecondBarValue = _SecondBarValue; // parametro di cui
LineColor = Color.FromArgb(217, 217, 217);
LineWidth = 25;
_Width = Width;
_Height = Height;
Text = _Text;
BackColor = _Backcolor;
ForeColor = forecolor;
ClientRectangle = rectangle;
Size = new Size(130, 130);
Font = font; //new Font("Segoe UI", 15);
SetStandardSize();
}
/// <summary>Determina el Valor del Progreso</summary>
[Description("Valor Entero que determina la posision de la Barra de Progreso."), Category("Behavior")]
public long Value
{
get { return _Value; }
set
{
if (value > _Maximum)
value = _Maximum;
_Value = value;
}
}
[Description("Obtiene o Establece el Valor Maximo de la barra de Progreso."), Category("Behavior")]
public long Maximum
{
get { return _Maximum; }
set
{
if (value < 1)
value = 1;
_Maximum = value;
}
}
[Description("Color Inicial de la Barra de Progreso"), Category("Appearance")]
public Color BarColor1
{
get { return _ProgressColor1; }
set
{
_ProgressColor1 = value;
}
}
[Description("Color Final de la Barra de Progreso"), Category("Appearance")]
public Color BarColor2
{
get { return _ProgressColor2; }
set
{
_ProgressColor2 = value;
}
}
[Description("Ancho de la Barra de Progreso"), Category("Appearance")]
public float BarWidth
{
get { return _BarWidth; }
set
{
_BarWidth = value;
}
}
[Description("Modo del Gradiente de Color"), Category("Appearance")]
public LinearGradientMode GradientMode
{
get { return _GradientMode; }
set
{
_GradientMode = value;
}
}
[Description("Color de la Linea Intermedia"), Category("Appearance")]
public Color LineColor
{
get { return _LineColor; }
set
{
_LineColor = value;
}
}
[Description("Ancho de la Linea Intermedia"), Category("Appearance")]
public int LineWidth
{
get { return _LineWitdh; }
set
{
_LineWitdh = value;
}
}
[Description("Obtiene o Establece la Forma de los terminales de la barra de progreso."), Category("Appearance")]
public _ProgressShape ProgressShape
{
get { return ProgressShapeVal; }
set
{
ProgressShapeVal = value;
}
}
[Description("Obtiene o Establece el Modo como se muestra el Texto dentro de la barra de Progreso."), Category("Behavior")]
public _TextMode TextMode
{
get { return ProgressTextMode; }
set
{
ProgressTextMode = value;
}
}
//[Description("Obtiene el Texto que se muestra dentro del Control"), Category("Behavior")]
//public string Text
//{
// get ( return _TextMode;}
// set
// {
// Text = ValueType;
// };
//}
[Description("Valore della seconda barra di avanzamento."), Category("Behavior")]
public long SecondBarValue
{
get { return _SecondBarValue; }
set
{
if (value > _Maximum)
value = _Maximum;
_SecondBarValue = value;
}
}
[Description("Colore iniziale della seconda barra di avanzamento."), Category("Appearance")]
public Color SecondBarColor1
{
get { return _SecondBarColor1; }
set
{
_SecondBarColor1 = value;
}
}
[Description("Colore finale della seconda barra di avanzamento."), Category("Appearance")]
public Color SecondBarColor2
{
get { return _SecondBarColor2; }
set
{
_SecondBarColor2 = value;
}
}
[Description("Larghezza della seconda barra di avanzamento."), Category("Appearance")]
public float SecondBarWidth
{
get { return _SecondBarWidth; }
set
{
_SecondBarWidth = value;
}
}
private void SetStandardSize()
{
int _Size = Math.Max(_Width, _Height);
Size = new Size(_Size, _Size);
}
public void OnPaintNew(string imagePath, string nomeFileOutput, bool save = true)
{
Bitmap bitmap = new Bitmap(this._Width, this._Height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
// Impostazione della qualità grafica
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//graphics.Clear(Color.Transparent); //<-- this.BackColor, SystemColors.Control, Color.Transparent
//PaintTransparentBackground(this, e);
//Disegna il cerchio bianco interno:
using (Brush mBackColor = new SolidBrush(this.BackColor))
{
graphics.FillEllipse(mBackColor,
18, 18,
(this._Width - 0x30) + 12,
(this._Height - 0x30) + 12);
}
// Disegna la sottile linea grigia al centro:
using (Pen pen2 = new Pen(LineColor, this.LineWidth))
{
graphics.DrawEllipse(pen2,
18, 18,
(this._Width - 0x30) + 12,
(this._Height - 0x30) + 12);
}
//Disegna la barra di avanzamento
using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle,
this._ProgressColor1, this._ProgressColor2, this.GradientMode))
{
using (Pen pen = new Pen(brush, this.BarWidth))
{
switch (this.ProgressShapeVal)
{
case _ProgressShape.Round:
pen.StartCap = LineCap.Round;
pen.EndCap = LineCap.Round;
break;
case _ProgressShape.Flat:
pen.StartCap = LineCap.Flat;
pen.EndCap = LineCap.Flat;
break;
}
//Qui viene effettivamente disegnata la barra di avanzamento
graphics.DrawArc(pen, 0x12, 0x12, (this._Width - 0x23) - 2, (this._Height - 0x23) - 2, -90, (int)Math.Round((double)((-360.0 / ((double)this._Maximum)) * this._Value)));
}
}
using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle,
this._SecondBarColor1, this._SecondBarColor2, this.GradientMode))
{
using (Pen pen = new Pen(brush, this.SecondBarWidth))
{
switch (this.ProgressShapeVal)
{
case _ProgressShape.Round:
pen.StartCap = LineCap.Round;
pen.EndCap = LineCap.Round;
break;
case _ProgressShape.Flat:
pen.StartCap = LineCap.Flat;
pen.EndCap = LineCap.Flat;
break;
}
// Draw the second progress bar
graphics.DrawArc(pen,
0x12, 0x12,
(this._Width - 0x23) - 2,
(this._Height - 0x23) - 2,
-90,
(int)Math.Round((double)((-360.0 / ((double)this._Maximum)) * this._SecondBarValue)));
}
}
switch (this.TextMode)
{
case _TextMode.None:
this.Text = string.Empty;
break;
case _TextMode.Value:
this.Text = _Value.ToString();
break;
case _TextMode.Percentage:
this.Text = Convert.ToString(Convert.ToInt32((100 / _Maximum) * _Value));
break;
default:
break;
}
if (this.Text != string.Empty)
{
using (Brush FontColor = new SolidBrush(this.ForeColor))
{
int ShadowOffset = 2;
SizeF MS = graphics.MeasureString(this.Text, this.Font);
SolidBrush shadowBrush = new SolidBrush(Color.FromArgb(100, this.ForeColor));
//Ombra del testo:
graphics.DrawString(this.Text, this.Font, shadowBrush,
Convert.ToInt32(_Width / 2 - MS.Width / 2) + ShadowOffset,
Convert.ToInt32(_Height / 2 - MS.Height / 2) + ShadowOffset
);
//Controllo del testo:
graphics.DrawString(this.Text, this.Font, FontColor,
Convert.ToInt32(_Width / 2 - MS.Width / 2),
Convert.ToInt32(_Height / 2 - MS.Height / 2));
}
}
// Draw the image in the center of the control
//string imagePath = @"C:\Sviluppo\Locale\GraficoAnello\PAI_A.png";
if (File.Exists(imagePath))
{
Image image = Image.FromFile(imagePath); // FastLoad(imagePath);
float aspectRatio = (float)image.Width / image.Height;
float maxWidth = _Width * 0.95f; // You can adjust this factor to control the maximum width of the image relative to the control width
float maxHeight = _Height * 0.95f; // You can adjust this factor to control the maximum height of the image relative to the control height
float imageWidth = Math.Min(image.Width, maxWidth);
//float imageHeight = Math.Min(image.Height, maxHeight);
float imageHeight = imageWidth / aspectRatio;
// Maintain the aspect ratio when resizing
if (aspectRatio > 1) // Landscape-oriented image
{
imageWidth = Math.Min(image.Width, maxWidth);
imageHeight = imageWidth / aspectRatio;
}
else // Portrait-oriented image
{
imageHeight = Math.Min(image.Height, maxHeight);
imageWidth = imageHeight * aspectRatio;
}
if (imageHeight > maxHeight)
{
imageHeight = maxHeight;
imageWidth = imageHeight * aspectRatio;
}
float imageX = (_Width - imageWidth) / 2f;
float imageY = (_Height - imageHeight) / 2f;
//float imageX = ((Width) - image.Width) / 2f;
//float imageY = ((Height)- image.Height) / 2f;
var wP = (imageWidth * bitmap.Width) / 100;
var hP = (imageHeight * bitmap.Height) / 100;
//image.Save(Application.StartupPath + @"\CerchioConArchi.png", ImageFormat.Png);
graphics.DrawImage(image, imageX, imageY, imageWidth, imageHeight);
//graphics.DrawImageUnscaled(image, 100, 100, (int)imageWidth , (int)imageHeight);
}
}
if (save)
bitmap.Save(nomeFileOutput, ImageFormat.Png);
outImmagine = bitmap;
}
public void DisposeBitmap(Bitmap daDistruggere)
{
daDistruggere.Dispose();
}
public Image FastLoad(string path)
{
using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path)))
return Image.FromStream(ms);
}
public void Dispose()
{
outImmagine = null;
}
}
}