2014-02-10 102 views
0

我在使用vb.net保存爲blob數據類型之前在重新調整圖像大小方面存在問題。我不知道如何調整圖像大小。如何在保存到數據庫之前重新調整圖像大小?

這裏是我的代碼中插入:

 Dim FileSize As UInt32 

    Dim mstream As New System.IO.MemoryStream() 
    pic_box_save.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg) 
    Dim arrImage() As Byte = mstream.GetBuffer() 
    FileSize = mstream.Length 
    mstream.Close() 
    MsgBox(FileSize) 


    Try 
     sql = "INSERT INTO clientreports(report_id, img) VALUES(@image_id, @image_data)" 
     sql_command = New MySqlCommand(SQL, sql_connection) 
     sql_command.Parameters.AddWithValue("@image_id", Nothing) 
     sql_command.Parameters.AddWithValue("@image_data", arrImage) 


     sql_command.ExecuteNonQuery() 
    Catch ex As Exception 
     MsgBox(ex.Message) 
     Exit Sub 
    End Try 

    MsgBox("Image has been saved.") 

請幫我傢伙如果有人知道如何解決這個問題。謝謝!

回答

0

要創建縮略圖/調整圖像:

Friend Function GetImageThumb(ByVal orgBmp As BitMap, 
        ByVal w as Int32, h as Int32) As Bitmap 

    Dim thumb As New Bitmap(w, h) 

    Using g As Graphics = Graphics.FromImage(thumb) 
     g.DrawImage(orgBmp , 0, 0, w + 1, h + 1) 
    End Using 
    Return thumb 
End Function 

對於數據存儲,你可能需要將圖像轉換到別的東西像一個Base64字符串或字節數組。

0

您可以使用以下代碼來調整具有特定要求的圖像大小。如果有必要,即使您可以保持寬高比。

Public Shared Function ResizeImage(ByVal pImage As Drawing.Image, ByVal pWidth As Integer, ByVal pHeight As Integer, Optional ByVal pPreserveAspectRatio As Boolean = True, Optional ByVal pIsImageDisposeRequired As Boolean = True) As Drawing.Image 
       Dim iNewWidth As Integer 
       Dim iNewHeight As Integer 
       Dim oNewImage As System.Drawing.Image 
       Dim originalWidth As Integer 
       Dim originalHeight As Integer 
       Dim oPercentWidth As Single 
       Dim oPercentHeight As Single 
       Dim oPercent As Single 

       Try 
        If pPreserveAspectRatio Then 
         originalWidth = pImage.Width 
         originalHeight = pImage.Height 
         oPercentWidth = CSng(pWidth)/CSng(originalWidth) 
         oPercentHeight = CSng(pHeight)/CSng(originalHeight) 
         oPercent = If(oPercentHeight < oPercentWidth, oPercentHeight, oPercentWidth) 
         iNewWidth = CInt(originalWidth * oPercent) 
         iNewHeight = CInt(originalHeight * oPercent) 
        Else 
         iNewWidth = pWidth 
         iNewHeight = pHeight 
        End If 

        oNewImage = New System.Drawing.Bitmap(iNewWidth, iNewHeight) 
        Using oGraphicsHandle As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(oNewImage) 
         oGraphicsHandle.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic 
         oGraphicsHandle.DrawImage(pImage, 0, 0, iNewWidth, iNewHeight) 
        End Using 

        If pIsImageDisposeRequired Then 
         pImage.Dispose() 
        End If 

        Return oNewImage 
       Catch ex As Exception 
        eCP.Excpetion.Untility.CommonUtils.WriteExceptionToFile(ex.Message, eCP.Excpetion.Untility.CommonUtils.LogFileTypes.Host) 
        Return pImage 
       End Try 
      End Function 

隨意詢問是否有任何混淆。

相關問題