如何在vb.net中调整图像大小? [英] How to resize image in vb.net?

查看:139
本文介绍了如何在vb.net中调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click

    Dim businessId As Integer = 0
    Dim strLogo As String = String.Empty
    Dim FileLogo As String = String.Empty
    Dim userId As Integer = 0
    userId = Convert.ToInt32(Session(Common.SESSION_USERID).ToString())

    If (flLogo.HasFile) Then
        Dim strPath As String = Server.MapPath("Common/WebSiteImages/")
        flLogo.SaveAs(strPath + flLogo.FileName)
        FileLogo = flLogo.FileName
        strLogo = "Common/WebSiteImages/" + flLogo.FileName
        Dim big As String = "~Big"
        big = resizeImageAndSave(strLogo, 450, 420, big)
        Dim thumb As String = "~Thumb"
        thumb = resizeImageAndSave(strLogo, 240, 172, thumb)
    Else
        strLogo = ViewState("Logo")
        If (strLogo Is Nothing) Then
            strLogo = String.Empty
        Else
            strLogo = ViewState("Logo")
        End If
    End If
    Dim dt As New DataTable()
    dt = GetWebsiteProfile(userId)
    If (dt Is Nothing) Then
        businessId = InsertUpdateBusinessSettings(0, userId, txtAddress.Text, txtPhoneNo.Text, editorDesc.Content, strLogo, txtCopyRight.Text,editorServices.Content)
        If businessId > 0 Then
            lblMsg.Text = "inserted successfuly...."
            lblMsg.ForeColor = System.Drawing.Color.Green
        End If
    Else
        Dim businessIdd As Integer = dt.Rows(0)("Id").ToString()
        If dt IsNot Nothing Then
            businessId = InsertUpdateBusinessSettings(businessIdd, userId, txtAddress.Text, txtPhoneNo.Text, editorDesc.Content, strLogo, txtCopyRight.Text,editorServices.Content)
            If businessId > 0 Then
                lblMsg.Text = "updated successfuly...."
                lblMsg.ForeColor = System.Drawing.Color.Green
            End If
        End If
    End If
End Sub













Private Function resizeImageAndSave(ByVal imagePath As String, ByVal width As Integer, ByVal height As Integer, ByVal name As String) As String
        imagePath = "D:\Aman\FindURBiz\Abhi\250814findurbiz\findurbiz\FindurBiz\" & imagePath
        Dim fullSizeImg As System.Drawing.Image = System.Drawing.Image.FromFile(imagePath)
        Dim thumbnailImg = New Bitmap(width, height)
        Dim thumbGraph = Graphics.FromImage(thumbnailImg)
        thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
        thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
        thumbGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
        Dim imageRectangle = New Rectangle(0, 0, width, height)
        thumbGraph.DrawImage(fullSizeImg, imageRectangle)
        Dim withoutExtension As String = imagePath
        Dim strImage As String = String.Empty
        Dim strArr() As String
        strArr = withoutExtension.Split(".")
        strImage = strArr(0)
        imagePath = strImage + ".jpg"

        Dim targetPath As String = imagePath.Replace(Path.GetFileNameWithoutExtension(imagePath), Path.GetFileNameWithoutExtension(imagePath) & name)


        thumbnailImg.Save(targetPath, System.Drawing.Imaging.ImageFormat.Jpeg)

        thumbnailImg.Dispose()
        Dim bigimage As String = String.Empty
        Return targetPath
        ViewState("targetPath") = bigimage

    End Function





我的问题 - :当一个小图像转换成大图像时,这个图像分辨率是不好。



请帮助我非常紧急...........



My Problem-: When a small image to converted a big image then this image resolution is not good.

Please help me its very urgent...........

推荐答案

你的意思是质量不好吗?当试图放大低分辨率图像时,可以预料到这一点。你必须记住像jpg这样的图像已经被压缩并由像素或点组成。当您放大jpg时,您会根据相邻像素的最佳猜测添加像素。



最好的选择是从高rez图像开始。



/ *更新---------------------------- * /



如上所述,增加小图像的分辨率只会达到一定的百分比。之后,您将获得像素化或失真。这只是使用基于像素的图像的本质。



有些图书馆可能有所帮助,但里程会有所不同。



http://stackoverflow.com/questions/249587/high -quality-image-scaling-c-sharp [ ^ ]
Do you mean that quality is not good? That is be be expected when trying to enlarge a low resolution image. You have to remember that an image such as a jpg is already compressed and made up of pixels or dots. When you enlarge an jpg you are adding pixels based on a best guess of neighboring pixels.

Your best bet is to start with high rez images.

/*Update----------------------------*/

As I mentioned above increasing the resolutions of a small image will only work up to a certain percentage. After that then you will get pixelation or distortion. This is just the nature of working with pixel based images.

There are some libraries that might help, but mileage will vary.

http://stackoverflow.com/questions/249587/high-quality-image-scaling-c-sharp[^]


这篇关于如何在vb.net中调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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