当前位置: 编程技术>.net/c#/asp.net
c#(vb.net)上传图片并生成缩略图的实现代码
来源: 互联网 发布时间:2014-08-30
本文导语: asp.net上传图片并生成缩略图的实现代码,分c#与vb.net两个版本,有需要的朋友参考下。 1、c#版的 代码示例: void Page_Load(Object sender, EventArgs e) { if (!Page.IsPostBack) { ImgPreview.Visible = false; } } void GetThumbnailImage(int ...
asp.net上传图片并生成缩略图的实现代码,分c#与vb.net两个版本,有需要的朋友参考下。
1、c#版的
代码示例:
void Page_Load(Object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ImgPreview.Visible = false;
}
}
void GetThumbnailImage(int width, int height, string strInfo, int left, int right)
{
string file = "Uploads/" + uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\') + 1);
string newfile = "Uploads/" + uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\') + 1) + ".jpg";
string strAdd = strInfo;
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(file));
System.Drawing.Image thumbnailImage =
oldimage.GetThumbnailImage(width, height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Response.Clear();
Bitmap output = new Bitmap(thumbnailImage);
Graphics g = Graphics.FromImage(output);
g.DrawString(strAdd, new Font("Courier New", 14), new SolidBrush(Color.Red), left, right);
output.Save(Server.MapPath(newfile), System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ContentType = "image/gif";
ImgPreview.Visible = true;
ImgPreview.ImageUrl = newfile;
}
bool ThumbnailCallback()
{
return true;
}
void Button_Click(object sender, EventArgs e)
{
int width, height, left, right;
string strAddInfo = txtAddInfo.Text;
width = Int32.Parse(txtWidth.Text);
height = Int32.Parse(txtHeight.Text);
left = Int32.Parse(txtLeft.Text);
right = Int32.Parse(txtRight.Text);
if (!(uploadFile.PostedFile.ContentLength > 0))
{
lblErrInfo.Text = "没有选择文件";
}
else
{
string path = Server.MapPath("./Uploads/" + uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\') + 1));
if (File.Exists(path))
{
lblErrInfo.Text = "已经有同名文件";
}
else
{
uploadFile.PostedFile.SaveAs(path);
GetThumbnailImage(width, height, strAddInfo, left, right);
}
}
}
上传图片并生成缩略图
width:100
height:150
添加信息: AspxBoy.Com
信息位置:left:10
right:135
2、vb.net版的
代码示例:
Sub UploadFile(sender As Object, e As EventArgs)
If FileUp.PostedFile.ContentLength = 0 Then
FileInfo.Visible = False
Exit Sub
Else
FileInfo.Visible = True
FDisplay1.Visible = True
End If
FSize.Text ="上传文件大小"+ CStr(FileUp.PostedFile.ContentLength/1024)+"KB"
FName.Text = "已上传文件名:"+FileUp.PostedFile.FileName+"
"+FName.Text
"写入数据库
on error resume next
dim myconn as sqlconnection
dim mycomm as sqlcommand
dim sql as string
dim id as integer
Dim image,anewimage As System.Drawing.Image
dim width,height,newwidth,newheight as integer
Dim callb As System.Drawing.Image.GetThumbnailImageAbort
myConn=New sqlconnection(ConfigurationSettings.AppSettings("数据库"))
myconn.open()
sql="insert into picture (姓名,班级,介绍,属性) values (""&request.cookies("dgxyl").values("dgxylname")&"",""&request.cookies("dgxyl").values("dgxylbj")&"",""&trim(request("TextBox1"))&"",""&request("r1")&"")"
Mycomm=New sqlcommand(sql,myconn)
mycomm.executenonquery()
myconn.close()
Dim myCommand As New SqlCommand("select top 1 id from picture order by id desc", myConn)
myCommand.Connection.Open()
Dim myReader As SqlDataReader=mycommand.executereader()
if myReader.Read() then
id=myReader("id")
end if
myconn.close()
"保存图片
FileUp.PostedFile.SaveAs( Server.MapPath("classpic")&cstr(id)&".jpg" )
"生成缩略图
image=System.Drawing.Image.FromFile(Server.MapPath("/classpic/"+cstr(id)+".jpg"))
width=image.Width
height=image.height
if width>height then
newwidth=250
newheight=image.height/image.Width*newwidth
else
newheight=250
newwidth=image.Width/image.height*newheight
end if
response.write("id="+cstr(id)+"width="+cstr(Width)+";height="+cstr(height)+" ")
response.write("newwidth="+cstr(newwidth)+";newheight="+cstr(newheight)+"
")
aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr())
aNewImage.Save(Server.MapPath("/smallpic/"+cstr(id)+".jpg"))
image.Dispose()
Dim FileSplit() As String = Split( FileUp.PostedFile.FileName, "" )
Dim FileName As String = FileSplit(FileSplit.Length-1)
Dim Exts() As String = Split( FileName, "." )
Dim Ext As String = LCase(Exts(Exts.Length-1))
FDisplay.Text = "查看上传文件"
FDisplay1.text="返回"
End Sub
请正确填写下面各项
上传文件
图片属性:本班全校(本班则只在本班显示,全校则在全校显示)
图片说明:
注意: