asp.net - 如何在上传时压缩照片的高度和宽度[asp.net]

标签 asp.net

我想在上传期间将上传的照片压缩为高度=600px & 宽度= 800px尺寸[仅当实际尺寸大于此]。照片保存在sql上server2008 带有数据类型图像!

 public byte[] imagetoByte()
        {
            if (FileUpload1.HasFile)
            {
                int imageFilelength = FileUpload1.PostedFile.ContentLength;
                byte[] ph = new byte[imageFilelength];
                HttpPostedFile ima = FileUpload1.PostedFile;
                MemoryStream memoryStream = new MemoryStream();
                ima.InputStream.Read(ph, 0, imageFilelength);
                return ph;
            }
            else
            {
                return null;
            }

         }

函数用于图片上传!!有人帮我解决这个问题..

最佳答案

可能的解决方案可以是:

int imageFilelength = FileUpload1.PostedFile.ContentLength;
byte[] ph = new byte[imageFilelength];

MemoryStream ms = new MemoryStream(ph);
Image img = System.Drawing.Image.FromStream(ms);

//Call function to resize 
Image ResizedImage = RezizeImage(img, 500, 500);

//Save Image
ResizedImage.Save("IMAGELOCATION.png", System.Drawing.Imaging.ImageFormat.Gif);
<小时/>
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;
    }

}

关于asp.net - 如何在上传时压缩照片的高度和宽度[asp.net],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37850974/

相关文章:

带有 Javascript 的 ASP.NET Web 用户控件在页面上多次使用 - 如何使 javascript 函数指向正确的控件

c# - 在 ASP.NET WebForms 项目中的什么地方可以找到 "System.Web.UI.Control"命名空间?

javascript - Panel 的 DefaultButton 属性如何工作?

ASP.NET MVC 将模型与文件一起*传递回 Controller

c# - 为什么 GridViewUpdateEventArgs#NewValues 和 GridViewUpdateEventArgs#OldValues 为空?

c# - 从 aspx 页面调用代码隐藏函数的问题

asp.net - 如何使用 linq 在 asp.net/vb.net 中过滤列表

asp.net - ext.net 中的客户端验证和显示字段样式

asp.net 数据网格边框在其他浏览器中显示不同(错误)

c# - 为应用程序提供提升的 UAC