在使用vb.net保存为blob数据类型之前,我有关于重新调整图像大小的问题。我不知道如何调整图像大小。
这是我在insert中的代码:
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 :(得分: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字符串或字节数组的内容。
答案 1 :(得分: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
随意询问是否有任何混淆。