上传时压缩或调整图像文件大小 [英] compress or resizing an image file when uploading

查看:85
本文介绍了上传时压缩或调整图像文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调整大小并上传图片文件。当我在本地系统中使用它时能够做到这一点,但是当我将它发布到我的服务器时我无法上传图像可能是这是我的代码的问题

am trying to resize and upload an image file. Am able to do it when am using it in local system but when i publish it to my server am not able to upload an image what might be the prob this is my code

public static bool IsImage(HttpPostedFile postedFile)
        {
            //-------------------------------------------
            //  Check the image mime types
            //-------------------------------------------
            if (postedFile.ContentType.ToLower() != "image/jpg" &&
                        postedFile.ContentType.ToLower() != "image/jpeg" &&
                        postedFile.ContentType.ToLower() != "image/pjpeg" &&
                        postedFile.ContentType.ToLower() != "image/gif" &&
                        postedFile.ContentType.ToLower() != "image/x-png" &&
                        postedFile.ContentType.ToLower() != "image/png")
            {
                return false;
            }

            //-------------------------------------------
            //  Check the image extension
            //-------------------------------------------
            if (Path.GetExtension(postedFile.FileName).ToLower() != ".jpg"
                && Path.GetExtension(postedFile.FileName).ToLower() != ".png"
                && Path.GetExtension(postedFile.FileName).ToLower() != ".gif"
                && Path.GetExtension(postedFile.FileName).ToLower() != ".jpeg")
            {
                return false;
            }
            return true;
        }

if(IsImage(imgUpload.PostedFile))
{
string imagePath = imgUpload.PostedFile.FileName;

                            byte[] buffer = getResizedImage(imagePath, 99, 99);

}

protected byte[] getResizedImage(String path, int width, int height)
        {
            System.IO.MemoryStream outStream = new System.IO.MemoryStream();
            try
            {
               Bitmap imgIn = new Bitmap(path);
                double y = imgIn.Height;
                double x = imgIn.Width;
              double factor = 1;
                if (width > 0)
                {
                    factor = width / x;
                }
                else if (height > 0)
                {
                    factor = height / y;
                }
                
                Bitmap imgOut = new Bitmap((int)(x * factor), (int)(y * factor));

                // Set DPI of image (xDpi, yDpi)
                imgOut.SetResolution(72, 72);
                Graphics g = Graphics.FromImage(imgOut);
                g.Clear(Color.White);
                g.DrawImage(imgIn, new Rectangle(0, 0, (int)(factor * x), (int)(factor * y)),
                  new Rectangle(0, 0, (int)x, (int)y), GraphicsUnit.Pixel);
                imgOut.Save(outStream, getImageFormat(path));
               
            }
            catch (Exception ex)
            {
                Common.ExceptionLogging("MNWeb", "getResizedImage", ex.ToString());
            }
            return outStream.ToArray();
        }

        protected string getContentType(String path)
        {
            switch (Path.GetExtension(path))
            {
                case ".bmp": return "Image/bmp";
                case ".gif": return "Image/gif";
                case ".jpg": return "Image/jpeg";
                case ".png": return "Image/png";
                default: break;
            }
            return "";
        }

        protected ImageFormat getImageFormat(String path)
        {
            switch (Path.GetExtension(path))
            {
                case ".bmp": return ImageFormat.Bmp;
                case ".gif": return ImageFormat.Gif;
                case ".jpg": return ImageFormat.Jpeg;
                case ".png": return ImageFormat.Png;
                default: break;
            }
            return ImageFormat.Jpeg;
        }

推荐答案

请检查您是否拥有足够的服务器位置权限来执行此任务。





如果满足您的查询,请选择答案。





问候,

Dheeraj Gupta
Please check that you have sufficient permissions over server location to perform this tasks.


Mark as answer if it suffice your query.


Regards,
Dheeraj Gupta


这篇关于上传时压缩或调整图像文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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