2010-08-03 3 views
1

J'ai une classe qui m'aide à redimensionner mes images à une taille d'image spécifiée, mais le problème est avec les tailles de fichier des images de sortie-en fait, c'est un peu drôle, mais voici la chose:quelques problèmes avec les images réduites et leurs tailles de fichiers accrues en C#

l'image originale est: 800x600 24bit fichier de taille 96dpi: 82kb

l'image re-taille est: 466x340 24bit fichier de taille 96dpi: 366KB

que dois-je faire? Existe-t-il un composant tiers ou un projet Open Source selon ce problème?

ici est ma classe dit:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Drawing.Drawing2D; 


public class imageClass 
{ 
    public enum Dimensions 
    { 
     Width, 
     Height 
    } 
    public enum AnchorPosition 
    { 
     Top, 
     Center, 
     Bottom, 
     Left, 
     Right 
    } 

    public static Image ScaleByPercent(Image imgPhoto, int Percent) 
    { 
     float nPercent = ((float)Percent/100); 

     int sourceWidth = imgPhoto.Width; 
     int sourceHeight = imgPhoto.Height; 
     int sourceX = 0; 
     int sourceY = 0; 

     int destX = 0; 
     int destY = 0; 
     int destWidth = (int)(sourceWidth * nPercent); 
     int destHeight = (int)(sourceHeight * nPercent); 

     Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb); 
     bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); 

     Graphics grPhoto = Graphics.FromImage(bmPhoto); 
     grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 

     grPhoto.DrawImage(imgPhoto, 
      new Rectangle(destX, destY, destWidth, destHeight), 
      new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), 
      GraphicsUnit.Pixel); 

     grPhoto.Dispose(); 
     return bmPhoto; 
    } 
    public static Image ConstrainProportions(Image imgPhoto, int Size, Dimensions Dimension) 
    { 
     int sourceWidth = imgPhoto.Width; 
     int sourceHeight = imgPhoto.Height; 
     int sourceX = 0; 
     int sourceY = 0; 
     int destX = 0; 
     int destY = 0; 
     float nPercent = 0; 

     switch (Dimension) 
     { 
      case Dimensions.Width: 
       nPercent = ((float)Size/(float)sourceWidth); 
       break; 
      default: 
       nPercent = ((float)Size/(float)sourceHeight); 
       break; 
     } 

     int destWidth = (int)(sourceWidth * nPercent); 
     int destHeight = (int)(sourceHeight * nPercent); 

     Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb); 
     bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); 

     Graphics grPhoto = Graphics.FromImage(bmPhoto); 
     grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 

     grPhoto.DrawImage(imgPhoto, 
     new Rectangle(destX, destY, destWidth, destHeight), 
     new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), 
     GraphicsUnit.Pixel); 

     grPhoto.Dispose(); 
     return bmPhoto; 
    } 

    public static Image FixedSize(Image imgPhoto, int Width, int Height) 
    { 
     int sourceWidth = imgPhoto.Width; 
     int sourceHeight = imgPhoto.Height; 
     int sourceX = 0; 
     int sourceY = 0; 
     int destX = 0; 
     int destY = 0; 

     float nPercent = 0; 
     float nPercentW = 0; 
     float nPercentH = 0; 

     nPercentW = ((float)Width/(float)sourceWidth); 
     nPercentH = ((float)Height/(float)sourceHeight); 

     //if we have to pad the height pad both the top and the bottom 
     //with the difference between the scaled height and the desired height 
     if (nPercentH < nPercentW) 
     { 
      nPercent = nPercentH; 
      destX = (int)((Width - (sourceWidth * nPercent))/2); 
     } 
     else 
     { 
      nPercent = nPercentW; 
      destY = (int)((Height - (sourceHeight * nPercent))/2); 
     } 

     int destWidth = (int)(sourceWidth * nPercent); 
     int destHeight = (int)(sourceHeight * nPercent); 

     Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); 
     bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); 

     Graphics grPhoto = Graphics.FromImage(bmPhoto); 
     grPhoto.Clear(Color.Red); 
     grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 

     grPhoto.DrawImage(imgPhoto, 
      new Rectangle(destX, destY, destWidth, destHeight), 
      new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), 
      GraphicsUnit.Pixel); 

     grPhoto.Dispose(); 
     return bmPhoto; 
    } 

    public static Image Crop(Image imgPhoto, int Width, int Height, AnchorPosition Anchor) 
    { 
     int sourceWidth = imgPhoto.Width; 
     int sourceHeight = imgPhoto.Height; 
     int sourceX = 0; 
     int sourceY = 0; 
     int destX = 0; 
     int destY = 0; 

     float nPercent = 0; 
     float nPercentW = 0; 
     float nPercentH = 0; 

     nPercentW = ((float)Width/(float)sourceWidth); 
     nPercentH = ((float)Height/(float)sourceHeight); 

     if (nPercentH < nPercentW) 
     { 
      nPercent = nPercentW; 
      switch (Anchor) 
      { 
       case AnchorPosition.Top: 
        destY = 0; 
        break; 
       case AnchorPosition.Bottom: 
        destY = (int)(Height - (sourceHeight * nPercent)); 
        break; 
       default: 
        destY = (int)((Height - (sourceHeight * nPercent))/2); 
        break; 
      } 
     } 
     else 
     { 
      nPercent = nPercentH; 
      switch (Anchor) 
      { 
       case AnchorPosition.Left: 
        destX = 0; 
        break; 
       case AnchorPosition.Right: 
        destX = (int)(Width - (sourceWidth * nPercent)); 
        break; 
       default: 
        destX = (int)((Width - (sourceWidth * nPercent))/2); 
        break; 
      } 
     } 

     int destWidth = (int)(sourceWidth * nPercent); 
     int destHeight = (int)(sourceHeight * nPercent); 

     Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); 
     bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); 

     Graphics grPhoto = Graphics.FromImage(bmPhoto); 
     grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 

     grPhoto.DrawImage(imgPhoto, 
      new Rectangle(destX, destY, destWidth, destHeight), 
      new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), 
      GraphicsUnit.Pixel); 

     grPhoto.Dispose(); 
     return bmPhoto; 
    } 

} 

salutations.

le format de fichier que je utilise est: JPEG

+0

De quel format de fichier correspondent les images et quel code utilisez-vous? – Blorgbeard

+0

Si l'image d'origine utilise une sorte de schéma de compression (comme c'est souvent le cas avec JPG ou PNG) et que vous n'en avez pas défini pour l'image redimensionnée, c'est très possible. Nous devons voir le code et connaître les types d'images. – Oded

+0

Utilisez 'Image.GetThumbnailImage' et stockez-le dans un format avec la compression activée. –

Répondre

2

Ceci est un effet secondaire normal de rééchantillonnage l'image avec le InterpolationMode fixé à une valeur de haute qualité, comme bicubique. Cela modifie subtilement les valeurs de pixels de presque chaque pixel, d'autant plus que la décompression génère un bruit subtil dans l'image. À peine visible à l'œil nu, tout à fait visible par le filtre de rééchantillonnage. Donner à l'encodeur jpeg un beaucoup plus de temps à compresser l'image. Seul le démarrage avec un format d'image non compressé, comme le format PNG, peut améliorer le résultat.

0

Si vous ne définissez pas explicitement un format d'image, il prendra par défaut le format BMP ou PNG. Si vous spécifiez explicitement ImageFomat.Jpeg, il utilisera quality = 100, au lieu de quality = 90 (le meilleur réglage, sans différences perceptibles). Check out the 28 image resizing pitfalls si vous n'allez use the recommended library for this situation

Le ImageResizer library

Vous avez mentionné une bibliothèque, et oui, elle existe. Le ImageResizer library évite tous les bogues GDI, par défaut à la qualité = 90, et ne fuit pas les poignées de mémoire ou de GDI, même lorsque les images sont corrompues. Il prend également en charge l'autoformation, le recadrage manuel, le redimensionnement, la rotation et de nombreuses autres fonctions. Il est testé dans le temps, testé sur le trafic et testé à l'unité.

Il a une API 1 ligne qui est très simple à utiliser. Go check it out!

Questions connexes