2010-01-27 20 views
10

Chciałbym zrobić proste narzędzie VB do zmiany rozmiaru obrazów za pomocą vb.net. Mam problem z określeniem, jakiej klasy vb użyć, aby faktycznie manipulować obrazami. Klasa Obraz i klasa Bitmap nie działają.Zmiana rozmiaru obrazów w VB.NET

Wszelkie pomysły, wskazówki, porady, linki do tutoriali są bardzo doceniane.

Dzięki.

Odpowiedz

13

Here is an article z pełnymi informacjami, jak to zrobić.

Private Sub btnScale_Click(ByVal sender As System.Object, _ 
    ByVal e As System.EventArgs) Handles btnScale.Click 
    ' Get the scale factor. 
    Dim scale_factor As Single = Single.Parse(txtScale.Text) 

    ' Get the source bitmap. 
    Dim bm_source As New Bitmap(picSource.Image) 

    ' Make a bitmap for the result. 
    Dim bm_dest As New Bitmap(_ 
     CInt(bm_source.Width * scale_factor), _ 
     CInt(bm_source.Height * scale_factor)) 

    ' Make a Graphics object for the result Bitmap. 
    Dim gr_dest As Graphics = Graphics.FromImage(bm_dest) 

    ' Copy the source image into the destination bitmap. 
    gr_dest.DrawImage(bm_source, 0, 0, _ 
     bm_dest.Width + 1, _ 
     bm_dest.Height + 1) 

    ' Display the result. 
    picDest.Image = bm_dest 
End Sub 

[Edycja]
One more w podobny sposób.

2

nie wiem zbyt wiele składnię VB.NET ale tu i pomysł

Dim source As New Bitmap("C:\image.png") 
Dim target As New Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb) 

Using graphics As Graphics = Graphics.FromImage(target) 
    graphics.DrawImage(source, new Size(48, 48)) 
End Using 
4

Spowoduje to ponowne rozmiar dowolny obraz przy użyciu najlepszej jakości ze wsparciem dla 32 bity na piksel z alfa. Nowy obraz będzie miał oryginalny obraz wyśrodkowany wewnątrz nowego w oryginalnym formacie obrazu.

#Region " ResizeImage " 
    Public Overloads Shared Function ResizeImage(SourceImage As Drawing.Image, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap 
     Dim bmSource = New Drawing.Bitmap(SourceImage) 

     Return ResizeImage(bmSource, TargetWidth, TargetHeight) 
    End Function 

    Public Overloads Shared Function ResizeImage(bmSource As Drawing.Bitmap, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap 
     Dim bmDest As New Drawing.Bitmap(TargetWidth, TargetHeight, Drawing.Imaging.PixelFormat.Format32bppArgb) 

     Dim nSourceAspectRatio = bmSource.Width/bmSource.Height 
     Dim nDestAspectRatio = bmDest.Width/bmDest.Height 

     Dim NewX = 0 
     Dim NewY = 0 
     Dim NewWidth = bmDest.Width 
     Dim NewHeight = bmDest.Height 

     If nDestAspectRatio = nSourceAspectRatio Then 
      'same ratio 
     ElseIf nDestAspectRatio > nSourceAspectRatio Then 
      'Source is taller 
      NewWidth = Convert.ToInt32(Math.Floor(nSourceAspectRatio * NewHeight)) 
      NewX = Convert.ToInt32(Math.Floor((bmDest.Width - NewWidth)/2)) 
     Else 
      'Source is wider 
      NewHeight = Convert.ToInt32(Math.Floor((1/nSourceAspectRatio) * NewWidth)) 
      NewY = Convert.ToInt32(Math.Floor((bmDest.Height - NewHeight)/2)) 
     End If 

     Using grDest = Drawing.Graphics.FromImage(bmDest) 
      With grDest 
       .CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality 
       .InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic 
       .PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality 
       .SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias 
       .CompositingMode = Drawing.Drawing2D.CompositingMode.SourceOver 

       .DrawImage(bmSource, NewX, NewY, NewWidth, NewHeight) 
      End With 
     End Using 

     Return bmDest 
    End Function 
#End Region 
+0

'Drawing2D.SmoothingMode' nie ma tu zastosowania, to jest istotne tylko dla 2D wektorowej metody takie jak 'Graphics.DrawLine' – alldayremix

+0

Ostrożnie z tym. Sekcja 'With grDest' wydawała się tak nieznacznie zwiększać wartości alfa, zauważalne tylko w iteracyjnym przetwarzaniu tego samego obrazu z elementem półprzezroczystym do obrazu. Z biegiem czasu stało się to coraz mniej nieprzejrzyste. Skomentowałem część .SmoothingMode i Changed CompositingMode do SourceCopy. Wciąż testowałem, ale zdaje się, że zrobił to jeden z nich. Obawiam się, że nie mogę podać dokładnej odpowiedzi, ponieważ nie rozumiem tak dobrze GDI. Może @Carter może być w stanie pomóc dalej, biorąc pod uwagę jego wiedzę na temat GDI. – stigzler

12

można po prostu użyć tego kodu w jednej linijce, aby zmienić rozmiar obrazu w Visual Basic .NET

Public Shared Function ResizeImage(ByVal InputImage As Image) As Image 
     Return New Bitmap(InputImage, New Size(64, 64)) 
End Function 

Gdzie;

  1. "InputImage" to obraz, który chcesz zmienić.
  2. „64 x 64” jest wymagany rozmiar można zmienić go w miarę potrzeb, tj 32x32 itp
0
Dim x As Integer = 0 
    Dim y As Integer = 0 
    Dim k = 0 
    Dim l = 0 
    Dim bm As New Bitmap(p1.Image) 
    Dim om As New Bitmap(p1.Image.Width, p1.Image.Height) 
    Dim r, g, b As Byte 
    Do While x < bm.Width - 1 
     y = 0 
     l = 0 
     Do While y < bm.Height - 1 
      r = 255 - bm.GetPixel(x, y).R 
      g = 255 - bm.GetPixel(x, y).G 
      b = 255 - bm.GetPixel(x, y).B 
      om.SetPixel(k, l, Color.FromArgb(r, g, b)) 
      y += 3 
      l += 1 
     Loop 
     x += 3 
     k += 1 
    Loop 
    p2.Image = om 
Powiązane problemy