影像處理
首頁 基礎 操作 敘述 控制項 函數 程式設計 影像處理 Q & A

向上

影像處理

  • 匯入命名空間
    Imports System.Drawing.Imaging
  • 變換影像物件儲存類型(如 .bmp → .gif)

        Private Sub ImgTypeChange(ByVal origFileName As String)
            '變換影像物件儲存類型(如.bmp → .jpg)
            'refer to: http://aspnet.4guysfromrolla.com/articles/012203-1.aspx
    
            '變更後檔案太小沒變!---NG! NG! NG!
    
            ' 擷取影像檔存入影像物件
            Dim fullSizeImg As System.Drawing.Image
            fullSizeImg = System.Drawing.Image.FromFile(origFileName)
    
            ' 變換影像物件儲存類型
            Dim j As Integer = InStr(origFileName, ".")
            origFileName = Mid(origFileName, 1, j) & "jpg"
            fullSizeImg.Save(origFileName, ImageFormat.Jpeg)
    
        End Sub
    
  • 變更影像大小

        Private Sub ImgFileResize(ByVal origFileName As String, ByVal iWidth As Integer, ByVal newFileName As String)
            '變更影像大小
            'refer to: http://aspnet.4guysfromrolla.com/articles/012203-1.aspx
    
            'Get the image.
            ' 擷取影像檔存入影像物件
            Dim fullSizeImg As System.Drawing.Image
            fullSizeImg = System.Drawing.Image.FromFile(origFileName)
    
            'New image
            ' 宣告新影像物件
            Dim thumbNailImg As System.Drawing.Image
    
            ' 不知所為為何,但這是一定要的!
            Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
            dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbNailCallBack)
    
            Dim imageHeight As Integer = fullSizeImg.Height
            Dim imageWidth As Integer = fullSizeImg.Width
    
            ' 變更大小為 iWidth x iWidth,維持長寬比()
            Dim resizedHeight, resizedWidth As Integer
            If imageHeight > imageWidth Then
                resizedHeight = iWidth
                resizedWidth = iWidth * imageWidth / imageHeight
            Else
                resizedWidth = iWidth
                resizedHeight = iWidth * imageHeight / imageWidth
            End If
            thumbNailImg = fullSizeImg.GetThumbnailImage(resizedWidth, resizedHeight, dummyCallBack, IntPtr.Zero)
    
            ' 依指定類型存檔
            thumbNailImg.Save(newFileName, ImageFormat.Jpeg)
    
        End Sub
        
        Function ThumbNailCallBack() As Boolean
    	' 不知所為為何,但這是一定要的!
    	Return False
        End Function