Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image re-size and display 1

Status
Not open for further replies.

tcstom

Programmer
Aug 22, 2003
235
GB
Hi,

I'm new to the System.Drawing namespace but have a need to re-size images according to their width and height. Basically the image may be too big for the space so based on a max-width I want to re-size an already uploaded image and display it on the page. I've found a lot of examples for programmatically re-sizing images within code but nothing for how to actually then write the HTML onto the page e.g. using an Image web control. The re-sized image would only need to be temporary, and preferably any examples in C# please.

Thanks.

Tom
emo_big_smile.gif
 
Although this is in VB.NET, you should be able to follow the basics (this is taken straight from one of my projects hence the VB.NET language). First I create a page named thumbnail.aspx:
Code:
Public Class Thumbnail
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Initialize objects
        Dim objImage, objThumbnail As System.Drawing.Image
        Dim strServerPath, strFilename As String
        Dim shtWidth, shtHeight As Short
        ' Get image folder path on server - use "\" string if root
        strServerPath = Server.MapPath("Images\")
        ' Retrieve name of file to resize from query string
        strFilename = strServerPath & Request.QueryString("filename")
        ' Retrieve file, or error.gif if not available
        Try
            objImage = objImage.FromFile(strFilename)
        Catch
            objImage = objImage.FromFile(strServerPath & "error.gif")
        End Try
        ' Retrieve width from query string
        If Request.QueryString("width") = Nothing Then
            shtWidth = objImage.Width
        ElseIf Request.QueryString("width") < 1 Then
            shtWidth = 100
        Else
            shtWidth = Request.QueryString("width")
        End If
        ' Work out a proportionate height from width
        shtHeight = objImage.Height / (objImage.Width / shtWidth)
        ' Create thumbnail
        objThumbnail = objImage.GetThumbnailImage(shtWidth, _
          shtHeight, Nothing, System.IntPtr.Zero)
        ' Send down to client
        Response.ContentType = "image/jpeg"
        objThumbnail.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
        ' Tidy up
        objImage.Dispose()
        objThumbnail.Dispose()

    End Sub

End Class
Then, whenever I need to display the smaller image I just call that page with the path of the original file (in this example the filename comes from a data source as it is part of a repeater control):
Code:
<img src='thumbnail.aspx?filename=<%# DataBinder.Eval(Container.DataItem, "FILENAME") %>
I'm pretty sure there isn't anything in there that you would have trouble converting to C#...



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Just to add my 1c...

The generatethumbnailimage thingie seems to produce really bad resolution images..

I tend to just use the image and graphics objects to make custom (high res) images.. As you have full controll as to how you save them, compression etc.. the size is often much smaller than the thumbnail image also...

Code for scaling....
(then all you need to do is save the resulting image to an output stream....)
Code:
Public Class ImageUtils

    Function ScaleImage(ByVal imgFull As Image, ByVal intX As Integer, ByVal intY As Integer) As Image

        Dim dblScaleFactor As Double
        Dim dblScaleX, dblScaleY As Double

        dblScaleX = intX / imgFull.Width
        dblScaleY = intY / imgFull.Height

        If dblScaleX > dblScaleY Then
            dblScaleFactor = dblScaleX
        Else
            dblScaleFactor = dblScaleY
        End If

        ' Get the scale factor.
        Dim scale_factor As Single = CType(dblScaleFactor, Single)

        ' Make a bitmap for the result.
        Dim imgBMP_dest As New Bitmap(CInt(imgFull.Width * scale_factor), CInt(imgFull.Height * scale_factor))

        ' Make a Graphics object for the result Bitmap.
        Dim imgOutput As Graphics = Graphics.FromImage(imgBMP_dest)

        ' Copy the source image into the destination bitmap.
        imgOutput.DrawImage(imgFull, 0, 0, imgBMP_dest.Width + 1, imgBMP_dest.Height + 1)

        imgOutput.Dispose()
        imgOutput = Nothing
        ' Display the result.
        Return imgBMP_dest

    End Function
End Class
 
I can't say that I've noticed it but that may be beacuse the images that I have to produce don't have to be of a particularly good quality anyway so I maybe just haven't noticed.

It's good to see another solution though, thanks.

I'll have to look into it but I'm sure there is another way that has been introduced in version 2.0 of the framework as well.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top