Image Upload and Display in VB.NET........................

Unknown | 6:02 AM |

<ajaxToolkit:AsyncFileUpload ID="pfepimage" Width="205px" runat="server" UploaderStyle="Modern"
                                                                    ThrobberID="pfeploader" OnClientUploadStarted="uploadStarted1" />
                                                                <asp:Image ID="pfeploader" runat="server" ImageUrl="~/Images/al_loading.gif" Height="15"
                                                                    Width="15" />
                                                                <asp:UpdatePanel ID="UpdImgDisplay" runat="server">
                                                                    <ContentTemplate>
                                                                        <asp:Image runat="server" AlternateText="No Image" ID="imgFirst" Height="50" Width="50" />
                                                                    </ContentTemplate>
                                                                </asp:UpdatePanel>
                                                                <asp:Label runat="server" ID="lblImageError" Text="*" ForeColor="Red" Visible="false"></asp:Label>



  <script type="text/javascript">
            function uploadStarted1() {
            }

        </script>






Private Sub pfepimage_UploadedComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs) Handles pfepimage.UploadedComplete

        Session("imagefile") = sender
        Dim extn As String
        Dim fStrm As Stream
        Dim FileContent As Byte()
        extn = Path.GetExtension(pfepimage.PostedFile.FileName)
        fStrm = pfepimage.PostedFile.InputStream

        Dim FullFileContent As Byte()
        FullFileContent = New Byte(pfepimage.PostedFile.ContentLength - 1) {}
        fStrm.Read(FullFileContent, 0, pfepimage.PostedFile.ContentLength)

        FileContent = createThumbNail(fStrm, 100, 100)
        Session("imageFileName") = pfepimage.PostedFile.FileName.ToString()
        Session("imageContent") = FileContent

        Dim savePath As String = MapPath("~/PFEPlabels/" & System.IO.Path.GetFileName(e.FileName))
        pfepimage.SaveAs(savePath)
        ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "image", "top.$get(""" + imgFirst.ClientID & """).src = 'PFEPlabels/" & System.IO.Path.GetFileName(e.FileName) & "';", True)
        ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "size", "top.$get(""" + lblImageError.ClientID & """).innerHTML = 'Uploaded';", True)

        ModalUnitLoadDataSheet.Show()

    End Sub
    Private Function createThumbNail(ByVal ImageStream As Stream, ByVal tWidth As Double, ByVal tHeight As Double) As Byte()
        Dim g As System.Drawing.Image = System.Drawing.Image.FromStream(ImageStream)
        Dim thumbSize As System.Drawing.Size = New System.Drawing.Size
        thumbSize = NewThumbSize(g.Width, g.Height, tWidth, tHeight)
        Dim imgOutput As New System.Drawing.Bitmap(g, thumbSize.Width, thumbSize.Height)
        Dim imgStream As MemoryStream = New MemoryStream
        Dim thisFormat As System.Drawing.Imaging.ImageFormat = g.RawFormat
        imgOutput.Save(imgStream, thisFormat)
        Dim imgBinLength As Integer = CType(imgStream.Length, Integer)
        Dim imgBin() As Byte = New Byte((imgBinLength) - 1) {}
        imgStream.Position = 0
        Dim n As Integer = imgStream.Read(imgBin, 0, imgBinLength)
        g.Dispose()
        imgOutput.Dispose()
        Return imgBin
    End Function

    Private Function NewThumbSize(ByVal currentwidth As Double, ByVal currentheight As Double, ByVal newWidth As Double, ByVal newHeight As Double) As System.Drawing.Size
        Dim tempMultiplier As Double

        If currentheight > currentwidth Then
            tempMultiplier = newHeight / currentheight
        Else
            tempMultiplier = newWidth / currentwidth
        End If
        Dim NewSize As New System.Drawing.Size(Convert.ToInt16(currentwidth * tempMultiplier), Convert.ToInt16(currentheight * tempMultiplier))
        Return NewSize

    End Function

    Private Sub savePFEPLabel(ByVal pfepId As Integer)
        Dim cmd As New SqlCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "sp_PFEP_Cont_Label_Img_addUpdate"

        If hdnPFEPLabel.Value = "" Then
            cmd.Parameters.Add("@Label_Img_ID", SqlDbType.BigInt, 0).Value = -1
        Else
            cmd.Parameters.Add("@Label_Img_ID", SqlDbType.BigInt, 0).Value = Convert.ToInt64(hdnPFEPLabel.Value)
        End If
        cmd.Parameters("@Label_Img_ID").Direction = ParameterDirection.InputOutput
        cmd.Parameters.Add("@PFEP_ID", SqlDbType.Int, 0).Value = pfepId
        cmd.Parameters.Add("@Container_ID", SqlDbType.BigInt, 0).Value = Convert.ToInt64(comboContainer1.SelectedValue.ToString())
        cmd.Parameters.Add("@FileName", SqlDbType.VarChar, 100).Value = Session("imageFileName")
        cmd.Parameters.Add("@FileContent", SqlDbType.Image).Value = Session("imagecontent")
        _dtLoggedInUser = Session("user_session")
        Dim dt As DataTable
        dt = Session("user_session")
        cmd.Parameters.Add("@Created_ID", SqlDbType.BigInt, 0).Value = Convert.ToInt64(dt.Rows(0)("user_id").ToString())
        cmd.Parameters.Add("Update_ID", SqlDbType.BigInt, 0).Value = Convert.ToInt64(dt.Rows(0)("user_id").ToString())

        DataConnect.GetInstance.ExecuteNonQuery(cmd)
        hdnPFEPLabel.Value = cmd.Parameters("@Label_Img_ID").Value.ToString()

        If File.Exists(Server.MapPath("~/PFEPlabels/") + "img.jpg") Then
            File.Delete(Server.MapPath("~/PFEPlabels/") + "img.jpg")
        End If
    End Sub
    Private Sub showImage(ByVal pfepId As Integer)
        Dim dt As New DataTable
        dt = DataConnect.GetInstance.GetDt("SELECT * FROM PFEP_Cont_Label_Img WHERE PFEP_ID=" & pfepId)
        If dt.Rows.Count > 0 Then
            Session("imageFileName") = dt.Rows(0)("FileName")
            Session("imageContent") = DirectCast(dt.Rows(0)("FileContent"), [Byte]())

        End If
        imgFirst.ImageUrl = "imgHandle.ashx?id=" & pfepId

    End Sub





Imports System.Web
Imports System.Web.Services
Imports System.Data.SqlClient

Public Class imgHandle
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Try
            Dim pfep_id As String = ""
            pfep_id = context.Request.QueryString("id")
            If pfep_id <> "" Then
                Dim dt As DataTable
                dt = DataConnect.GetInstance.GetDt("SELECT * FROM PFEP_Cont_Label_Img WHERE PFEP_ID=" & pfep_id)
                context.Response.Clear()
                If dt.Rows.Count > 0 Then
                    context.Response.BinaryWrite(DirectCast(dt.Rows(0)("FileContent"), [Byte]()))
                Else
                    context.Response.Write("sss")
                End If
                context.Response.End()
                context.Response.Flush()
            End If
        Catch ex As Exception
            context.Response.End()
        End Try
    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....