亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在asp.net中調整圖像大小而不丟失圖像質量

在asp.net中調整圖像大小而不丟失圖像質量

弒天下 2019-12-26 14:21:26
我正在開發一個ASP.NET 3.5 Web應用程序,該應用程序允許我的用戶上傳jpeg,gif,bmp或png圖像。如果上載的圖像尺寸大于103 x 32,則我希望將上載的圖像尺寸調整為103 x32。我已經閱讀了一些博客文章和文章,還嘗試了一些代碼示例,但似乎沒有任何用處。有沒有人成功做到這一點?
查看完整描述

3 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

不久前,我遇到了同樣的問題,并以這種方式處理:


private Image RezizeImage(Image img, int maxWidth, int maxHeight)

{

    if(img.Height < maxHeight && img.Width < maxWidth) return img;

    using (img)

    {

        Double xRatio = (double)img.Width / maxWidth;

        Double yRatio = (double)img.Height / maxHeight;

        Double ratio = Math.Max(xRatio, yRatio);

        int nnx = (int)Math.Floor(img.Width / ratio);

        int nny = (int)Math.Floor(img.Height / ratio);

        Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb);

        using (Graphics gr = Graphics.FromImage(cpy))

        {

            gr.Clear(Color.Transparent);


            // This is said to give best quality when resizing images

            gr.InterpolationMode = InterpolationMode.HighQualityBicubic;


            gr.DrawImage(img,

                new Rectangle(0, 0, nnx, nny),

                new Rectangle(0, 0, img.Width, img.Height),

                GraphicsUnit.Pixel);

        }

        return cpy;

    }


}


private MemoryStream BytearrayToStream(byte[] arr)

{

    return new MemoryStream(arr, 0, arr.Length);

}


private void HandleImageUpload(byte[] binaryImage)

{

    Image img = RezizeImage(Image.FromStream(BytearrayToStream(binaryImage)), 103, 32);

    img.Save("IMAGELOCATION.png", System.Drawing.Imaging.ImageFormat.Gif);

}

我剛剛讀到,這是獲得最高質量的方法。


查看完整回答
反對 回復 2019-12-26
  • 3 回答
  • 0 關注
  • 736 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號