Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    script sostitutivo a getThumbnailImage

    Ciao a tutti,
    ho un problema sul resize di alcune immagini: usando getThumbnailImage infatti, a seconda della foto JPG questa viene sgranata dopo il resize (nota: lo fa solo con alcune foto, penso sia un problema di codifica interna delle immagini jpg) Ho dunque letto che è meglio usare un altro metodo per il resize di immagini di una certa dimensione...mi sapete dunque indicare dove posso trovare uno script sostitutivo a quello che uso attualmente, cioè:

    (N.b: lo script permette di definire subito, richiamando l'immagine, il tipo di resize)

    codice:
    <%@ Page Language="C#" %>
    <%@ import Namespace="System.Drawing" %>
    <%@ import Namespace="System.Drawing.Imaging" %>
    <script runat="server">
     
    void Page_Load(Object sender, System.EventArgs e)
    {
      if (Request.QueryString["img"] == null) return;
     
      string imgName = Convert.ToString(Request.QueryString["img"]);
      if (imgName == "") return;
     
      string func = "";
      if (Request.QueryString["fn"] != null) func = Convert.ToString(Request.QueryString["fn"]);
      if (func != "g" && func != "z" && func != "a") func = "l";
     
      // ZoomX di default = 100%, oppure larghezza massima/larghezza assoluta di default = 150 pixel
      float resize_w = (func == "z") ? 100 : 150;
      if (Request.QueryString["w"] != null) resize_w = Convert.ToSingle(Request.QueryString["w"]);
     
      // ZoomY di default = ZoomX, oppure altezza massima/altezza assoluta di default = 100 pixel
      float resize_h = (func == "z") ? resize_w : 100;
      if (Request.QueryString["h"] != null) resize_h = Convert.ToSingle(Request.QueryString["h"]);
      
     
      try
        {
          // Carica l'immagine originale e ottieni le sue dimensioni
          System.Drawing.Image origImage = System.Drawing.Image.FromFile(Server.MapPath(imgName));
          int w = origImage.Width;
          int h = origImage.Height;
     
          if (func == "z")
            {
              // La funzione è uno zoom
     
              // Se lo zoom corrisponde al 100% per tutti e due gli assi, imposta 'no function'
              if (resize_w == 100 && resize_h == 100) func = "";
              else
                {
                  // Massimo 1000% per ambedue gli assi
                  if (resize_w > 1000) resize_w = 1000;
                  if (resize_h > 1000) resize_h = 1000;
     
                  w = Convert.ToInt32((w * resize_w) / 100);
                  h = Convert.ToInt32((h * resize_h) / 100);
                }
            }
          else
            {
              int limit_w = Convert.ToInt32(resize_w);
              int limit_h = Convert.ToInt32(resize_h);
     
              if (func == "a")
                {
                  // La funzione è un ridimensionamento assoluto (l'aspect ratio è ignorato)
     
                  w = limit_w;
                  h = limit_h;
                }
              else
                {
                  // La funzione è una limitazione ad un massimo di dimensioni ('l' oppure 'g')
     
                  if (w > limit_w || h > limit_h)
                    {
                      // L'immagine supera le dimensioni massime, in questo caso, 'l' e 'g' sono equivalenti,
                      // ricalcola
     
                      if (w > ((h * limit_w) / limit_h))
                        {
                          h = Convert.ToInt32((h * limit_w) / w);
                          w = limit_w;
                        }
                      else
                        {
                          w = Convert.ToInt32((w * limit_h) / h);
                          h = limit_h;
                        }
                    }
                  else if (func == "g")
                    {
                      // La funzione 'g' (grow), ingrandisce l'immagine fino al massimo
                      // dei limiti se questa è più piccola
     
                      if (w < limit_w && h < limit_h)
                        {
                          // L'immagine può essere ingrandita, ricalcola
     
                          if (limit_w <= ((w * limit_h) / h))
                            {
                              h = Convert.ToInt32((h * limit_w) / w);
                              w = limit_w;
                            }
                          else
                            {
                              w = Convert.ToInt32((w * limit_h) / h);
                              h = limit_h;
                            }
                        }
                      // L'immagine non è più piccola dei limiti, imposta 'no function'
                      else func = "";
                    }
                  // L'immagine non supera le dimensioni massime, imposta 'no function'
                  else func = "";
                }
            }
     
          // Limite delle dimensioni minime, a scapito dell'aspect ratio
          if (w < 10) w = 10;
          if (h < 10) h = 10;
     
          Response.ContentType = "image/jpeg";
     
          if (func == "")  // 'no function'
            {
              // L'immagine non supera le dimensioni massime oppure lo zoom
              // corrisponde al 100%, output dell'immagine originale
     
              origImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
          else
            {
              // L'immagine deve essere ridimensionata con GetThumbnailImage()
     
              System.Drawing.Image newImage = origImage.GetThumbnailImage(w, h, null, IntPtr.Zero);
              newImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
     
              newImage.Dispose();
            }
     
          origImage.Dispose();
        }
      catch (Exception ex)
        {
          Response.Write(ex.Message);
        }
    }
     
    </script>
    grazie
    Where there is a will, there is a way

  2. #2
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,887
    Vedi thread in rilievo sui link e le discussioni utili...

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 © 2026 vBulletin Solutions, Inc. All rights reserved.