?
Solved

Thumbnail image resize ASP.net

Posted on 2008-11-04
5
Medium Priority
?
439 Views
Last Modified: 2010-05-18
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

0
Comment
Question by:byte1
  • 2
3 Comments
 
LVL 4

Expert Comment

by:BToson
ID: 22882007
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
 

Author Comment

by:byte1
ID: 22882021
The above code would actually resize the image to a smaller proportion. I need to display it in an image control.

0
 
LVL 4

Accepted Solution

by:
BToson earned 2000 total points
ID: 22882073
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question