图像调整大小并降低图像分辨率 [英] Image resizing and decreasing the image resolution

查看:561
本文介绍了图像调整大小并降低图像分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文件上传控件和按钮的webform,在上传图像时我想调整它的大小并减小它的物理尺寸(以KB为单位)。



我使用的方法是调整大小但图像大小仍然很大,我试图使用

 Bitmap.SetResolution(,)

降低分辨率,但它没有影响以KB为单位的图像大小



这是我的aspx代码:

 <   body  >  
< 表格 id = form1 runat = server &克t;
< div >
< asp:FileUpload ID = FileUpload1 runat = server / >
< asp:按钮 ID = Button1 runat < span class =code-keyword> = server 文本 = 按钮 OnClick = Button1_Click / >
< / div >
< / form >
< / body >





以下是背后的代码:

 < span class =code-key单词> protected   void  Button1_Click( object  sender,EventArgs e)
{
if (FileUpload1.HasFile)
{
int Image_Width = 830 ;
int Image_Height = 430 ;
string ImageName = f _ + DateTime.Now.Millisecond + DateTime.Now.DayOfYear + DateTime.Now.Minute + DateTime.Now.Hour;
HttpPostedFile pf = FileUpload1.PostedFile;
ImageName + = Path.GetExtension(pf.FileName);
System.Drawing.Image bm_Large = System.Drawing.Image.FromStream(pf.InputStream);
bm_Large = ResizeBitmap((Bitmap)bm_Large,Image_Width,Image_Height);
string pathToSave_Large = Server.MapPath( 〜/ images /测试Ajax / + ImageName);
bm_Large.Save(pathToSave_Large);
FileUpload1.PostedFile.InputStream.Close();
}
}

public static 位图ResizeBitmap (位图b, int nWidth, int nHeight)
{
位图结果= new 位图(nWidth,nHeight);
使用(图形g = Graphics.FromImage((System.Drawing.Image)结果))
g.DrawImage(b, 0 0 ,nWidth,nHeight);
// result.SetResolution(30,30);
返回结果;
}

解决方案

Tis网站的搜索肯定会返回参考 [ ^ ]。

但无论如何:看看Bitmap的保存重载 [ ^ ]。如果大小很重要,你可以压缩位图(但是你会丢失图像质量)。


参考 - 如何在不丢失图像质量的情况下调整图像大小或如何在不损失质量的情况下减少图像文件大小使用asp.net [ ^ $。

引用:

运行我的应用程序后,我上传照片以在完成过程后调整大小我的图片尺寸大幅减少到218 KB您是否相信这一点但是我们可以通过使用上面的代码尽可能地减少图像尺寸而不会丢失图像质量和我的图像质量与原始图像相同检查


I have a webform contains a file upload control and a button, when uploading the image I want to resize it and decrease its physical size (in KB).

I use a method that Do the resizing but the image size still big, i tried to decrease the resolution using

Bitmap.SetResolution( , )

but it didn't affect the size of the image in KB

Here's my aspx code:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>



and Here's the code behind:

protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        int Image_Width = 830;
        int Image_Height = 430;
        string ImageName = "f_" + DateTime.Now.Millisecond + DateTime.Now.DayOfYear + DateTime.Now.Minute + DateTime.Now.Hour;
        HttpPostedFile pf = FileUpload1.PostedFile;
        ImageName += Path.GetExtension(pf.FileName);
        System.Drawing.Image bm_Large = System.Drawing.Image.FromStream(pf.InputStream);
        bm_Large = ResizeBitmap((Bitmap)bm_Large, Image_Width, Image_Height);
        string pathToSave_Large = Server.MapPath("~/images/Test Ajax/" + ImageName);
        bm_Large.Save(pathToSave_Large);
        FileUpload1.PostedFile.InputStream.Close();
    }
}

public static Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
{
    Bitmap result = new Bitmap(nWidth, nHeight);
    using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
        g.DrawImage(b, 0, 0, nWidth, nHeight);
    //result.SetResolution(30, 30);
    return result;
}

解决方案

Tis site's seach would surely have returned a reference here[^].
But anyway: have a look at Bitmap's Save overloads[^]. You can compress a bitmap, if size matters (and you're going to lose image quality, though).


Refer - how to resize image without losing image quality or how to reduce image file size without losing quality using asp.net[^].

Quote:

After run my application I upload above pic to resize after completion process my pic size reduces drastically to 218 KB do you believe this but it’s true we can reduce image size as much as possible by using above code without losing quality of image and my image quality same as original one check it


这篇关于图像调整大小并降低图像分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆