图片上传问题,,在线等

wziemail 2007-11-28 01:09:15
请问,网站在处理上传图片时,是怎样来控制图片的大小的,(怎样把照片或大图片缩成几K,是不是要先把原图接受到服务器后在处理?)
...全文
104 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
honfei 2007-11-30
  • 打赏
  • 举报
回复
给分呀????
wziemail 2007-11-28
  • 打赏
  • 举报
回复
好的 ,多谢
honfei 2007-11-28
  • 打赏
  • 举报
回复
你直接把我上面的代码拷到本机调试一下就OK了
honfei 2007-11-28
  • 打赏
  • 举报
回复
我刚把我的网站上写了这个给你代码你看一下就明白了;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using DB;
using System.Data.SqlClient;
public partial class User_UpLoadFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}


protected void UPbutton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
{
string name = FileUpload1.PostedFile.FileName;

FileInfo file = new FileInfo(name);
string SavePath = "/upload/";
string fileExes = (System.IO.Path.GetExtension(name)).ToString().ToLower();

Random ran = new Random();
int RandKey = ran.Next(9, 999);
string fileName = "P" + System.DateTime.Now.ToString("yyMMddhhmm") + RandKey + fileExes;
string fileName_s = "S" + fileName.Substring(1);
string webFilePath = Server.MapPath(SavePath + fileName);
string webFilePath_s = Server.MapPath(SavePath + fileName_s);


if (!File.Exists(webFilePath))
{
try
{
FileUpload1.SaveAs(webFilePath);

this.Image1.Visible = true;
this.Image1.ImageUrl = SavePath + fileName_s;
MakeThumbnail(webFilePath, webFilePath_s, 99, 113, "Cut"); // 生成缩略图方法
string Bigimg = SavePath + fileName;
string Smallimg = SavePath + fileName_s;
DataInsert(Bigimg, Smallimg);
error.Text = "提示:上传成功,生成缩略图大小:" + FileUpload1.PostedFile.ContentLength / 1024 + "K";
}
catch (Exception ex)
{
error.Text = "提示:文件上传失败,失败原因:" + ex.Message;
}
}
else
{
error.Text = "提示:文件已经存在,请重命名后上传";
}
}
else
{
error.Text = "提示:文件格式不符,只允许:.jpg/gif";
}
}
}
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

int towidth = width;
int toheight = height;

int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;

switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}


System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(System.Drawing.Color.Transparent);
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);

try
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}


protected void DataInsert(string Bigimage, string Smlimage)
{
ConnDB db = new ConnDB();//这里是我的数据库连接,你改一下 try
{
string UserName = Session["UserName"].ToString();
string sql = "insert into User_Photo (User_name,Photo_big,Photo_small) values('" + UserName + "','" + Bigimage + "','" + Smlimage + "')";
db.ExecSql(sql);
}
catch (SqlException error)
{
throw (error);
}

}
}

HTML页面代码:

<body>
<form id="form1" runat="server">
<div>

<table style="width:383font-family: 宋体, Arial, Helvetica, sans-serif;font-size: 12px;border-spacing: 0px;border-collapse: 0;caption-side: 0;empty-cells: 0">
<tr>
<td align="center">
<asp:Image ID="Image1" runat="server" BorderColor="#EEEEEE" BorderStyle="Solid"
BorderWidth="5px" Height="113px" ImageUrl="#" Visible="False" Width="99px" />
</td>
</tr>
</table>
<table

style="font-family: 宋体, Arial, Helvetica, sans-serif;font-size: 12px;border-spacing: 0px;border-collapse: 0;caption-side: 0;empty-cells: 0"
class="style10">
<tr>
<td class="style8">
<span lang="zh-cn">选择照片:</span></td>
<td class="style9">
<asp:FileUpload ID="FileUpload1" runat="server" BackColor="White"
BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px"
Font-Overline="False" Font-Size="12px" Font-Strikeout="False"
ForeColor="#333333" Width="240px" />
</td>
<td>
<asp:Button ID="UPbutton" runat="server" Text="上传" Width="64px"
CssClass="inputx" Height="23px" onclick="UPbutton_Click" />
</td>
</tr>
</table>
<table width="368px"
style="font-family: 宋体, Arial, Helvetica, sans-serif;font-size: 12px;border-spacing: 0px;border-collapse: 0;table-layout: 0;caption-side: 0;empty-cells: 0">
<tr>
<td >
 </td>
<td class="style7" align="Right" >
 </td>
<td style="height:23px;">

<asp:Label ID="error" runat="server" Font-Size="12px" ForeColor="#FF0066"></asp:Label>

</td>
</tr>
<tr>
<td >  </td>
<td class="style7" align="Right" >  </td>
<td style="height:23px;">  </asp:Label>

<input id="Button1" class="style11" type="button" value="关闭窗口" onclick="winclose()"/></td>
</tr>
</table>

</div>
</form>
</body>
wziemail 2007-11-28
  • 打赏
  • 举报
回复
那业已就是说还是得把文件上传上来,然后处理,生产缩略图,再删掉原来的图片?

正规的网站是怎样实现的
eyuannet 2007-11-28
  • 打赏
  • 举报
回复
上传到服务器 然后生成 缩略图

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧