Dynamically displaying pictures on web applications by using VS 2005 Syste.Drawing method

Last post 04-28-2007 2:37 AM by atarikg. 2 replies.

Sort Posts:

  • Dynamically displaying pictures on web applications by using VS 2005 Syste.Drawing method

    04-26-2007, 1:13 PM
    • Loading...
    • SujaRavi
    • Joined on 03-02-2007, 4:59 AM
    • Posts 19

    Here I am posting the sample code for resizing the pictures (thumb nail) on your web applications.  Using this method we can resize the pictures with required width and height depends on application image size.

     


    Calss.Vb file
    --------------

    Public Shared Function CreateThumbnail(ByVal [source] As Drawing.Bitmap, ByVal thumbWi As Integer, ByVal thumbHi As Integer) As Drawing.Bitmap
            If [source].Width < thumbWi And [source].Height < thumbHi Then
                Return [source]
            End If
            Dim ret As System.Drawing.Bitmap = Nothing
            Try
                Dim wi, hi As Integer
                ' this using image graphics interpolatin
                'co-oridination technique

                If [source].Width > [source].Height Then
                    wi = thumbWi
                    hi = CInt([source].Height * (CDec(thumbWi) / [source].Width))
                Else
                    hi = thumbHi
                    wi = CInt([source].Width * (CDec(thumbHi) / [source].Height))
                End If
                ' System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
                ret = New Drawing.Bitmap(wi, hi)
                Dim imagesize As String = Chr(110) + Chr(117)
                Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(ret)
                Try
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
                    g.FillRectangle(System.Drawing.Brushes.White, 0, 0, wi, hi)
                    g.DrawImage([source], 0, 0, wi, hi)
                Finally
                    g.Dispose()
                End Try
            Catch
            End Try
            Return ret
        End Function
       
        Example.Aspx page
        ------------------
       
       
        <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Example.aspx.vb" Inherits="Example" %>
       
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
        <html xmlns="http://www.w3.org/1999/xhtml" >
        <head runat="server">
            <title>Untitled Page</title>
        </head>
        <body>
            <form id="form1" runat="server">
            <div>
           
            </div>
            </form>
        </body>
    </html>

     


    Example.Aspx.vb page
    --------------------


    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Drawing.Drawing2D
    Imports System.Net

    Partial Class Example
        Inherits System.Web.UI.Page
     
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim gImage As String = Request.QueryString("TestImg")
            Dim imageUrl As String = "http://servername/foldername/" + Trim(gImage) + ".jpg"
            Dim imageHeight As Integer = Request.QueryString("h")
            Dim imageWidth As Integer = Request.QueryString("w")
            Dim fullSizeImg As System.Drawing.Image
            Dim c As System.Net.WebClient = New System.Net.WebClient
            Try
                c.UseDefaultCredentials = True
                Dim s As IO.Stream = c.OpenRead(imageUrl)
                fullSizeImg = System.Drawing.Image.FromStream(s)
                s.Close()
                c.Dispose()
                c = Nothing
            Catch ex As Exception
                fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath("NoPtImage .jpg"))
            End Try
            Response.ContentType = "image/jpeg"
            Dim thumbNailImg As System.Drawing.Image
            thumbNailImg = CreateThumbnail(fullSizeImg, imageHeight, imageWidth)
            thumbNailImg.Save(Response.OutputStream, ImageFormat.Jpeg)
            thumbNailImg.Dispose()
        End Sub
    End Class

    ImgDisply.Aspx
    ------------
    <asp:Table runat="server" CellPadding="0" CellSpacing="0" HorizontalAlign="center" Width="100%" >
     <asp:TableRow>
       <asp:TableCell>   <asp:TextBox ID="ProductNum" Width="20%" runat="server"></asp:TextBox>
       <asp:TableCell Width="70%" RowSpan="5" > <img alt="Pic" src="Example.aspx?TestImg=<%=ProductNum.Text %>&h=100&w=90" />
         </asp:TableCell>
       </asp:TableRow>

     

     

    Thanks

    Suji Ravi. m

    Suji Ravi. M
    MCAD
  • Re: Dynamically displaying pictures on web applications by using VS 2005 Syste.Drawing method

    04-28-2007, 1:25 AM
    Answer
    • Loading...
    • atarikg
    • Joined on 06-24-2006, 1:34 PM
    • California, LA
    • Posts 264

    wow thats cool.thanks for that..

     

    Regards.

    I am not a perfect programmer, but i have perfect programmers' habits [ i think so :)]
  • Re: Dynamically displaying pictures on web applications by using VS 2005 Syste.Drawing method

    04-28-2007, 2:37 AM
    • Loading...
    • atarikg
    • Joined on 06-24-2006, 1:34 PM
    • California, LA
    • Posts 264

    Hey i wanna show this resized picture on a image control in the same page. how can i do that ? (i dont wanna use a frame etc.)

     

    Regards...

    I am not a perfect programmer, but i have perfect programmers' habits [ i think so :)]
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter