Click here to Skip to main content
15,867,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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;
        }
Posted
Updated 30-Jul-13 20:29pm
v2
Comments
walterhevedeich 31-Jul-13 2:29am    
Any error while running it on the server?
adriancs 31-Jul-13 2:44am    
You should post the error/exception message too.

1 solution

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
 
Share this answer
 
Comments
adriancs 31-Jul-13 2:44am    
Its

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900