Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1551 | Anzahl registrierter User: 1841 | Besucher online: 61
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Resize image to fit in picturebox


Autor: pproost
Sprache: VB.NET
Bewertung:
5.44 (2 votes)
Anzahl der Aufrufe: 25314
  
Kick it on dotnet-kicks.de  

Beschreibung:

Automatically resize an image so it shows ok in the picturebox on your form. It doesn't change the actual image

Abgelegt unter: Image, Resize, Picturebox.



Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Public Sub AutosizeImage(ByVal ImagePath As String, ByVal picBox As PictureBox, Optional ByVal pSizeMode As PictureBoxSizeMode = PictureBoxSizeMode.CenterImage)
        Try
            picBox.Image = Nothing
            picBox.SizeMode = pSizeMode
            If System.IO.File.Exists(ImagePath) Then
                Dim imgOrg As Bitmap
                Dim imgShow As Bitmap
                Dim g As Graphics
                Dim divideBy, divideByH, divideByW As Double
                imgOrg = DirectCast(Bitmap.FromFile(ImagePath), Bitmap)

                divideByW = imgOrg.Width / picBox.Width
                divideByH = imgOrg.Height / picBox.Height
                If divideByW > 1 Or divideByH > 1 Then
                    If divideByW > divideByH Then
                        divideBy = divideByW
                    Else
                        divideBy = divideByH
                    End If

                    imgShow = New Bitmap(CInt(CDbl(imgOrg.Width) / divideBy), CInt(CDbl(imgOrg.Height) / divideBy))
                    imgShow.SetResolution(imgOrg.HorizontalResolution, imgOrg.VerticalResolution)
                    g = Graphics.FromImage(imgShow)
                    g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                    g.DrawImage(imgOrg, New Rectangle(0, 0, CInt(CDbl(imgOrg.Width) / divideBy), CInt(CDbl(imgOrg.Height) / divideBy)), 0, 0, imgOrg.Width, imgOrg.Height, GraphicsUnit.Pixel)
                    g.Dispose()
                Else
                    imgShow = New Bitmap(imgOrg.Width, imgOrg.Height)
                    imgShow.SetResolution(imgOrg.HorizontalResolution, imgOrg.VerticalResolution)
                    g = Graphics.FromImage(imgShow)
                    g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                    g.DrawImage(imgOrg, New Rectangle(0, 0, imgOrg.Width, imgOrg.Height), 0, 0, imgOrg.Width, imgOrg.Height, GraphicsUnit.Pixel)
                    g.Dispose()
                End If
                imgOrg.Dispose()

                picBox.Image = imgShow
            Else
                picBox.Image = Nothing
            End If


        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

herbivore schrieb am:  08.12.2006 22:40:37

Mir scheint das Snippet wegen PictureBox.SizeMode (PictureBoxSizeMode.StretchImage bzw. Zoom) überflüssig.
MaF schrieb am:  06.09.2008 11:22:04

Durchaus nicht (überflüssig)!
Ich hatte das Problem, dass die Anzeige vieler, großer Bilder (5 MPixel und mehr) den Arbeitsspeicher sprengte.


Diese Snippets könnten für Sie interessant sein:
[VB.NET] Invert Colors from Image
[C#] Bild beim Zeichnen invertieren
[C#] Square Thumbnail generieren
[ASP.net] Datei nach Bildupload prüfen
[C#] Change PixelFormat
[VB.NET] Durschnittsfarbe eines Bildes ermitteln
[C#] Image in IPictureDisp umwandeln
[C#] Bild drehen (nach Winkelangabe)
[C#] Image zu Base64 konvertieren und zurück
[C#] Image in string wandeln und zurück
[C#] Bildgröße im Verhältnis verändern (Interpoliert)
[C#] Bild von einem Control oder Panel
[VB.NET] Bildausschnitt auslesen
[VB.NET] Bildgröße im Verhältnis verändern (Interpoliert)
[VB.NET] Bildgröße prozentual verändern (Interpoliert)
[VB.NET] Bildgröße in beiden Dimensionen verändern (Interpoliert)
[C#] base64 codierten String in eine Bitmap umwandeln
[C#] Bitmap in base64 codierten String wandeln
[C#] Image aus URL laden
[C#] Bilder verkleinern, beschneiden und beides gleichzeitig
[VB.NET] Bytearray to Image / Image to Bytearray
[C#] Bild splitten
[C#] Text2Bitmap (String in Bild umwandeln)
[C#] Hintergrundfarbe Transparenter Bilder (z.B. PNG) ändern
[C#] Größe eines Bildes mit mit gleichem Seitenverhältniss ändern
[C#] WPF: Image aus dem Web in Image-Element anzeigen
[C#] Bitmap schneller in Graustufen wandeln
[C#] ISO-Abbild von einer CD/DVD erstellen
[VB.NET] Let your image glow - Bild glühen lassen
[C#] old Windows Forms Image to new WPF Image
[C#] SystemIconsImageListWrapper
[C#] Form als Tray Icon ablegen (Notifyicon)
[VB.NET] Control Resize wie in der IDE auch zur Laufzeit - Extended
[VB.NET] Resize,Crop und beides gleichzeitig von Images
[C#] Kleine Array-Erweiterung
[C#] Bild anzeigen - Datei schließen
[VB.NET] Bild via Drag and Drop von PictureBox zu PictureBox
[VB.NET] Webcam in PictureBox anzeigen

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.