首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在上传过程中压缩照片的高度和宽度[asp.net]

如何在上传过程中压缩照片的高度和宽度[asp.net]
EN

Stack Overflow用户
提问于 2016-06-16 05:57:14
回答 1查看 69关注 0票数 3

我想压缩上传照片到大小的Height =600px & Width= 800px,只有当实际的大小大于此期间,upload.photos是保存在sql server2008上与数据类型的图像!

代码语言:javascript
复制
 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;
            }

         }

功能用于图像上传!谁来帮我解决这个..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-16 06:11:29

可能的解决办法如下:

代码语言:javascript
复制
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);
代码语言:javascript
复制
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;
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37850974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档