Click here to Skip to main content
16,017,881 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have convert a bytes received from database to image file. After that, I will get the image dimensions to check either width or height is exceed the maximum width/height.

If the width is max 1000px and saved image is 1200px, I need to resize the width so it not reach the max dimension. How to resize just Width or Height and the other one is automatically adjust aspect ratio? Example. Width is 1200px, Height is 500px. I just need to resize width to 1000px and the height will auto adjust just like a third party photo editor resize.

Currently I'm using a fix code for Width and Height so it will stretch over making it long or fat.


Question 1: Resize Width or Height and auto adjust opposite length
Question 2: Direct resize and replace the original file without go to another folder


VB.NET
Private Sub ResizeThis(ByVal imgpath As String, ByVal newpath As String)
    Try
        Dim bmp As New Bitmap(imgpath)
        Dim intWidth As Integer = bmp.Width : Dim intHeight As Integer = bmp.Height
        Dim newWidth, newHeight As Integer
        If intWidth > 1000 And intHeight > 200 Then
            newWidth = 1000 : newHeight = 200
        ElseIf intWidth > 1000 Then
            newWidth = 1000 : newHeight = intHeight
        ElseIf intHeight > 200 Then
            newWidth = intWidth : newHeight = 200
        End If
        Using OriginalImage = Image.FromFile(imgpath)
            Using ResizedImage As New Bitmap(OriginalImage, newWidth, newHeight)
                ResizedImage.Save(newpath, Drawing.Imaging.ImageFormat.Png)
            End Using
        End Using
    Catch ex As Exception
        str_errmsg = "Oops! Something is problem with resize logo dimension."
        MessageBox.Show(str_errmsg & vbCrLf & "Err: " & ex.Message)
    End Try
End Sub
Posted
Updated 21-Jan-16 16:34pm
v2
Comments
Ralf Meier 22-Jan-16 3:52am    
I'm not shure if I understood right your issue ...

to question 1 :
- your method works (basicly) as you want ?
- but you want to make the resizing variable : if your source-image has for example 1000 x 500 px and you order to resize it to 1000 x 1000 px - what should happen ? Or should the given parameter corrected basing to the aspect-ratio of the source-image ?

to question 2:
- you want to save the modified image in the place with the name of the source-image ? If Yes - why don't you overwrite it (the source) or 1st delete it and then save it with the new dimensions ?
Luiey Ichigo 22-Jan-16 10:10am    
from your question for my question:-
Q1: The image save to mysql blob is having multi dimension especially logo since logo size is various. So, I want to resize it to new width only and the height will auto adjust to aspect ratio. Because if I fix the width and height to the image component in my program, it will look stretch/fat look.

Q2: Yes, you have give me a hint. Delete old image and save with new. Yes. I will try that
Luiey Ichigo 22-Jan-16 10:17am    
This is what I do and it add perfectly:


Private Sub ResizeThis(ByVal imgpath As String, ByVal newpath As String)
Try
Dim fs As New FileStream(imgpath, FileMode.Open)
Dim original As Image = Image.FromStream(fs)
fs.Close()
Dim intWidth As Integer = original.Width : Dim intHeight As Integer = original.Height
Dim newWidth, newHeight As Integer
If intWidth > 1000 Then
newWidth = 1000
newHeight = 1000 * (intHeight / intWidth)
File.Delete(imgpath)
Using ResizedImage As New Bitmap(original, newWidth, newHeight)
ResizedImage.Save(imgpath, Drawing.Imaging.ImageFormat.Png)
End Using
End If
Catch ex As Exception
str_errmsg = "Oops! Something is problem with resize logo dimension."
MessageBox.Show(str_errmsg & vbCrLf & "Err: " & ex.Message)
End Try
End Sub
Ralf Meier 24-Jan-16 12:23pm    
@Luiey:
If you reply to someones Comment or Answer please use the Reply-Button which corresponds to the item ... in this case the Answerer automaticly get as Message/Notification from the Forum-Software.

Back to your Question :
If I see right you changed the code for working with the Aspect-Ratio (it is the quotient of Width and Height). I think, your question now is answered - isn't it ?
Luiey Ichigo 25-Jan-16 8:04am    
@Ralf, sorry for that. Not very familiar with forum things much. Thanks for the advise

Yes it is now been answered and work perfectly but a bit differ. I'll put in solutions. If you have another approach, you may add it too for other references.

Thanks

1 solution

Solutions have been found.
VB
Private Sub ExtractImages()
    Dim listpath As String = "D:\image folder\image-list\"
    'First line was retrieve blob from mysql database
    Dim mybytearray As Byte() = arr_imagelist.Item(0).arr_image
    Dim name As String = arr_imagelist.Item(0).str_name.ToString.ToLower
    'This line is new name and replacing unwanted character for filename purposes
    Dim newname As String = System.Text.RegularExpressions.Regex.Replace(name.Trim, " ", "_", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    Dim oFileStream As System.IO.FileStream
    oFileStream = New System.IO.FileStream(listpath & newname & ".png", System.IO.FileMode.Create)
    oFileStream.Write(mybytearray, 0, mybytearray.Length)
    oFileStream.Close()
    'File is send for resize with desired width
    ResizeThis(listpath & name & ".png", 2000, Drawing.Imaging.ImageFormat.Png)
End Sub

Private Sub ResizeThis(ByVal imgpath As String, ByVal maxwidth As Integer, ByVal format As Drawing.Imaging.ImageFormat)
    'This line is use to stream the file to memory in order to overwrite original image. Otherwise, delete file first was another option.
    Dim fs As New FileStream(imgpath, FileMode.Open)
    Dim original As Image = Image.FromStream(fs)
    fs.Close()
    Dim intWidth As Integer = original.Width : Dim intHeight As Integer = original.Height
    Dim newWidth, newHeight As Integer
    If intWidth > maxwidth Then
        newWidth = maxwidth
        newHeight = maxwidth * (intHeight / intWidth)
        File.Delete(imgpath)
        Using ResizedImage As New Bitmap(original, newWidth, newHeight)
            ResizedImage.Save(imgpath, format)
        End Using
    End If
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900