2010-01-27 67 views
10

我想制作一个简单的VB实用程序来使用vb.net调整图像大小。我无法弄清楚用什么样的vb类来实际操作图像。 Image类和Bitmap类不起作用。在VB.NET中调整图像大小

任何想法,提示,技巧,教程链接非常感谢。

谢谢。

回答

13

Here is an article有关如何操作的完整详细信息。

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 

[编辑]
One more上类似的路线。

2

不知道多少VB.NET的语法,但这里的想法和

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

这将重新使用尺寸与阿尔法32bpp的支持,最优质的任何图像。新图像将以原始宽高比将新图像置于新图像的中央。

#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'在这里不适用,它仅适用于2D矢量绘图相关方法,如'Graphics.DrawLine' – alldayremix 2014-10-01 15:59:37

+0

请小心。 'With grDest'部分似乎总是略微增加alpha值,只有在对图像使用半透明元素迭代处理相同图像时才会显着。随着时间的推移,这变得越来越不透明。我注释掉了.SmoothingMode部分并将合成模式更改为SourceCopy。仍然在测试,但其中一个似乎已经完成了。害怕我不能给出一个准确的答案,因为不了解GDI那么好。也许@Carter或许可以帮助他进一步了解GDI。 – stigzler 2016-09-25 17:07:23

12

您可以简单地使用这一行代码,以调整在Visual Basic .NET图像

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

在哪里;

  1. “InputImage”是要调整大小的图像。
  2. “64×64”是必要的大小可能会改变它作为您的需求,即32X32等
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