2010-01-27 43 views
10

Tôi muốn tạo một tiện ích VB đơn giản để thay đổi kích thước hình ảnh bằng vb.net. Tôi đang gặp khó khăn trong việc tìm ra loại vb để sử dụng để thực sự thao tác các hình ảnh. Lớp Image và lớp Bitmap không hoạt động.Thay đổi kích thước hình ảnh trong VB.NET

Bất kỳ ý tưởng, gợi ý, mẹo, liên kết hướng dẫn nào được đánh giá cao.

Cảm ơn.

Trả lời

13

Here is an article với đầy đủ chi tiết về cách thực hiện việc này.

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 

[Chỉnh sửa]
One more trên dòng tương tự.

2

Không biết cú pháp VB.NET nhiều nhưng đây là ý tưởng và

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

Điều này sẽ thay đổi kích thước bất kỳ hình ảnh bằng cách sử dụng chất lượng tốt nhất với sự hỗ trợ cho 32bpp với alpha. Hình ảnh mới sẽ có hình ảnh gốc được căn giữa bên trong hình ảnh mới với tỷ lệ khung hình gốc.

#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' không áp dụng ở đây, nó chỉ thích hợp cho 2D vẽ vector các phương thức như 'Graphics.DrawLine' – alldayremix

+0

Hãy cẩn thận với điều này. Phần 'With grDest' xuất hiện để tăng giá trị alpha từng chút một, chỉ đáng chú ý khi xử lý lặp lại cùng một hình ảnh với phần tử bán mờ cho hình ảnh. Theo thời gian, điều này trở nên ít mờ nhạt hơn. Tôi đã nhận xét phần .SmoothingMode và Thay đổi CompositingMode thành SourceCopy. Vẫn đang thử nghiệm nhưng một trong hai dường như đã thực hiện nó. Sợ tôi không thể đưa ra câu trả lời chính xác vì không hiểu rõ GDI. Có thể @Carter có thể giúp tiếp tục đưa ra kiến ​​thức về GDI của anh ấy. – stigzler

12

Bạn chỉ có thể sử dụng mã một dòng này để thay đổi kích thước hình ảnh của bạn trong .net visual basic

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

ở đâu;

  1. "InputImage" là hình ảnh bạn muốn thay đổi kích thước.
  2. "64 X 64" là kích thước cần thiết bạn có thể thay đổi nó như nhu cầu của bạn tức là 32x32, vv
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 
Các vấn đề liên quan