Thumbnail image resize ASP.net

I have a image Control in asp.net that i want to use to display a thumbnail image.
The image is in a folder location  /images/logo . Format can be Jpeg/Gif/Bitmap. Problem is , it does not work as expected. I see the image resizing to the correct proportion but it does not get displayed...

Private Function GetCompanyLogo(ByVal ImageName As String)
        Dim CompanyLogoURL As String = Server.MapPath("../images/logo/") + ImageName.ToString()
        Try
            If (File.Exists(CompanyLogoURL)) Then
 
                Dim imgHeight As Double = 0.0
                Dim imgWidth As Double = 0.0
                Dim maxWidth As Double = 200.0
                Dim maxHeight As Double = 200.0
 
                Dim currentImage As System.Drawing.Image = System.Drawing.Image.FromFile(CompanyLogoURL)
                imgHeight = currentImage.Height
                imgWidth = currentImage.Width
                If imgWidth > maxWidth Or imgHeight > maxHeight Then
                    Dim deltaWidth As Double = imgWidth - maxWidth
                    Dim deltaHeight As Double = imgHeight - maxHeight
                    Dim scaleFactor As Double
                    If deltaHeight > deltaWidth Then
                        scaleFactor = maxHeight / imgHeight
                    Else
                        scaleFactor = maxWidth / imgWidth
                    End If
                    imgWidth *= scaleFactor
                    imgHeight *= scaleFactor
                End If
 
                CompanyLogo.ImageUrl = CompanyLogoURL
                CompanyLogo.Height = Unit.Pixel(imgHeight)
                CompanyLogo.Width = Unit.Pixel(imgWidth)
 
            Else
                Response.Write(CompanyLogoURL)
            End If
 
 
        Catch e As Exception
            Response.Write(e)
            Response.Write(CompanyLogoURL)
        Finally
 
        End Try
    End Function

Open in new window

byte1Asked:
Who is Participating?
 
BTosonConnect With a Mentor Commented:
Well you could do something like the following:
<img src="?Image=companylogo.jpg" />


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
	If Request("Image") IsNot Nothing Then GetCompanyLogo(Request("Image"))
End Sub

Open in new window

0
 
BTosonCommented:
Hi,

Are you tryiong to actually resize the image?
If so, please see the attached code.
This will dynamically resize the image and output it to the response steam.

Private Sub GetCompanyLogo(ByVal ImageName As String)
Dim CompanyLogoURL As String = Server.MapPath("images\logos\") + ImageName.ToString()
Try
	If (File.Exists(CompanyLogoURL)) Then
		Dim imgHeight As Double = 0.0
		Dim imgWidth As Double = 0.0
		Dim maxWidth As Double = 200.0
		Dim maxHeight As Double = 200.0
		Dim currentImage As System.Drawing.Image = System.Drawing.Image.FromFile(CompanyLogoURL)
		imgHeight = currentImage.Height
		imgWidth = currentImage.Width
		If imgWidth > maxWidth Or imgHeight > maxHeight Then
		Dim deltaWidth As Double = imgWidth - maxWidth
		Dim deltaHeight As Double = imgHeight - maxHeight
		Dim scaleFactor As Double
		If deltaHeight > deltaWidth Then
			scaleFactor = maxHeight / imgHeight
		Else
			scaleFactor = maxWidth / imgWidth
		End If
		imgWidth *= scaleFactor
		imgHeight *= scaleFactor
		End If
 
		Dim newImage = currentImage.GetThumbnailImage(imgWidth, imgHeight, Nothing, IntPtr.Zero)
		Response.AddHeader("Content-type", "image/jpeg")
		newImage.Save(Response.OutputStream, Drawing.Imaging.ImageFormat.Jpeg)
		Response.End()
	Else
		Response.Write(CompanyLogoURL)
	End If
Catch e As Exception
	Response.Write(e)
	Response.Write(CompanyLogoURL)
	Finally
End Try
End Sub

Open in new window

0
 
byte1Author Commented:
The above code would actually resize the image to a smaller proportion. I need to display it in an image control.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.