代码如下
Byte[] oFileByte = new byte[this.File1.PostedFile.ContentLength];
System.IO.Stream oStream = this.File1.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度
int tWidth = 100; //设置缩略图初始宽度
int tHeight = 100; //设置缩略图初始高度
if(oWidth >= oHeight)
{
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
{
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 oFullName = Server.MapPath("txt/") + "/" + "o" + DateTime.Now.ToShortDateString().Replace("-","") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存原图的物理路径
string tFullName = Server.MapPath("txt/") + "/" + "t" + DateTime.Now.ToShortDateString().Replace("-","") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存缩略图的物理路径
try
{
//以JPG格式保存图片
oImage.Save(oFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(Exception ex)
{
throw ex;
}
finally
{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
Byte[] oFileByte = new byte[this.File1.PostedFile.ContentLength];
System.IO.Stream oStream = this.File1.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度
int tWidth = 100; //设置缩略图初始宽度
int tHeight = 100; //设置缩略图初始高度
if(oWidth >= oHeight)
{
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
{
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 oFullName = Server.MapPath("txt/") + "/" + "o" + DateTime.Now.ToShortDateString().Replace("-","") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存原图的物理路径
string tFullName = Server.MapPath("txt/") + "/" + "t" + DateTime.Now.ToShortDateString().Replace("-","") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存缩略图的物理路径
try
{
//以JPG格式保存图片
oImage.Save(oFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(Exception ex)
{
throw ex;
}
finally
{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
解决方案 »
- DataSet,DataTable
- DropDownList如何添加optgroup?
- 如果获取HTTP 请求协议的原始文本内容??
- 关于存储过程的两个小问题,高手请进
- JS设置<img 控件的src属性,图片显示不出来
- 关于切换asp.NET版本版本的问题(V1.1 and V2.0)
- 关于VS.Net 2005?
- 我把两个文件放在更目录下不同的文件夹下,想用 panelbar中的选项链接他们的时候应该怎么写路径啊?
- 做完一个asp.net工程以后,ip地址变了,结果工程就打不开了,怎么办?
- 网页导出word默认打开为web视图
- 昨天公司网站被攻击了。
- 怎样在asp.net 里面打开更新锁定。或者在sql 里面打开
{
if (imageUpload.HasFile)
{
string extension = Path.GetExtension(imageUpload.FileName);
if (SafeTool.CheckAllowFile(extension))
{
string path = Server.MapPath("~/Images/FlashImages");
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + extension;
imageUpload.PostedFile.SaveAs(path + "\\" + fileName); Uri uri = Request.Url;
//注意,这里数据库里保存的是相对地址,显示给用户的是绝对URL,这样即使域名或者路径发生相对更改,也不会影响使用
//txtLinkUrl.Text = string.Format("{0}://{1}/download/{2}", uri.Scheme, uri.Authority, fileName);//显示给用户的地址
//hfPath.Value = string.Format("download/{0}", fileName);//数据库中保存的地址
txtImagePath.Text = string.Format("Images/FlashImages/{0}", fileName);
}
else
{
JScript.Alert("你上传的文件格式不符合要求!");
}
}
else
{
JScript.Alert("请选择一个文件上传!");
}
}
tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
这里不是有吗?
oFullName="d:\asdfasdf.jpg";
你自己定义下就可以了~
{
if (file.ContentLength == 0)
{
return UploadMsg.NoFile;
}
if (file.ContentLength > maxSize && maxSize != -1)
{
return UploadMsg.SizeOut;
}
if (file.ContentLength > space && space != -1)
{
return UploadMsg.NoSpace;
}
string[] type = new string[0];
string fileName = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1).ToLower();
switch (fileType)
{
case FileType.Image:
type = "jpg,gif,png,bmp".Split(',');
fileName = FileExt(file.ContentType);
break;
case FileType.Music:
type = "mp3,wma".Split(',');
break;
case FileType.Other:
type = name;
break;
}
bool chkExt = false;
foreach (string ext in type)
{
if (ext == fileName)
{
chkExt = true;
break;
}
}
if (type.Length == 0)
{
chkExt = true;
}
if (!chkExt || fileName == "")
{
return UploadMsg.TypeError;
} if (fileType == FileType.Other)
{
if (stillPath != "")
{
file.SaveAs(stillPath);
return UploadMsg.Success;
}
return UploadMsg.Other;
}
string newPath = DateTime.Now.ToString("yyyy\\/MM\\/dd\\/");// "\"为转义字符
if (!Directory.Exists(path + "\\" + newPath))
{
Directory.CreateDirectory(path + "\\" + newPath);
}
string newFileName = CreateFileName();
while (File.Exists(path + @"\" + newFileName + "." + fileName))
{
newFileName = CreateFileName();
}
if (fileType == FileType.Image)
{
file.SaveAs(path + "\\" + newPath + @"\" + newFileName + "." + fileName);
//MakeThumbnail(path + "\\" + newPath + @"\" + newFileName + "." + fileName,
// path + "\\" + newPath + @"\" + newFileName + "_pre." + fileName,
// preWidth, preHeight, "Cut");
Hashtable ht = GetPhotoExif(path + "\\" + newPath + @"\" + newFileName + "." + fileName);
MakeMark(path + "\\" + newPath + @"\" + newFileName + "." + fileName, ); path = string.Format("Size={0}&FileName={1}&Width={2}&Height={3}&DC={4}&Pic={5}", file.ContentLength, file.FileName.Substring(file.FileName.LastIndexOf(@"\") + 1),
(int)ht["Width"], (int)ht["Height"],
(string)ht["Company"] + " " + (string)ht["DC"],
newPath + newFileName + "." + fileName); }
else
{
file.SaveAs(path + "\\" + newPath + @"\" + newFileName + "." + fileName);
path = string.Format("Size={0}&LocalPath={1}&Path={2}", file.ContentLength,
file.FileName,
newPath + newFileName + "." + fileName);
}
return UploadMsg.Success;
}
/// 上传文件的基类方法
/// </summary>
/// <param name="file">文件流</param>
/// <param name="maxSize">允许的最大尺寸</param>
/// <param name="space">剩余空间</param>
/// <param name="fileType">文件类型</param>
/// <param name="stillPath">静止路径</param>
/// <param name="preWidth">缩略图宽度</param>
/// <param name="preHeight">缩略图高度</param>
/// <param name="path">输入路径 返回结果</param>
/// <param name="">水印</param>
/// <param name="name">可选参数 其他后缀名的文件</param>
/// <returns></returns>
file.SaveAs(path + "\\" + newPath + @"\" + newFileName + "." + fileName
因为我现在("txt/")
是在这个项目的目录,有什么办法可以将文件保存在上级的目录中