当前位置:首页 > 代码 > 正文

c图片上传代码(c上传文件)

admin 发布:2022-12-19 20:28 142


本篇文章给大家谈谈c图片上传代码,以及c上传文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

怎么通过代码往C#的windows窗体的picturebox控件里添加本地图片?具体代码是什么?

添加一个picturebox控件,一个button控件,一个openFileDialog控件

在点击button的时候打开一张图片并将打开的图片显示在picturebox上面

//设置能获取的图片的格式

this.openFileDialog1.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";

//显示对话框提示用户打开图片并且用户点击了“打开”按钮

if (this.openFileDialog1.ShowDialog() == DialogResult.OK)

{

//将图片显示在picturebox上

Bitmap pic = new Bitmap(this.openFileDialog1.FileName);

this.pictureBox1.Image = pic;

}

图片的相对路径与绝对路径一般是用在窗体的背景图片或空间的背景图片上

使用绝对路径有一个弊端:当你的图片或者项目被移动到其他电脑上的时候在加载图片的话就会出现错误(例如找不到图片);如果使用相对路径就不会出现这种情况

图片相对路径的用法:将所要用到的图片全部凡在项目的Debug文件夹下(也可以在Debug文件夹下创建一个文件夹专门用来保存图片),在添加图片的时候用

this.BackgroundImage = new Bitmap(Application.StartupPath +@"(\如果图片保存在文件夹中需要加上文件夹名称,如果没有则不加)\图片名称.后缀名");

Application.StartupPath是获取当前项目的Debug文件夹所在的路径,你可以是这弹出看一看:

MessageBox.Show(Application.StartupPath.ToString());

希望能够对你有用!

C#如何上传图片

C/S程序展示图片

C#上传图片很轻松,直接放个OpenFileDialog

上传按钮的点击事件:

openFileDialog1.ShowDialog();

pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

B/S程序上传图片:

C#上传图片很轻松,直接放个FileUpLoad

上传按钮的点击事件:

string file=FileUpload1.FileName;

FileUpload1.SaveAs(MapPath("~/images/"+file));

imgBook.ImageUrl = "~/images/" + file;

asp上传单个图片的最简单代码是什么?

需要三个文件:

第一个(upload.asp)

% Response.Buffer=True %

html

head

meta http-equiv="Content-Type" content="text/html; charset=gb2312"

title图片上传/title

/head

body background="images/bg.gif" topmargin="30" style=""div align="center"

center

table border="1" cellspacing="1" style="border-collapse: collapse" bordercolor="#C0C0C0" width="420" id="AutoNumber69" bgcolor="#F5F5F5"

tr

td width="100%" bgcolor="#F5F5F5" bordercolor="#F5F5F5"form name="form1" method="post" action="uploadsave.asp" enctype="multipart/form-data"

p align="center"span style="font-size: 12px"图片上传:/spanbr

br

 

input type="file" class="f11" name="file1" size="23"

br

br

input type=submit name="submit" value="上 传" class=anniu

br

br

span style="font-size: 12px"文件格式为font color="#EC5106"*.gif,*.jpg,*.jpeg

/font/span/p

/form/td

/tr

/table

/center

/body

/html

第二个文件(uploadsave.asp)

% Response.Buffer=True %

html

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""

html xmlns=""

head

meta http-equiv="Content-Type" content="text/html; charset=gb2312" /

title图片上传/title

/head

body topmargin="20"

!--#include FILE="upload_asjob.asp"--

%

set upload=new upload_5xsoft

set file=upload.file("file1")

if file.fileSize0 then

'自动生成文件名

filename=date()

filename=filenametime()

filename=replace(filename,"-","")

filename=replace(filename,":","")

filename=replace(filename," ","")

filename=filename+"."

filenameend=file.filename

filenameend=split(filenameend,".")

if filenameend(1)="gif" or filenameend(1)="jpg" or filenameend(1)="jpeg" then

filename=filenamefilenameend(1)

file.saveAs Server.mappath("uppic/"filename)

response.write "p align=center"

response.write "图片已成功上传至服务器中,请使用下面红色地址添加图片,brp align=centera href=upload.asp点击这里重新上传图片!/a"

response.write "/p"

response.write "p align=center"

response.write "font color=red"

response.write "asjobcom/uppic/"filename

response.write "/font/p"

response.write "p align=center"

response.write "a href='uppic/"filename"' target=_blank点击这里查看上传的图片/a"

response.write "/p"

response.write "p align=centera href=javascript:window.close()【关闭窗口】/a"

else

response.write "文件格式不对"

response.write "a href=upload.asp返回"

end if

set file=nothing

else

response.write "文件内容不能为空"

response.write "a href=upload.asp返回"

end if

set upload=nothing

response.write "br"

%

最后一个(upload_asjob.asp)

SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT

dim upfile_5xSoft_Stream

Class upload_5xSoft

dim Form,File,Version

Private Sub Class_Initialize

dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile

dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr

Version="化境编程界HTTP上传程序 Version 1.0"

if Request.TotalBytes1 then Exit Sub

set Form=CreateObject("Scripting.Dictionary")

set File=CreateObject("Scripting.Dictionary")

set upfile_5xSoft_Stream=CreateObject("Adodb.Stream")

upfile_5xSoft_Stream.mode=3

upfile_5xSoft_Stream.type=1

upfile_5xSoft_Stream.open

upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes)

vbEnter=Chr(13)Chr(10)

iDivLen=inString(1,vbEnter)+1

strDiv=subString(1,iDivLen)

iFormStart=iDivLen

iFormEnd=inString(iformStart,strDiv)-1

while iFormStart iFormEnd

iStart=inString(iFormStart,"name=""")

iEnd=inString(iStart+6,"""")

mFormName=subString(iStart+6,iEnd-iStart-6)

iFileNameStart=inString(iEnd+1,"filename=""")

if iFileNameStart0 and iFileNameStartiFormEnd then

iFileNameEnd=inString(iFileNameStart+10,"""")

mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10)

iStart=inString(iFileNameEnd+1,vbEntervbEnter)

iEnd=inString(iStart+4,vbEnterstrDiv)

if iEndiStart then

mFileSize=iEnd-iStart-4

else

mFileSize=0

end if

set theFile=new FileInfo

theFile.FileName=getFileName(mFileName)

theFile.FilePath=getFilePath(mFileName)

theFile.FileSize=mFileSize

theFile.FileStart=iStart+4

theFile.FormName=FormName

file.add mFormName,theFile

else

iStart=inString(iEnd+1,vbEntervbEnter)

iEnd=inString(iStart+4,vbEnterstrDiv)

if iEndiStart then

mFormValue=subString(iStart+4,iEnd-iStart-4)

else

mFormValue=""

end if

form.Add mFormName,mFormValue

end if

iFormStart=iformEnd+iDivLen

iFormEnd=inString(iformStart,strDiv)-1

wend

End Sub

Private Function subString(theStart,theLen)

dim i,c,stemp

upfile_5xSoft_Stream.Position=theStart-1

stemp=""

for i=1 to theLen

if upfile_5xSoft_Stream.EOS then Exit for

c=ascB(upfile_5xSoft_Stream.Read(1))

If c 127 Then

if upfile_5xSoft_Stream.EOS then Exit for

stemp=stempChr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))ChrB(c)))

i=i+1

else

stemp=stempChr(c)

End If

Next

subString=stemp

End function

Private Function inString(theStart,varStr)

dim i,j,bt,theLen,str

InString=0

Str=toByte(varStr)

theLen=LenB(Str)

for i=theStart to upfile_5xSoft_Stream.Size-theLen

if iupfile_5xSoft_Stream.size then exit Function

upfile_5xSoft_Stream.Position=i-1

if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then

InString=i

for j=2 to theLen

if upfile_5xSoft_Stream.EOS then

inString=0

Exit for

end if

if AscB(upfile_5xSoft_Stream.Read(1))AscB(MidB(Str,j,1)) then

InString=0

Exit For

end if

next

if InString0 then Exit Function

end if

next

End Function

Private Sub Class_Terminate

form.RemoveAll

file.RemoveAll

set form=nothing

set file=nothing

upfile_5xSoft_Stream.close

set upfile_5xSoft_Stream=nothing

End Sub

Private function GetFilePath(FullPath)

If FullPath "" Then

GetFilePath = left(FullPath,InStrRev(FullPath, "\"))

Else

GetFilePath = ""

End If

End function

Private function GetFileName(FullPath)

If FullPath "" Then

GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)

Else

GetFileName = ""

End If

End function

Private function toByte(Str)

dim i,iCode,c,iLow,iHigh

toByte=""

For i=1 To Len(Str)

c=mid(Str,i,1)

iCode =Asc(c)

If iCode0 Then iCode = iCode + 65535

If iCode255 Then

iLow = Left(Hex(Asc(c)),2)

iHigh =Right(Hex(Asc(c)),2)

toByte = toByte chrB("H"iLow) chrB("H"iHigh)

Else

toByte = toByte chrB(AscB(c))

End If

Next

End function

End Class

Class FileInfo

dim FormName,FileName,FilePath,FileSize,FileStart

Private Sub Class_Initialize

FileName = ""

FilePath = ""

FileSize = 0

FileStart= 0

FormName = ""

End Sub

Public function SaveAs(FullPath)

dim dr,ErrorChar,i

SaveAs=1

if trim(fullpath)="" or FileSize=0 or FileStart=0 or FileName="" then exit function

if FileStart=0 or right(fullpath,1)="/" then exit function

set dr=CreateObject("Adodb.Stream")

dr.Mode=3

dr.Type=1

dr.Open

upfile_5xSoft_Stream.position=FileStart-1

upfile_5xSoft_Stream.copyto dr,FileSize

dr.SaveToFile FullPath,2

dr.Close

set dr=nothing

SaveAs=0

end function

End Class

/SCRIPT

c图片上传代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c上传文件、c图片上传代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/22883.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载