[WinForms]调整图像宽度/高度,保持纵横比并替换原始图像 [英] [WinForms] Resize image width/height, keep aspect ratio and replace original image

查看:72
本文介绍了[WinForms]调整图像宽度/高度,保持纵横比并替换原始图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我已将从数据库收到的字节转换为图像文件。在那之后,我将获得图像尺寸以检查宽度或高度是否超过最大宽度/高度。



如果宽度是最大1000px并且保存的图像是1200px ,我需要调整宽度,使其达不到最大尺寸。如何调整宽度或高度,另一个是自动调整宽高比?例。宽度为1200px,高度为500px。我只需要将宽度调整为1000px,高度将自动调整,就像第三方照片编辑器调整大小一样。



目前我正在使用Width和高度因此会延长使其变长或变胖。





问题1:调整宽度或高度并自动调整相反长度

问题2:直接调整大小并替换原始文件而不转到另一个文件夹




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


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

推荐答案

已找到解决方案。

Solutions have been found.
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


这篇关于[WinForms]调整图像宽度/高度,保持纵横比并替换原始图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆