Dear Sir,
I'm Trying to check the dimensions of an image before resizing it in VB.NET.
I want to check image dimension below 640x640
Private SmallImage As Byte()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Get the original image.
Dim originalImage = PictureBox1.Image
'Create a new, blank image with the same dimensions.
Dim croppedImage As New Bitmap(originalImage.Width, originalImage.Height)
'Prepare to draw on the new image.
Using g = Graphics.FromImage(croppedImage)
Dim path As New GraphicsPath
'Create an ellipse that fills the image in both directions.
path.AddEllipse(0, 0, croppedImage.Width, croppedImage.Height)
Dim reg As New Region(path)
'Draw only within the specified ellipse.
g.Clip = reg
g.DrawImage(originalImage, Point.Empty)
End Using
'Dim signatureFileName = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName)
If String.IsNullOrEmpty(txtFileName.Text) Then
MessageBox.Show("cannot empty")
Return
End If
'Display the new image.
PictureBox2.Image = croppedImage
'Save Small Image
Dim signaturePath As String = Path.Combine(Application.StartupPath, $"{txtFileName.Text}" & "Round.png")
Dim ResizedImage As Image = ResizeImage(PictureBox2.Image, 640, 640)
SmallImage = SaveImage(ResizedImage)
ResizedImage.Save(signaturePath, ImageFormat.Png)
'croppedImage.Save("test.png")
End Sub
Private Function ResizeImage(ByRef InputImage As Image, MaxWidth As Integer, MaxHeight As Integer) As Image
'ReSize the Image if needed to save space in the database
If MaxWidth < InputImage.Width Or MaxHeight < InputImage.Height Then
'ReSize Image
Dim Scale As Double = Math.Min(MaxWidth / InputImage.Width, MaxHeight / InputImage.Height)
Return New Bitmap(InputImage, New Size(Math.Round(InputImage.Width * Scale), Math.Round(InputImage.Height * Scale)))
Else
'Image size was OK
Return InputImage
End If
End Function
Private Function SaveImage(TheImage As Image) As Byte()
'Converts Image to Byte Array
Using mStream As New MemoryStream()
'TheImage.Save(mStream, TheImage.RawFormat)
TheImage.Save(mStream, ImageFormat.Png)
Return mStream.ToArray()
End Using
End Function