if
(FileUpload1.FileName.ToString() ==
""
)
{
Label3.Text =
"请选择图片!"
;
}
else
{
Boolean FileOK =
false
;
if
(
this
.FileUpload1.HasFile)
{
if
(FileUpload1.PostedFile.ContentLength <= 2097152)
{
Session[
"WorkingImage"
] = Guid.NewGuid().ToString() + Path.GetExtension(FileUpload1.FileName.ToString()).ToLower();
String FileExtension = Path.GetExtension(Session[
"WorkingImage"
].ToString()).ToLower();
String[] allowedExtensions = {
".png"
,
".jpeg"
,
".jpg"
,
".gif"
};
for
(
int
i = 0; i < allowedExtensions.Length; i++)
{
if
(FileExtension == allowedExtensions[i])
{
FileOK =
true
;
}
}
}
else
{
FileOK =
false
;
Response.Write(
"<script language=javascript>alert('图片不要超过 2M 大小!');window.location.href=window.location.href;</script>"
);
}
}
if
(FileOK)
{
try
{
Byte[] oFileByte =
new
byte
[
this
.FileUpload1.PostedFile.ContentLength];
System.IO.Stream oStream =
this
.FileUpload1.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
int
oWidth = oImage.Width;
int
oHeight = oImage.Height;
int
tWidth;
int
tHeight;
if
(oWidth >= oHeight)
{
tWidth = 300;
tHeight = (
int
)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
{
tHeight = 300;
tWidth = (
int
)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
}
Bitmap tImage =
new
Bitmap(tWidth, tHeight);
Graphics g = Graphics.FromImage(tImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(oImage,
new
Rectangle(0, 0, tWidth, tHeight),
new
Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
string
fullFileName =
this
.FileUpload1.PostedFile.FileName.ToString();
fullFileName = fullFileName.Remove(fullFileName.LastIndexOf(
'.'
));
string
filename = fullFileName.Substring(fullFileName.LastIndexOf(
"\\"
) + 1);
string
strLocation = path.ToString();
string
tFullName = strLocation + Session[
"WorkingImage"
].ToString();
try
{
tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
TextBox3.Text =
"uploadfiles/product/"
+ Session[
"WorkingImage"
].ToString();
path =
"../uploadfiles/product/"
+ Session[
"WorkingImage"
].ToString();
}
catch
(Exception)
{
Response.Write(
"<script language=javascript>alert('上传失败,请重试!');window.location.href=window.location.href;</script>"
);
}
finally
{
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
}
catch
(Exception)
{
Response.Write(
"<script language=javascript>alert('上传失败,请重试!');window.location.href=window.location.href;</script>"
);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下