Torna al Thread
/// <summary>
/// Ridimensiona il logo per adattarlo al report
/// </summary>
/// <returns>Image</returns>
public static System.Drawing.Image ResizeLogoForReport(MemoryStream msLogo)
{
// recupero immagine
System.Drawing.Image imageFile = System.Drawing.Image.FromStream(msLogo);
//altezza e larghezza immagine
int w = imageFile.Width;
int h = imageFile.Height;
int size = w;
if (h > w)
{
size = h;
}
//controllo le dimensioni, se maggiori di 100 ridimensiono
int maxsize = 100;
if (size > (maxsize + 1))
{
size = maxsize;
if (w > h)
{
double rapp = (double)maxsize / (double)w;
w = maxsize;
h = Convert.ToInt32((double)h * rapp);
}
else
{
double rapp = (double)maxsize / (double)h;
h = maxsize;
w = Convert.ToInt32((double)w * rapp);
}
imageFile.GetThumbnailImage(w, h, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
MemoryStream imageStream = new MemoryStream();
imageFile.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
imageStream.Close();
}
//quadrato vuoto
Bitmap emptyBmp = new Bitmap(size, size);
// creazione di un oggetto di tipo Graphics
Graphics g = Graphics.FromImage(emptyBmp);
//sfondo
SolidBrush drawBrush = new SolidBrush(Color.White);
//disegno un rettangolo
Rectangle rectSfondo = new Rectangle(0, 0, size, size);
g.FillRectangle(drawBrush, rectSfondo);
//disegno un rettangolo
int pointX = 0;
int pointY = 0;
if (h > w)
{
pointX = ((h - w) / 2);
}
else
{
pointY = ((w - h) / 2);
}
Rectangle rect = new Rectangle(pointX, pointY, w, h);
drawBrush = new SolidBrush(Color.Black);
g.DrawImage(imageFile, rect);
// Generazione del punto di posizionamento del testo
Point A = new Point(pointX, pointY);
Point B = new Point(pointX + w, pointY);
Point C = new Point(pointX, pointY + h);
Point D = new Point(pointX + w, pointY + h);
//g.DrawRectangle(new Pen(drawBrush, 1), pointX-2, pointY-2,w+4,h+4);
g.DrawLine(new Pen(drawBrush, 1), A, B); // da A a B
g.DrawLine(new Pen(drawBrush, 1), C, D); //da C a D
g.DrawLine(new Pen(drawBrush, 1), A, C); //da A a C
g.DrawLine(new Pen(drawBrush, 1), B, D); //da B a D
// restituzione Image all'oggetto chiamante
g.Flush();
MemoryStream ms1 = new MemoryStream();
emptyBmp.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
ms1.Close();
return System.Drawing.Image.FromHbitmap(emptyBmp.GetHbitmap()); ;
}
/// <summary>
/// Required, but not used
/// </summary>
/// <returns>true</returns>
public static bool ThumbnailCallback()
{
return true;
}