當前位置:首頁 » 文件管理 » 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#來處理圖片 然後顯示在頁面
方法 度娘很多

熱點內容
luxu776ftp下載 發布:2024-10-12 16:21:32 瀏覽:767
電腦上怎樣壓縮文件 發布:2024-10-12 16:12:36 瀏覽:555
iphone壓縮包怎麼解壓 發布:2024-10-12 16:11:59 瀏覽:105
刀心點編程 發布:2024-10-12 16:06:29 瀏覽:536
阿里雲伺服器反應慢 發布:2024-10-12 16:03:22 瀏覽:628
丙烯的存儲 發布:2024-10-12 15:32:46 瀏覽:37
plc編程入門視頻教程 發布:2024-10-12 15:23:23 瀏覽:165
總是在新窗口打開文件夾 發布:2024-10-12 15:16:28 瀏覽:66
怎麼連接加密wifi 發布:2024-10-12 15:10:10 瀏覽:774
為什麼安卓手機游戲體驗比蘋果好 發布:2024-10-12 14:53:10 瀏覽:880