2009-10-01 13 views

Répondre

1

Le lien suivant peut vous aider :)

snippets.dzone.com/posts/show/4336

Mise à jour: Le code dans le lien ci-dessus redimensionne physiquement et enregistre l'image si votre intention est de redimensionner le fichier d'image afin d'économiser de l'espace , sinon, comme mentionné, vous pouvez simplement définir manuellement la hauteur/largeur sur le contrôle de l'image.

0

Si vous chargez une image, vous pouvez simplement définir les propriétés Largeur/Hauteur.

par exemple.

Bitmap bmp = new Bitmap(@"c:\myfile.bmp"); 
bmp.Width = (int)(bmp.Width/2); 
bmp.Height = (int)(bmp.Height/2); 
0

le charger dans gdi + (système.drawing) et mettre à l'échelle?

-2

de ma galerie de code personnel. Un peu vieux quand même.

protected Image FixedSize(Image imgPhoto, int Width, int Height) 
{ 
    int width = imgPhoto.Width; 
    int height = imgPhoto.Height; 
    int x = 0; 
    int y = 0; 
    int num5 = 0; 
    int num6 = 0; 
    float num7 = 0f; 
    float num8 = 0f; 
    float num9 = 0f; 
    num8 = ((float) Width)/((float) width); 
    num9 = ((float) Height)/((float) height); 
    if (num9 < num8) 
    { 
     num7 = num9; 
     num5 = Convert.ToInt16((float) ((Width - (width * num7))/2f)); 
    } 
    else 
    { 
     num7 = num8; 
     num6 = Convert.ToInt16((float) ((Height - (height * num7))/2f)); 
    } 
    int num10 = (int) (width * num7); 
    int num11 = (int) (height * num7); 
    Bitmap image = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); 
    image.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); 
    Graphics graphics = Graphics.FromImage(image); 
    graphics.Clear(ColorTranslator.FromHtml("#ffffff")); 
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    graphics.DrawImage(imgPhoto, new Rectangle(num5, num6, num10, num11), new Rectangle(x, y, width, height), GraphicsUnit.Pixel); 
    graphics.Dispose(); 
    return image; 
} 
+1

-1: num1, num2, num3, ... num11. Aucune infraction, mais le choix des noms de variables se lit comme le code est délibérément obfusqué. – Juliet

+0

Ceci est un code WTF sérieux: noms de variables, Convert.ToInt16, ColorTranslators.FromHtml. Je m'abstiendrai de voter parce que vous avez au moins le droit InterpolationMode. – MusiGenesis

+0

Kidooosss ... ce code provient de .NET Reflector. J'ai l'idée de copier-coller. L'intention est de montrer seulement la logique. évaluation ... votre choix. :-) – NinethSense

0

En additiona à ce que d'autres ont dit, pour des images plus petites que 300x300, Image.GetThumbnailImage est très pratique:

static void Main(string[] args) 
{ 
    Bitmap b = new Bitmap(@"C:\Documents and Settings\juliet\My Documents\My Pictures\julietawesome.bmp"); 
    Image image = b.GetThumbnailImage(100, 100, null, IntPtr.Zero); 
    image.Save(@"C:\Documents and Settings\juliet\My Documents\My Pictures\thumbnail.bmp", ImageFormat.Bmp); 
} 
0

Puisque personne d'autre a dit ceci: essayez WPF.

0
Public Function ScaleByPercent(ByVal imgPhoto As Image, ByVal Percent As Integer) As Image 

     Dim nPercent As Single = (CType(Percent, Single)/100) 
     Dim sourceWidth As Integer = imgPhoto.Width 
     Dim sourceHeight As Integer = imgPhoto.Height 
     Dim sourceX As Integer = 0 
     Dim sourceY As Integer = 0 

     Dim destX As Integer = 0 
     Dim destY As Integer = 0 
     Dim destWidth As Integer = CType((sourceWidth * nPercent), Integer) 
     Dim destHeight As Integer = CType((sourceHeight * nPercent), Integer) 

     Dim bmPhoto As New Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb) 
     bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution) 

     Dim grPhoto As Graphics = 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 

    End Function 
+0

Sachez que l'algorithme de redimensionnement GDI produira des résultats médiocres lors de modifications majeures de la taille de l'image. J'ai écrit un code similaire dans le passé, puis je suis passé à une bibliothèque tierce pour obtenir un algorithme de redimensionnement de meilleure qualité. – ScottS

Questions connexes