2016-06-16 6 views
6

Desidero comprimere la foto di caricamento nella dimensione Height =600px & Width= 800px [solo se la dimensione effettiva è maggiore di questa] durante il tempo di caricamento.Le foto sono risparmiando su sql server2008 con un'immagine tipo di dati!Come comprimere l'altezza e la larghezza di una foto durante il caricamento [asp.net]

public byte[] imagetoByte() 
     { 
      if (FileUpload1.HasFile) 
      { 
       int imageFilelength = FileUpload1.PostedFile.ContentLength; 
       byte[] ph = new byte[imageFilelength]; 
       HttpPostedFile ima = FileUpload1.PostedFile; 
       MemoryStream memoryStream = new MemoryStream(); 
       ima.InputStream.Read(ph, 0, imageFilelength); 
       return ph; 
      } 
      else 
      { 
       return null; 
      } 

     } 

la funzione viene utilizzata per il caricamento di immagini !! qualcuno mi aiuti a risolvere questo problema ..

risposta

0

Possibile soluzione può essere:

int imageFilelength = FileUpload1.PostedFile.ContentLength; 
byte[] ph = new byte[imageFilelength]; 

MemoryStream ms = new MemoryStream(ph); 
Image img = System.Drawing.Image.FromStream(ms); 

//Call function to resize 
Image ResizedImage = RezizeImage(img, 500, 500); 

//Save Image 
ResizedImage.Save("IMAGELOCATION.png", System.Drawing.Imaging.ImageFormat.Gif); 

private Image RezizeImage(Image img, int maxWidth, int maxHeight) 
{ 
    if(img.Height < maxHeight && img.Width < maxWidth) return img; 
    using (img) 
    { 
     Double xRatio = (double)img.Width/maxWidth; 
     Double yRatio = (double)img.Height/maxHeight; 
     Double ratio = Math.Max(xRatio, yRatio); 
     int nnx = (int)Math.Floor(img.Width/ratio); 
     int nny = (int)Math.Floor(img.Height/ratio); 
     Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb); 
     using (Graphics gr = Graphics.FromImage(cpy)) 
     { 
      gr.Clear(Color.Transparent); 

      // This is said to give best quality when resizing images 
      gr.InterpolationMode = InterpolationMode.HighQualityBicubic; 

      gr.DrawImage(img, 
       new Rectangle(0, 0, nnx, nny), 
       new Rectangle(0, 0, img.Width, img.Height), 
       GraphicsUnit.Pixel); 
     } 
     return cpy; 
    } 

} 
+0

Immagine img = System.Drawing.Image.FromStream (ms); la dichiarazione sta mostrando errore ... dice "il Parametro non è valido" .. !! – Aju

+0

Suppongo che tu abbia capito il problema. vota se questa risposta ti ha aiutato. Grazie – Sami

Problemi correlati