当前位置:首页 » 文件管理 » aspnet上传图片压缩

aspnet上传图片压缩

发布时间: 2022-09-02 13:15:13

‘壹’ 如何在.NET平台下用C#代码实现图片的上传与裁剪和压缩

using System;
using System.Data;
using System.Configuration;
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.IO;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

//检查上传文件的格式是否有效
if(this.FileUpload1.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
{
Response.Write("上传图片格式无效!");
return;
}
//生成原图
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 = 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(".") + "/" + "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(".") + "/" + "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();
}
}
}

‘贰’ C#中如何把一张图片压缩在线急等,求大神

Bitmap newPic = new Bitmap(90,90);//新图片的大小
Graphics g = Graphics.FromImage(newPic);

g.DrawImage(this.pictureBox1.Image, 0,0, newPic.Width,newPic.Height);
//this.pictureBox1.Image就是你的图片

newPic.Save("path", System.Drawing.Imaging.ImageFormat.Bmp);

System.Drawing.Imaging.ImageFormat.Bmp是把图片压成bmp格式的
如果是jpeg的就写 System.Drawing.Imaging.ImageFormat.Jpeg

仅供参考,帮到的不多

‘叁’ ASP.NET(C#)自动解压并判断解压出来的文件格式问题

首先咱滤清一下你的整个逻辑思路:1. 没压缩前首先遍历文件夹,查找swf文件。2. Copy 这个swf文件到一个临时文件夹。3. 把这个临时文件夹打包压缩。4. 上传(用 Fileupload控件) 按照这个逻辑你应该清楚怎么写代码了吧,难点应该是压缩。下面是示例代码: using System;
using System.IO;
using System.IO.Compression;public class GZipTest
{
public static bool CompareData(byte[] buf1, int len1, byte[] buf2, int len2)
{
// Use this method to compare data from two different buffers.
if (len1 != len2)
{
Console.WriteLine("Number of bytes in two buffer are different {0}:{1}", len1, len2);
return false;
} for ( int i= 0; i< len1; i++)
{
if ( buf1[i] != buf2[i])
{
Console.WriteLine("byte {0} is different {1}|{2}", i, buf1[i], buf2[i]);
return false;
}
}
Console.WriteLine("All bytes compare.");
return true;
} }
详细信息参考MSDN:http://msdn.microsoft.com/zh-cn/library/system.io.compression.gzipstream(VS.80).aspx

‘肆’ c#怎么对图片文件在上传过程进行大小压缩

批量JPG图片压缩是一款简单实用、绿色免费、小巧轻便、功能齐全的JPG压缩软件。批量JPG图片压缩能够把图片损耗到最小。

能够支持将大量的JPG图片进行批量压缩,能够按照宽度或者高度等比压缩。批量JPG图片压缩用特别的算法,在对图片进行修改时能避免二次压缩。需要的可以前来下载使用呢。

1、点击压缩软件打开,点击页面上的蓝色按钮(图片压缩)。

4、然后点击页面中的“开始压缩”按钮。

‘伍’ C#代码实现图片压缩至40K以下

任何压缩算法的压缩比都不能无限的提高的。
最有效地方法就是降低图片的分辨率,即像素值。图片的每个像素占用4个字节,假设一般JPG格式的压缩比为1:10的话,40K字节最多只能储存 40000/4*10 = 10万像素的图片,即图片的长乘以宽不超过10万,一般图片长宽比是4:3;所以图片的大小最好不超过 360 X 270 。
降低图片的分辨率就是缩小图片,用Graphics对象、Bitmap对象就能做到。

Bitmap bmpDest = new Bitmap(360,270);
Bitmap bmpSrc = new Bitmap("Source.jpg");
Graphics g = Graphics.FromImge(bmpDest);
g.DrawImage(bmpSrc, rectDest,rectSrc,GraphicsUnit.Pixel);

或者将PictureBox的长和宽限定在360 X 270

‘陆’ C#如何把用户上传的图片文件压缩,然后再把压缩后的图片保存到服务器上

你说的压缩,实际上就是生成缩略图。

你利用 postedFile.InputStream 生成缩略图后,缩略图可以直接保存到服务器上成为文件,不需要再保存到 postedFile 中了。

‘柒’ C#,asp.net 图片在显示前先压缩再显示

压缩是指缩小图片质量和大小吗? 如果是的 可以用C#来处理图片 然后显示在页面
方法 度娘很多

热点内容
plc编程入门视频教程 发布:2024-10-12 15:23:23 浏览:162
总是在新窗口打开文件夹 发布:2024-10-12 15:16:28 浏览:66
怎么连接加密wifi 发布:2024-10-12 15:10:10 浏览:774
为什么安卓手机游戏体验比苹果好 发布:2024-10-12 14:53:10 浏览:880
小额免密码怎么设置 发布:2024-10-12 14:48:23 浏览:477
java接口转换 发布:2024-10-12 14:30:10 浏览:585
启动脚本执行器失败怎么解决 发布:2024-10-12 14:12:32 浏览:586
安卓系统如何设置息屏时间 发布:2024-10-12 14:11:58 浏览:831
配置资源需要什么条件 发布:2024-10-12 14:07:37 浏览:262
java数据库连接的url 发布:2024-10-12 14:03:25 浏览:784