Funzione "ScriviJPG400px".
Scrive un file JPEG.
private bool ScriviJPG400px(string strSorgente, string strDestinazione, bool b) { // Scrittura del file JPG finale double scaleFactor; double picH; // Alt. Picture Box double picW; // Largh. PictureBox double imgH; // Alt. Immagine double imgW; // Largh. Immagine int imgFinalH; // Alt. Immagine Scalata int imgFinalW; // Largh. Immagine Scalata double rapportoDimensioni; bool isBalanced = true; Bitmap bmp2; //Dimensioni dell'immagine in pixel //N.B.: picImage e' un oggetto immagine, qui non usato // Default picH = 400; //picImage.Height picW = 400; //picImage.Width if (b == false) { picH = 50; picW = 50; } try { //Bitmap dal File Trovato su Disco Bitmap bmp = new Bitmap(strSorgente); pictureBox1.Image = bmp; pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; pictureBox1.Refresh(); imgH = bmp.Height; imgW = bmp.Width; //Il fattore di scala sara' dato dalla frazione che ha //per numeratore il max fra picH e picW //e per dividendo il max fra imgH e imgW //La bitmap da visualizzare scaleFactor = System.Math.Max(picH, picW) / System.Math.Max(imgH, imgW); //Le immagini che hanno dimensioni inferiori alla max dimensione (400 px o 50 px) //rimangono invariate if (scaleFactor > 1) scaleFactor = 1; imgFinalH = (int)(imgH * scaleFactor); imgFinalW = (int)(imgW * scaleFactor); // Analisi rapporto fra dimensioni: rapportoDimensioni = System.Math.Min(imgH, imgW) / System.Math.Max(imgH, imgW) * 100; isBalanced = true; /* if (rapportoDimensioni < 50) isBalanced = false; if (!isBalanced) { if (imgFinalH >= imgFinalW) { bmp2 = new Bitmap(imgFinalW + imgFinalH / 2, imgFinalH); } else { bmp2 = new Bitmap(imgFinalW, imgFinalH + imgFinalW / 2); } } else { bmp2 = new Bitmap(imgFinalW, imgFinalH); } */ // rapportoDimensioni si legge come percentuale del valore max per ottenere il valore min. // Valore 50 => una delle due dimensioni e' la meta' dell'altra // Valore 70 => la dimensione piu' piccola e' il 70% di quella piu' grande. // Valore 30 => la dimensione piu' piccola e' il 30% di quella piu' grande, sbilanciata. // Per un valore inferiore a 50 si intende fare dei bordi bianchi all'immagine nella parte piu' piccola. // La misura di questi bordi bianchi deve diventare tale da avere un rapporto di almeno 50. bmp2 = new Bitmap(400, 400); bmp2.SetResolution(96, 96); Graphics g = Graphics.FromImage(bmp2); g.Clear(Color.White); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; if (!isBalanced) { // Scrivo l'immagine e la centro rispetto alla dimensione modificata. if (imgFinalH >= imgFinalW) { g.DrawImage(bmp, /* x */ bmp2.Width / 2 - imgFinalW / 2, /* y */ 0, bmp2.Width - bmp2.Height / 2, bmp2.Height); //g.DrawImage(bmp, 0, 0, bmp2.Width - bmp2.Height / 2, bmp2.Height); // OK, allineato a SX } else { g.DrawImage(bmp, 0, bmp2.Height / 2 - imgFinalH / 2, bmp2.Width, bmp2.Height - bmp2.Height / 2); //g.DrawImage(bmp, 0, 0, bmp2.Width, bmp2.Height - bmp2.Width / 2); // OK allineato a SX } } else { // Non devo fare nulla //g.DrawImage(bmp, 0, 0, bmp2.Width, bmp2.Height); // ISTRUZIONE ORIGINALE //g.DrawImage(bmp, new Point(0,0)); // OK, allineamento in alto a sx // g.DrawImage(bmp, 200 - bmp2.Width / 2, 200 - bmp2.Height / 2, imgFinalW, imgFinalH); // OK, Allineamento a sx g.DrawImage(bmp, 200 - imgFinalW / 2, 200 - imgFinalH / 2, imgFinalW, imgFinalH); // OK, Immagine centrata } //SCRITTURA DEL FILE JPG bmp2.Save(strDestinazione, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception eX) { //picImage.Image = null; RTBFilesTrovati.AppendText(eX.Message + "\n"); pictureBox1.Image = null; return false; } pictureBox1.Image = null; return true; } }