如何更新或上传asp.net c#中的图像? [英] How upadate or uploading image in asp.net c#?

查看:49
本文介绍了如何更新或上传asp.net c#中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想更新或上传图片,比如改变脸谱的个人资料图片..我该怎么办..?

谢谢你..

hello,
I want to update or upload image like changing profile picture of facebook.. how can i do..?
thank u..

推荐答案

我在你的代码和代码之间混合了主题;)



加价:

I made a mix between your code and code of this thread ;)

Markup :
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default2.aspx.vb" Inherits="MyTestProject.Default2" %>

<!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></title>
    <style type="text/css">
        #profile_pic_wrapper{ position:relative; border:#ccc solid 1px; width:200px; height:200px;}
        #profile_pic_wrapper a{ position:absolute; display:none; top:0; right:0; line-height:20px; padding:5px; color:#fff; background-color:#333; text-decoration:underline;}
        #profile_pic_wrapper:hover a{ display:block; }
        #profile_pic_wrapper:hover a:hover{text-decoration:none;}
        .profile_pic{ width:200px; height:200px;}
    </style>
    <script type="text/javascript" language="javascript">
        function Func() {
            document.getElementById("filUpload").click();
            document.getElementById("imgProfilePic");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <div id="profile_pic_wrapper">
            <asp:Image ID="imgProfilePic" runat="server" ImageUrl="~/cute_girl_001facebook_timeline_cover.jpg" CssClass="profile_pic"/>
            <!--<asp:HyperLink ID="lnkChangePicture" runat="server" NavigateUrl="~/Default3.aspx">Change Picture</asp:HyperLink>-->
            <label onclick="Func()">Change picture</label>
            <asp:Button runat="server" ID="btnUpload" />
            <input type="file" id="filUpload" style="visibility:hidden" runat="server" accept="image/*"/>
            <asp:Label runat="server" ID="lblOutput"></asp:Label>
        </div>

    </div>
    </form>
</body>
</html>



代码背后:


Code behind :

Imports System.Drawing

Partial Public Class Default2
    Inherits System.Web.UI.Page

    ''' <summary>
    '''
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    ''' <summary>
    '''
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click

        'Initialize variables
        Dim sSavePath As String
        Dim sThumbExtension As String
        Dim intThumbWidth As Integer
        Dim intThumbHeight As Integer

        'Set constant values
        sSavePath = "images/"
        sThumbExtension = "_thumb"
        intThumbWidth = 160
        intThumbHeight = 120

        'If file field isn’t empty
        If filUpload.PostedFile IsNot Nothing Then

            'Check file size (mustn’t be 0)
            Dim myFile As HttpPostedFile = filUpload.PostedFile
            Dim nFileLen As Integer = myFile.ContentLength
            If (nFileLen = 0) Then
                lblOutput.Text = "No file was uploaded."
                Return
            End If

            'Check file extension (must be JPG)
            If Not System.IO.Path.GetExtension(myFile.FileName).ToLower() = ".jpg" Then
                lblOutput.Text = "The file must have an extension of JPG"
                Return
            End If

            'Read file into a data stream
            Dim myData(nFileLen) As Byte
            myFile.InputStream.Read(myData, 0, nFileLen)

            'Make sure a duplicate file doesn’t exist.  If it does, keep on appending an
            'incremental numeric until it is unique
            Dim sFilename As String = System.IO.Path.GetFileName(myFile.FileName)
            Dim file_append As Integer = 0
            While (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
                file_append += 1
                sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) _
                                 + file_append.ToString() + ".jpg"
            End While

            'Save the stream to disk
            Dim newFile As System.IO.FileStream = New System.IO.FileStream(Server.MapPath(sSavePath + sFilename), System.IO.FileMode.Create)
            newFile.Write(myData, 0, myData.Length)
            newFile.Close()

            'Check whether the file is really a JPEG by opening it
            Dim myCallBack As Image.GetThumbnailImageAbort = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
            Dim myBitmap As Bitmap
            Try
                myBitmap = New Bitmap(Server.MapPath(sSavePath + sFilename))
                'If jpg file is a jpeg, create a thumbnail filename that is unique.
                file_append = 0
                Dim sThumbFile As String = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) _
                                           + sThumbExtension + ".jpg"
                While (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile)))
                    file_append += 1
                    sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) _
                                + file_append.ToString() + sThumbExtension + ".jpg"
                End While

                'Save thumbnail and output it onto the webpage
                Dim myThumbnail As Image = myBitmap.GetThumbnailImage(intThumbWidth, intThumbHeight, myCallBack, IntPtr.Zero)
                myThumbnail.Save(Server.MapPath(sSavePath + sThumbFile))
                imgProfilePic.ImageUrl = sSavePath + sThumbFile

                'Displaying success information
                lblOutput.Text = "File uploaded successfully!"

                'Destroy objects
                myThumbnail.Dispose()
                myBitmap.Dispose()
            Catch errArgument As ArgumentException
                'The file wasn't a valid jpg file
                lblOutput.Text = "The file wasn't a valid jpg file."
                System.IO.File.Delete(Server.MapPath(sSavePath + sFilename))
            End Try
        End If

    End Sub

    Public Function ThumbnailCallback() As Boolean
        Return False
    End Function

End Class


ASP文件上传控件是你的朋友;)

The ASP File Upload control is your friend ;)
<asp:FileUpload ID="fuImage" runat="server" />


这篇关于如何更新或上传asp.net c#中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆