问题描述
我正在尝试在我的网站上启用图像压缩,如果有任何用户上传图像,图像应该被压缩。我试过了下面的代码,但问题是它正在压缩所有图像,当文件大小约为2mb或更多时,它会很好但当文件大小为10kb时,50kb它甚至会尝试压缩此图像并且最终输出已损坏/模糊图像。
请帮助这个
Hi,
I am trying to enable Image compression on my website, where if any user uploads an Image, the image should get compressed.I have tried out below code but the issue is that it is compressing all Images,what happens is when a file size is around 2mb or more it works great but when file size is 10kb,50kb it even tries to compress this image as well and final output is corrupt/blurred image.
Please help on this
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Generate a Thumbnails from Uploaded Image</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fileupload1" runat="server" />
<asp:Button ID="btnsave" runat="server" Text="Upload" onclick="btnsave_Click" />
</div>
<div>
<asp:DataList ID="dtlist" runat="server" RepeatColumns="3" CellPadding="5">
<itemtemplate>
<asp:Image ID="Image1" ImageUrl='<%# Bind("Name", "~/Images/{0}") %>' runat="server" />
<br />
<asp:HyperLink ID="HyperLink1" Text='<%# Bind("Name") %>' NavigateUrl='<%# Bind("Name", "~/Images/{0}") %>' runat="server"/>
</itemtemplate>
<itemstyle bordercolor="Brown" borderstyle="dotted" borderwidth="3px" horizontalalign="Center">
VerticalAlign="Bottom" />
</itemstyle></div>
</form>
</body>
</html>
之后在代码中添加以下命名空间
After that add following namespaces in code behind
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
完成添加后命名空间写下面的代码
After completion of adding namespaces write the following code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
}
protected void BindDataList()
{
DirectoryInfo dir = new DirectoryInfo(MapPath("Images"));
FileInfo[] files = dir.GetFiles();
ArrayList listItems = new ArrayList();
foreach (FileInfo info in files)
{
listItems.Add(info);
}
dtlist.DataSource = listItems;
dtlist.DataBind();
}
protected void btnsave_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
string targetPath = Server.MapPath("Images/" + filename);
Stream strm = fileupload1.PostedFile.InputStream;
var targetFile = targetPath;
//Based on scalefactor image size will vary
GenerateThumbnails(0.5, strm, targetFile);
BindDataList();
}
private void GenerateThumbnails(double scaleFactor, Stream sourcePath, string targetPath)
{
using (var image = Image.FromStream(sourcePath))
{
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var thumbnailImg = new Bitmap(newWidth, newHeight);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbGraph.DrawImage(image, imageRectangle);
thumbnailImg.Save(targetPath, image.RawFormat);
}
}
推荐答案
这篇关于客户端的图像压缩代码和Loader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!