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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
BTosonCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.