Visualizzazione dei risultati da 1 a 4 su 4

Discussione: GetThumbnailImage

  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    2

    GetThumbnailImage

    Ciao a tutti.
    Sono nuovo nel forum. Mi sto avvicinando al .NET da poco... Ho implementato una funzione di upload con ridimensionamento dell'immagine e creazione della sua thumbnail, ma i risultati non sono molto belli. Funziona tutto perfettamente ma la qualità dell'immagine risultante è indecente (come se fosse uno di quei jpeg con qualità 10% !!!!).

    Il bello è che io uso ImageFormat.Png(formato direi migliore)... ed anche usando .Jpeg si ottiene la stessa schifezza.

    Tutto in C#. Vi posto quelle che sono le righe essenziali

    String UploadedFile = MyFile.PostedFile.FileName;
    int ExtractPos = UploadedFile.LastIndexOf("\\") + 1;

    String UploadedFileName = UploadedFile.Substring(ExtractPos,UploadedFile.Len gth - ExtractPos);



    FileName.InnerHtml = UploadedFileName;
    MyContentType.InnerHtml = MyFile.PostedFile.ContentType;
    ContentLength.InnerHtml = MyFile.PostedFile.ContentLength.ToString();

    FileDetails.Visible = true;
    // salvo il file
    MyFile.PostedFile.SaveAs(Request.PhysicalApplicati onPath + "\\public\\upload\\" + UploadedFileName );



    if (imageUrl.IndexOf("/") >= 0 || imageUrl.IndexOf("\\") >= 0 )
    {
    //We found a / or \
    Response.End();
    }


    imageUrl = "public/upload/" + imageUrl;

    System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(image Url));

    // qui cè il codice che calcola le dimensioni delle 2 immagine ridimensionate (che non vi posto solo per motivi di lunghezza...


    System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback);

    System.Drawing.Image Img = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, null, IntPtr.Zero);
    System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(Lthumb, Hthumb, null, IntPtr.Zero);

    //We need to create a unique filename for each generated image

    DateTime MyDate = DateTime.Now;
    String adesso = MyDate.ToString("ddMMyyhhmmss");
    String MyString = adesso + ".jpg";
    String MyStringThumb = adesso + "_thumb.jpg";

    thumbNailImg.Save(Request.PhysicalApplicationPath + "\\public\\imageresize\\" + MyStringThumb, ImageFormat.Jpeg);
    Img.Save(Request.PhysicalApplicationPath + "\\public\\imageresize\\" + MyString, ImageFormat.Jpeg);

    thumbNailImg.Dispose();
    Img.Dispose();

    MyFile.Dispose();

    thumbNailImg = null;
    MyFile = null;
    Img = null;


    Ho provato anche con questa soluzione (dal msdn)


    System.Drawing.Imaging.ImageCodecInfo[] Info = System.Drawing.Imaging.ImageCodecInfo.GetImageEnco ders();
    System.Drawing.Imaging.EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(2);
    Params.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Qu ality, 100L);
    Params.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Co lorDepth, 24);
    Response.ContentType = Info[1].MimeType;

    e a metterli come paramentri ecc ecc ma non cambia nulla


    Qualcunno mi sa dire perchè? sicuramente sbaglierò.. ma non capisco dove se tutto funziona

    Grazie mille....ciao


    p.s. vi allego un risultato della foto per rendere meglio l'idea!!!
    Immagini allegate Immagini allegate

  2. #2
    cerca fra le mie discussioni... qualcosa trovi sicuro
    Richard B. Riddick : "Non avrete mica paura del buio?"

    Scusatemi ma sono mesi che non pago piu' la bolletta della luce....

  3. #3
    Medesimo tuo problema

    Guarda questa mia procedura e riadattala :



    codice:
    private bool SaveImage(Stream File,string FILE_ROOT,string FILE_NAME,ref string RETURN_CODE)
        {
            try
            {
                int AP_W = 0;
                int AP_H = 0;
    
                System.Drawing.Image Img = System.Drawing.Image.FromStream(File);
    
                System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
    
                // Resize and Save Image
                this.GetImageResizedValue(Img, ref AP_W, ref AP_H);
    
                Bitmap imgOutput = new Bitmap(Img, AP_W, AP_H); 
    
                Graphics myresizer;
                myresizer = Graphics.FromImage(imgOutput);
                myresizer.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                myresizer.DrawImage(Img, 0, 0, AP_W, AP_H);
    
                imgOutput.Save(FILE_ROOT + "\\" + FILE_NAME, System.Drawing.Imaging.ImageFormat.Jpeg);
    
                // Resize and Save Thumbnail
                this.GetThumbnailsValue(Img, ref AP_W, ref AP_H);
    
                imgOutput = new Bitmap(Img, AP_W, AP_H);
    
                myresizer = Graphics.FromImage(imgOutput);
                myresizer.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                myresizer.DrawImage(Img, 0, 0, AP_W, AP_H);
    
                imgOutput.Save(FILE_ROOT + "\\" + ConfigurationManager.AppSettings["IMAGE_PUBLIC_THUMBNAILS"].ToString() + "\\" + FILE_NAME, System.Drawing.Imaging.ImageFormat.Jpeg);
    
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }
    In teoria basta che gli passi l'immagine . . .

    Questa salva tutto (gif , jpg , png) in Jpg quindi occhio . . .

    Una foto da 750 kb la riduce a 800 x 600 in soli 50kb

    Fammi sapere

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    2

    Ottimo!!

    Grazie mille a tutti!
    Quest'ultimo adattato funziona benissimo...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.