You're here: Snippet Directory » Microsoft .NET (152)
Language:

Bild resizen, neue Bildgröße ermitteln

Language: Deutsch
Programming Language: C#
Published by: Thomas
Last Update: 6/11/2006
Views: 1409

License: BSD License

Description

Wenn man z.B. Bilder in einem Verzeichnis auf dem Server darstellen will, empfiehlt es sich eine Voransicht, sogenannte Thumbnails, zu generieren. Nun kann man das entweder tun, indem man die Thumbnails bereits beim Upload erstellt, oder man erstellt diese dynamisch zur Laufzeit.

Gegeben ist also eine maximale Breite und Höhe der Thumbnails, ausgehend von diesen Daten und der Originalgröße des Bildes muss nun die Thumbnailgröße berechnet werden.

In C# kann man das z.B. wie folgt gestalten.

Code

1 protected void Button1_Click(object sender, EventArgs e) 2 { 3 4 int width = 90; 5 int height = 90; 6 7 float originalWidth = float.Parse(TextBox1.Text); 8 float originalHeight = float.Parse(TextBox2.Text); 9 10 int newWidth = 0, newHeight = 0; 11 12 if (originalWidth > width || originalHeight > height) 13 { 14 15 // calculate the new thumb size 16 float factorX = (float)(originalWidth / width); 17 float factorY = (float)(originalHeight / height); 18 19 Label2.Text = factorX.ToString(); 20 Label3.Text = factorY.ToString(); 21 22 if (factorX > factorY) 23 { 24 newWidth = width; 25 newHeight = (int)(originalHeight / factorX); 26 } 27 else if (factorX < factorY) 28 { 29 newWidth = (int)(originalWidth / factorY); 30 newHeight = height; 31 } 32 else 33 { 34 newWidth = (int)(originalWidth / factorY); 35 newHeight = (int)(originalHeight / factorX); 36 } 37 38 } 39 else 40 { 41 newHeight = (int)originalHeight; 42 newWidth = (int)originalWidth; 43 } 44 45 Label1.Text = newWidth.ToString() + " x " + newHeight.ToString(); 46 47 }

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS